how to detect mouse down and up coordinates on an image
조회 수: 7 (최근 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
댓글 수: 0
채택된 답변
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Blue에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!