MATLAB: How to detect mouse down and up coordinates on an image

Image Processing Toolboxmouse click release buttonup buttondown fcn image gui

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

Best Answer

Thomas, see my attached demos.
Related Question