how to detect mouse down and up coordinates on an image
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Thomas
am 13 Mär. 2014
Bearbeitet: Sidney
am 14 Jun. 2015
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 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Sidney
am 14 Jun. 2015
Bearbeitet: Sidney
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Blue finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!