how to detect mouse down and up coordinates on an image

6 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2014-6-8
Thomas, see my attached demos.

更多回答(1 个)

Sidney
Sidney 2015-6-14
编辑:Sidney 2015-6-14
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.

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by