how to detect mouse down and up coordinates on an image
3 views (last 30 days)
Show older comments
Hello,
I am trying to write function that supports boxing area of image with mouse. When i click on an image hold the click, move cursor and release the click i want my function to detect pixel coordinates of 2 points
a) where mouse button was pressed and
b) where mouse button was released
The first part works well with the below code
function cutByHand(img_str)
imObj = imread(img_str);
figure
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickDown);
function ImageClickDown ( objectHandle , eventData )
axesHandles = get(objectHandle,'Parent');
coordinate = get(axesHandles,'CurrentPoint');
(...)
My problem is with geting the point where mouse was released since neither of below
set(h,'KeyReleaseFcn',@ImageClickUp);
set(h, 'WindowButtonUpFcn',@ImageClickUp);
can work with handle h being of type image. The closest i could get is with
set(gcf, 'WindowButtonUpFcn',@ImageClickUp);
function ImageClickUp ( objectHandle , eventData )
coordinates = get(objectHandle,'CurrentPoint');
(...)
but this only gives the figure coordinates and not the pixel coordinates of an image. Do you know any solution or workaround that would give pixel coordinates of the mouse up location?
in case the problem is somewhere else in the code (i doubt it) below is the full code of my function
function cutByHand(img_str)
imObj = imread(img_str);
figure%('KeyReleaseFcn', @ImageClickUp); %this doesnt trigger at all
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickDown);
%set(h,'KeyReleaseFcn',@ImageClickUp);
set(gcf, 'WindowButtonUpFcn',@ImageClickUp);
function ImageClickDown ( objectHandle , eventData )
axesHandles = get(objectHandle,'Parent');
coordinate = get(axesHandles,'CurrentPoint');
coordinate = coordinate(1,1:2);
setappdata(gcf,'down', coordinate);
end
function ImageClickUp ( objectHandle , eventData )
%axesHandle = get(objectHandle,'Parent');
coordinates = get(objectHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
down=getappdata(gcf,'down');
up=coordinates;
%do stuff with up and down
end
end
0 Comments
Accepted Answer
More Answers (1)
Sidney
on 14 Jun 2015
Edited: Sidney
on 14 Jun 2015
Hi Thomas:
I think you just need to get the axesHandle in your ImageClickUp:
function ImageClickUp ( objectHandle , eventData )
axesHandle = get(objectHandle,'CurrentAxes');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
...
end
I've tried the code and it works for me.
0 Comments
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!