Matlab的GUI中figure的WindowButtonDownFcn与axes的ButtonDownFcn

我的程序任务很简单,就是当在一幅图像上单击的时候,实时显示当前鼠标单击点的坐标,并将当前单击点以红十字叉表示,同是再以此单击点为中心,构建一个绿色的矩形框,在功能上我都已经实现了,但现在却发现了一个问题,就是我发现figure的WindowButtonDownFcn与axes的ButtonDownFcn行为迥异,当我重载figure的WindowButtonDownFcn,可以正确的实现我的功能,但当我重载axes的ButtonDownFcn而将figure的WindowButtonDownFcn屏蔽掉的时候,程序却没有任何的反应,我检查了好多遍,都没有查出原因,渴望热心的朋友帮我看看,万分感谢,为了不至于让系统扣大家的M币,我把代码直接贴在下面了。
function varargout = MouseClickDrawFrame(varargin)
% MOUSECLICKDRAWFRAME M-file for MouseClickDrawFrame.fig
%      MOUSECLICKDRAWFRAME, by itself, creates a new MOUSECLICKDRAWFRAME or raises the existing
%      singleton*.
%
%      H = MOUSECLICKDRAWFRAME returns the handle to a new MOUSECLICKDRAWFRAME or the handle to
%      the existing singleton*.
%
%      MOUSECLICKDRAWFRAME('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOUSECLICKDRAWFRAME.M with the given input arguments.
%
%      MOUSECLICKDRAWFRAME('Property','Value',...) creates a new MOUSECLICKDRAWFRAME or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MouseClickDrawFrame_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MouseClickDrawFrame_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 MouseClickDrawFrame
% Last Modified by GUIDE v2.5 30-Dec-2009 20:32:58
% Author: ilovehust
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MouseClickDrawFrame_OpeningFcn, ...
                   'gui_OutputFcn',  @MouseClickDrawFrame_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 MouseClickDrawFrame is made visible.
function MouseClickDrawFrame_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 MouseClickDrawFrame (see VARARGIN)
% Choose default command line output for MouseClickDrawFrame
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MouseClickDrawFrame wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = MouseClickDrawFrame_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 edit_rowCoordinate_Callback(hObject, eventdata, handles)
% hObject    handle to edit_rowCoordinate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_rowCoordinate as text
%        str2double(get(hObject,'String')) returns contents of edit_rowCoordinate as a double

% --- Executes during object creation, after setting all properties.
function edit_rowCoordinate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_rowCoordinate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit_colCoordinate_Callback(hObject, eventdata, handles)
% hObject    handle to edit_colCoordinate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit_colCoordinate as text
%        str2double(get(hObject,'String')) returns contents of edit_colCoordinate as a double

% --- Executes during object creation, after setting all properties.
function edit_colCoordinate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_colCoordinate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in push_InputImage.
function push_InputImage_Callback(hObject, eventdata, handles)
% hObject    handle to push_InputImage (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global g_InputImage;
[FileName, PathName, FilterIndex] = uigetfile({'*.bmp;*.jpg;*.gif','(*.bmp;*.jpg;*.gif)';'*.bmp','(*.bmp)';'*.jpg','(*.jpg)';'*.gif','(*.gif)';},'打开图片');
if FilterIndex == 0
    return;
end
g_InputImage = imread(fullfile(PathName, FileName));
axes(handles.axes1);
imshow(g_InputImage, []);

% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global g_InputImage;
% axes(handles.axes1);
% imshow(g_InputImage, []);
% ClickPoint = get(gca,'currentpoint');
% col_coordinate = ClickPoint(1,1);
% row_coordinate = ClickPoint(1,2);
% CrossImage = DrawCross(g_InputImage, round(col_coordinate), round(row_coordinate), 5, 3, [255, 0, 0]);
% ImageFrame = DrawFrameByCenter(CrossImage, round(col_coordinate), round(row_coordinate), 50, 25, 3, [0, 255, 0]);
% axes(handles.axes1);
% imshow(CrossImage, []);
% set(handles.edit_rowCoordinate, 'String', num2str(round(row_coordinate)));
% set(handles.edit_colCoordinate, 'String', num2str(round(col_coordinate)));

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% 说明:当将figure1_WindowButtonDownFcn中的代码全部屏蔽掉而将axes1_ButtonDownFcn中的代码全部打开的时候,单击没有任何的反应。
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global g_InputImage;
ClickPoint = get(gca,'currentpoint');
col_coordinate = ClickPoint(1,1);
row_coordinate = ClickPoint(1,2);
CrossImage = DrawCross(g_InputImage, round(col_coordinate), round(row_coordinate), 5, 3, [255, 0, 0]);
ImageFrame = DrawFrameByCenter(CrossImage, round(col_coordinate), round(row_coordinate), 50, 25, 3, [0, 255, 0]);
axes(handles.axes1);
imshow(ImageFrame, []);
set(handles.edit_rowCoordinate, 'String', num2str(round(row_coordinate)));

set(handles.edit_colCoordinate, 'String', num2str(round(col_coordinate)));


1、figure的windowbuttondown函数是当鼠标在figure中点击时(无论是左键还是右键),程序调用的回调函数。这时,程序不管鼠标是在那一个控件的区域内,只要点击,都调用该函数;
2、axes或其它控件的ButtonDownFcn函数是当控件位于该控件的区域内时,点击时所调用的函数。matlab如何区分控件呢?是通过控件的句柄--handle来区别的。
    初次建立axes控件时,系统获得一个句柄,这个句柄是和该axes是对应的;当显示了图片以后,原先的axes改变了(包括axes轴的x、y轴的范围,比例等),再点击axes轴,这时是点击的图片,并不是原先的axes了,所以ButtonDownFcn当然不会被调用了,但windowbuttondown照样被调用。
3、解决的办法是:在用imshow或其它函数显示完图像时,用h=imshow等取得显示的图像的句柄,定义图像对象的ButtonDownFcn回调函数为自己定义的函数。


matlab GUI 中AXES显示图像后响应buttondownfcn的问题

一旦在这个 axe 上显示图像之后,那个 ButtonDownFcn 就没有反应了
也就是在这个 callback 函数中的任何代码都不执行了
我调试了一下,可以解决的。方法如下:
1 )在你的 GUI OpeningFcn 函数中加入如下代码:
         axes(handles.axes1);
         Image=imread('C:\Users\liuhuanjin\Pictures\5HPQ1IFA00AN0001.jpg'); %
读取图像
         hImage=image(Image); % 显示图像,同时取得图像的句柄( handle
         set(hImage,'ButtonDownFcn',@myCallback); %  设置图像对象的 ButtonDown 回调函数为自己定义的 myCallback

        handles.hImage=hImage;%
保存图像的句柄到 handles 结构,以备后用
        guidata(hObject,handles);

2 )在自己定义的 myCallback 函数中加入代码:
          function myCallback(hObject, eventdata, handles) % 自定义的 callback 函数
          ........% 在此加入你的代码

    因为图像显示后你点击的是图像,所以 axes 的那个 ButtonDownFcn 回调函数并没有调用,而是调用与图像对象相关的回调函数了。
应该是这样用 set(hImage,'ButtonDownFcn',{@myCallback,handles,k});

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值