How to display coordinates as rectangle is drawn over an image?

조회 수: 5 (최근 30일)
KAE
KAE 2018년 6월 28일
답변: Tim Jackman 2018년 9월 21일
In a GUI, I have an image and the user selects a rectangle using getrect that is later used to crop the image. As the user is selecting the rectangle, I would like to display the rectangle coordinates in real time so that the user can manually seek to match desired coordinates, if they want. (I need to keep the manual aspect of getrect because sometimes there aren't any desired coordinates). Is a popup msgbox the best way to do this, or something else?
Let's say in the following code, the user wants to choose a rectangle with coordinates [100 200 100 100]. How can they get feedback on the coordinates as they manually draw the rectangle?
imshow('moon.tif')
rect = getrect

채택된 답변

Madeline Gardner
Madeline Gardner 2018년 6월 28일
Hello!
One way that you could do this is by using imrect instead of getrect. getrect only returns the coordinates of the rectangle when the user is done placing it, but imrect allows the user to resize the rectangle and can return the current coordinates at any time.
So, for example, using code from the imrect documentation, you could display the current coordinates of the rectangle in the title of the figure (this version is slightly simplified, the one in the doc is a bit more complex):
imshow('moon.tif')
rect = imrect;
addNewPositionCallback(rect, @(p) title(mat2str(p,3)));
It isn't the prettiest way to display the coordinates, but you could modify the callback function to display them in whatever way you want!
Hope that helps!
  댓글 수: 2
KAE
KAE 2018년 6월 28일
Thanks so much! (In case anyone is trying this: You have to move the rectangle for the coordinates to start appearing in the title).
Madeline Gardner
Madeline Gardner 2018년 6월 28일
No problem! I'm glad it worked :) Sorry if the coordinates not appearing at first was confusing!

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

추가 답변 (1개)

Tim Jackman
Tim Jackman 2018년 9월 21일
Beginning in 18b, this is doable with the new Rectangle ROI:
https://www.mathworks.com/help/images/roi-based-processing.html
Here is an example of how to begin drawing on the current axes and display the rectangle's position as you move the ROI around.
function displayCoordinates
% Create ROI class
h = images.roi.Rectangle();
% Add listener for ROI movement
addlistener(h,'MovingROI',@movingCallback);
% Begin drawing on current axes
h.draw;
end
function movingCallback(src,evt)
src.Label = mat2str(evt.CurrentPosition,3);
end

카테고리

Help CenterFile Exchange에서 Build Interactive Tools에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by