Problems reading points from image loaded into guide GUI
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lyn
el 28 de Dic. de 2014
Comentada: Image Analyst
el 4 de En. de 2022
Hi,
I made a GUI to display an image and retrieve current point when clicked. I did this in 3 slightly different ways, but don't know why they work/don't work in each case. Could someone help explain why..?
Case 1: 'pts' is displayed in command window
function varargout = Atest2(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Atest2_OpeningFcn, ...
'gui_OutputFcn', @Atest2_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
function Atest2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Atest2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function axes1_CreateFcn(hObject, eventdata, handles)
Im = imagesc(imread('test.jpg'));
set(Im,'HitTest','Off')
set(hObject,'ButtonDownFcn',@axes1_ButtonDownFcn)
function axes1_ButtonDownFcn(hObject, eventdata, handles)
pts = hObject.CurrentPoint
Case 2: 'pts' is NOT displayed in command window (only difference: imshow instead of imagesc /imread)
function varargout = Atest2(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Atest2_OpeningFcn, ...
'gui_OutputFcn', @Atest2_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
function Atest2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Atest2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function axes1_CreateFcn(hObject, eventdata, handles)
Im = imshow('test.jpg');
set(Im,'HitTest','Off')
set(hObject,'ButtonDownFcn',@axes1_ButtonDownFcn)
function axes1_ButtonDownFcn(hObject, eventdata, handles)
pts = hObject.CurrentPoint
Case 3: Code in 'createfcn' moved to 'openingfcn'. Pts displays in command window, but now with only 2 arguments instead of 6.
function varargout = Atest2(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Atest2_OpeningFcn, ...
'gui_OutputFcn', @Atest2_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
function Atest2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
Im = imshow('test.jpg');
set(Im,'HitTest','Off')
set(hObject,'ButtonDownFcn',@axes1_ButtonDownFcn)
function varargout = Atest2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function axes1_CreateFcn(hObject, eventdata, handles)
function axes1_ButtonDownFcn(hObject, eventdata, handles)
pts = hObject.CurrentPoint
It seems almost as if there's a bug in my software..? (I'm using a free trial of Matlab 2014b (8.4.0.150421), on MacBook Air, OS X Yosemite 10.10.1.)
0 comentarios
Respuesta aceptada
Geoff Hayes
el 28 de Dic. de 2014
Lyn - for your third case,
function Atest2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
Im = imshow('test.jpg');
set(Im,'HitTest','Off')
set(hObject,'ButtonDownFcn',@axes1_ButtonDownFcn)
hObject is the figure and not the axes, so that will (probably) explain why you are observing only an (x,y) coordinate rather than the two (x,y,z) coordinates. If you want this code to be similar to the other cases, then you would have to supply the handle to the axes like
set(handles.axes1,'ButtonDownFcn',@axes1_ButtonDownFcn)
As for the other cases behaving differently, try running the following line of code after each call to set the hit test value
set(Im,'HitTest','Off')
get(Im,'type')
For the use of imagesc, the type should be image. What is the type for Im when imshow is used? It may be that you are disabling the hit test for something other than the image (the axes perhaps?).
2 comentarios
Geoff Hayes
el 1 de En. de 2015
Lyn - glad that the code is working. Though I am not sure about using
set(handles.axes1,'ButtonDownFcn',@axes1_ButtonDownFcn)
in case 2 because I would have thought that the handles structure is empty when the axes1_CreateFcn function is called. (At least it seems to be on my version of MATLAB, R2014a.)
Más respuestas (1)
Image Analyst
el 1 de En. de 2015
Why not just use impixelinfo() to have a live real-time readout of location and pixel value as the user mouses around over your image?
3 comentarios
Roger Breton
el 4 de En. de 2022
I'm trying to "improve" the impixelinfo() function as I would like, not only to retrieve the pixel RGB data but also the calculated CIE Lab values.
So far, I'm using this function to retrieve the x,y coordinates :
function mouseMove (object, eventdata)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
end
I don't think I could use impixelinfo() to retrieve the RGB values based on the x,y coordinates returned by CurrentPoint? But it would greatly simplify my code if it could work that way...
Ver también
Categorías
Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!