How to display coordinates as rectangle is drawn over an image?
Show older comments
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
Accepted Answer
More Answers (1)
Tim Jackman
on 21 Sep 2018
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
Categories
Find more on Build Interactive Tools in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!