Getting no results from a mouse click when using ButtonDownFcn with imshow

I'm unable to obtain (x,y) coordinates of an image using a mouse click with "imshow". I can do it with "image" but not with "imshow", and I need "imshow". I've seen lots of other posts on this issue, but none of their solutions seem to work, or the discussion is too technical for me.
I have 4 images arranged two by two, and I'm interested in clicking only on the image in subplot (2,2,3). But when I click nothing happens. Apparantly my function "clicky4" is not being called. Here's the relevant section of code:
global x y
.........
Image = single(rgb2gray(FilterRGBImage));
subplot(2, 2, 3);
vv = imshow(Image,'InitialMagnification', 'Fit');
set(vv, 'HitTest','off','PickableParts','none');
% Try to use the mouse to select a pixel
set(gca,'ButtonDownFcn', @clicky4)
And here is my function "clicky4":
function clicky4(gcbo,eventdata,handles)
%This function is supposed to grab the position (x,y) of the mouse upon a click
global x y
v = get(gca,'Currentpoint');
hold on
x = v(1,1);
y = v(1,2);
end
Any suggestions will be much appreciated. I'm a relative beginner at this stuff....

Réponses (1)

Would ginput(1) or impixelinfo help you?

8 commentaires

jack feinberg
jack feinberg le 16 Mar 2019
Modifié(e) : jack feinberg le 16 Mar 2019
Thank you for your suggestion.
ginput(1) pauses execution until a button is pressed. Unfortunately, I want to watch the images be rapidly displayed (they are frames of a movie), and then click on a spot in a particular frame. Using ginput(1) will just make the first frame of the movie pause until I click. So I don't see how I can make ginput(1) work.
impixelinfo displays the [x,y] coordinates of the mouse if my window doesn't fill up the screeen, but if my window does fill up the screen then that display is gone. Also, as mentioned above, I want to click on a particular frame of the movie and then have that [x,y] remembered for all future frames, irrespective of how I move the mouse after the click.
Perhaps Walter Roberson, the grand wizard of such dilemnas, can weigh in on this probelm?
Try using image() instead of imshow() as imshow() tends to clear the axes.
Thanks for your reply. As mentioned in the preamble to my question, using image() does not seem to work. Using image() all I get displayed is a purple rectangle instead of my image, nor can I set the 'InitialMagnification' to 'Fit' when using image(). There must be a way to solve this problem using imshow(). I think the problem has to do with the statements
vv = imshow(Image,'InitialMagnification', 'Fit');
set(vv, 'HitTest','off','PickableParts','none');
but I've tried lots of different values for HitTest and PickableParts, and none seem to work.
colormap(gray) after you image()
Are you wanting initial magnification to be 'fit' because your array is larger or smaller than the axes area?
1) OK. We are getting there. Colormap(gray) helped. The purple is gone.
But the program still doesn't pay attention to my mouse clicks. It won't call my function "clicky4". (See above.) Instead I get the error message:
"Undefined function 'clicky4' for input arguments of type 'matlab.graphics.axis.Axes'.
Error while evaluating Axes ButtonDownFcn."
2) And yes, I've shrunk (blurred, actually) my original image by a known amount so that my array is now smaller, but for display I want it to enlarge to fill the axes area.
Is clicky4 stored in the same .m file as the other code, or in a separate clicky4.m file? Is it possibly defined as a nested function?
Yup. That was the problem; my @function was stored inthe wrong location. A beginner's mistake! Thanks. It all works now!
@jack feinberg: Please use flags only to inform admins and editors about contents, which might conflict with the terms of use, but not to post a comment. Thanks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Produits

Version

R2017a

Commenté :

Jan
le 19 Mar 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by