How can I set a callback for mouse clicks on an image?
조회 수: 40 (최근 30일)
이전 댓글 표시
I am struggling to understand how to set up callbacks for mouse actions on a displayed image. As an example, I've created a simple gui with GUIDE. The layout is a single figure with one axes and one button. I made two changes to the code generated by GUIDE.
- The button's ButtonDownFnc loads an image and displays it in the axes with image.
- The axes' ButtonDownFnc displays a string indicating it was called.
When I run it without loading the image, the axes callback is called, but after loading the image, it is no longer called. I cannot figure out how to get callbacks to work after displaying the image. Can someone help me understand what I'm missing?
Thanks
This is what the GUI looks like before and after loading the image:
This is the code:
function varargout = img_test(varargin)
% IMG_TEST MATLAB code for img_test.fig
% IMG_TEST, by itself, creates a new IMG_TEST or raises the existing
% singleton*.
%
% H = IMG_TEST returns the handle to a new IMG_TEST or the handle to
% the existing singleton*.
%
% IMG_TEST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMG_TEST.M with the given input arguments.
%
% IMG_TEST('Property','Value',...) creates a new IMG_TEST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before img_test_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to img_test_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 img_test
% Last Modified by GUIDE v2.5 06-Jun-2017 13:40:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @img_test_OpeningFcn, ...
'gui_OutputFcn', @img_test_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 img_test is made visible.
function img_test_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 img_test (see VARARGIN)
% Choose default command line output for img_test
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes img_test wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = img_test_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;
% --- 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)
disp('axes1_ButtonDownFnc');
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
A = imread('ngc6543a.jpg');
image(A);
댓글 수: 0
채택된 답변
Don Zheng
2017년 6월 9일
Replacing the following line
image(A);
with the following lines
im = image(A);
im.ButtonDownFcn = @axes1_ButtonDownFcn;
should make it work.
Reason is that the image is another graphic element sitting on top of the axis object. The mouse down is received by the image, not the axis. For your interests, if you axis is larger than the image, clicking in the white space outside the image but inside the axis should work with your original code.
댓글 수: 3
Scott Feltman
2020년 6월 3일
I have a follow-up question about this. I found this to be the solution for my own GUI issue, but now the problem is that the handles structure is not passed to the ButtonDownFcn. Do you know why the ImageButtonDownFcn doesn't have access to the handles?
Ugo Bruzadin Nunes
2022년 5월 29일
You may have to set your handles as visible Visible='on'. I've encoutnered this problem (no visible handles) and this may be your issue!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!