get a cropped image using mouse click

Hello,
I want to crop an image using coordinates of mouse click. I find a solution with handler function, but the problem is that I can't get results from the function. I used the following code:
function demoOnImageClick (I)
clc;clear;
imObj = imread(I);
disp(size(imObj));
figure;
hAxes = axes();
imageHandle = imshow(imObj);
set(imageHandle,'ButtonDownFcn',@ImageClickCallback);
function coordinates = ImageClickCallback ( objectHandle , eventData )
axesHandle = get(objectHandle,'Parent');
coordinates = get(axesHandle,'CurrentPoint');
coordinates = coordinates(1,1:2);
end
end
Using this code, I can't get coordinates at each click to use them in the imcrop function. What can I do?

댓글 수: 1

Guillaume
Guillaume 2015년 7월 8일
Note there is absolutely no point in having clear at the start of a function.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2015년 7월 8일

3 개 추천

The simplest way to get mouse coordinates on your image is to use ginput:
imshow(img);
[columns, rows] = ginput;
But also note that if you call imcrop without specifying the cropping coordinates you get an interactive tool anyway, so I would just do:
[croppedimage, rect] = imcrop(img);

카테고리

도움말 센터File Exchange에서 Data Exploration에 대해 자세히 알아보기

질문:

2015년 7월 8일

댓글:

2015년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by