How to draw a ROI (rectangle) in an image and get the coordinates

75 次查看(过去 30 天)
Hi
I am trying to draw a rectangle in an image and get the coordinates in order to match them with the XYZ values. I tried some of the code I found here, along with some changes to implement that to my case, but I don't think it works well!
%Draw a ROI = rectangle
message = sprintf('Draw a box over the image.\nDouble click inside the box to finish drawing.');
uiwait(msgbox(message));
rect=imrect;
binaryImage = rect.createMask();
xy = rect.getPosition;
% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
%get the coordinates.. this is for freehand -> what do I have to change???
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
subplot(2, 3, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow; % Force it to draw immediately.
%match the coordinates (positions)with the XYZ values
for i=1:length(x)
X(i)=XYZ2(y(i),x(i),1);
Y(i)=XYZ2(y(i),x(i),2);
Z(i)=XYZ2(y(i),x(i),3);
end
Any ideas? Also, is it possible to use this specific ROI to a raw of images?
Many thanks
  1 个评论
Adam
Adam 2015-6-25
I'm not sure I fully understand what you are trying to do or are asking about.
rect.getPosition
will give you the position of the drawn rectangle.

请先登录,再进行评论。

回答(2 个)

Adam Hug
Adam Hug 2015-6-30
I believe you can simplify your code by using the "rbbox" command in MATLAB. See the following doc link for more information:
  3 个评论
Image Analyst
Image Analyst 2018-3-25
Once you know the top, bottom, left, and right columns from one image, it's easy to apply it to any other image. It just depends on what you want to do, like just plot the box in the overlay, or plot it with handles asking the user to confirm the position, or use it to crop the image, or use it to mask the image inside or outside the box, or whatever.

请先登录,再进行评论。


Chi
Chi 2018-7-29
Hope this FE file will help you.

Community Treasure Hunt

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

Start Hunting!

Translated by