您的位置:首页 > 编程语言 > MATLAB

matlab gui matlab gui 鼠标点击显示图像颜色值

2012-02-17 20:03 453 查看
首先看看效果



首先功能说明下,运行后通过myfile菜单打开一幅图片之后在axes中显示,由于要使用图片的放大缩小等功能将figure 的菜单栏与工具栏都一并打开了。
界面编程主要是callback函数编写,不多说了直接上代码

function varargout = mytest(varargin)
% MYTEST M-file for mytest.fig
%      MYTEST, by itself, creates a new MYTEST or raises the existing
%      singleton*.
%
%      H = MYTEST returns the handle to a new MYTEST or the handle to
%      the existing singleton*.
%
%      MYTEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MYTEST.M with the given input arguments.
%
%      MYTEST('Property','Value',...) creates a new MYTEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mytest_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mytest_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help mytest

% Last Modified by GUIDE v2.5 17-Feb-2012 15:10:23

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @mytest_OpeningFcn, ...
'gui_OutputFcn',  @mytest_OutputFcn, ...
'gui_LayoutFcn',  [] , ...
'gui_Callback',   []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before mytest is made visible.
function mytest_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mytest (see VARARGIN)

% Choose default command line output for mytest
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
setappdata(handles.myfigure,'is_open',0);
set(handles.myfigure,'menubar','figure','toolbar','figure');

%  set(handles.myaxes,'OuterPosition',[0,-30,w/3,h]);

% UIWAIT makes mytest wait for user response (see UIRESUME)
% uiwait(handles.myfigure);

% --- Outputs from this function are returned to the command line.
function varargout = mytest_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function myfigure_WindowButtonDownFcn(hObject, eventdata, handles)
% [x,y,but]=ginput(1);
%     if(but==1)
% %         if x>0&&y>0
%         %  text(x,y,'q1');
%         x1=num2str(x);
%         y1=num2str(y);
%         s=strcat(x1,',',y1);
%         set(handles.mytxt,'String',s);
% %         end
%     end
is_open=getappdata(handles.myfigure,'is_open');
if is_open==1
p=[0 0 0];
p = get(handles.myaxes,'currentpoint');

A=get(handles.myfigure,'UserData');
r=getappdata(handles.myfigure,'si');
x=num2str(p(1));
y=num2str(p(3));
s=strcat(x,',',y);
set(handles.mytxt,'String',s);
tx=floor(p(3));
ty=floor(p(1));
disp(r.Width)
disp(r.Height)
if tx<=r.Height&&ty<=r.Width&&tx>=1&&ty>=1
r=A(tx,ty,1);
g=A(tx,ty,2);
b=A(tx,ty,3);
rs=strcat(num2str(r),',',num2str(g),',',num2str(b));
set(handles.rgb,'String',rs);
stru=datacon(r,g,b);
set(handles.mystruct,'String',stru);
end
end
% dynpoint(handles.myaxes) ;
% col=get(handles.myaxes,'color');
%
%         r=num2str(sum(col));
%
%         set(handles.mytxt,'String',r)
function myfigure_WindowButtonUpFcn(hObject, eventdata, handles)

% --- Executes when user attempts to close myfigure.
function myfigure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to myfigure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);

% --- Executes during object creation, after setting all properties.
function myaxes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to myaxes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate myaxes
axis([0 105 0 140]);

% --------------------------------------------------------------------
function file_Callback(hObject, eventdata, handles)
% hObject    handle to file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------
function f_open_Callback(hObject, eventdata, handles)
% hObject    handle to f_open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
[filename, pathname] = uigetfile( ...
{'*.bmp;*.jpg;*.png;*.jpeg', 'Image Files (*.bmp, *.jpg, *.png,*.jpeg)'; '*.*', 'All Files (*.*)'},'Pick an image');
if isequal(filename,0) || isequal(pathname,0),
return;
end

fpath=[pathname filename];
A=imread(fpath);%andles    structure with handles and user data (see GUIDATA)

r=imfinfo(fpath);
setappdata(handles.myfigure,'si',r);
set(handles.myfigure,'UserData',A);
f=image(A);
setappdata(handles.myfigure,'is_open',1);

% --------------------------------------------------------------------
function f_exit_Callback(hObject, eventdata, handles)
% hObject    handle to f_exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(findobj('Tag','myfigure'));%关闭主窗口
以上是主程序,其中函数datacon是数据库连接函数
%%%图片像素位置是通过首先取得鼠标在axes中的位置在取整处理后索引颜色的

%%%取点的语句 p = get(handles.myaxes,'currentpoint');


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐