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

73 views (last 30 days)
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 Comment
Adam
Adam on 25 Jun 2015
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.

Sign in to comment.

Answers (2)

Adam Hug
Adam Hug on 30 Jun 2015
I believe you can simplify your code by using the "rbbox" command in MATLAB. See the following doc link for more information:
  3 Comments
Image Analyst
Image Analyst on 25 Mar 2018
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.
swathi
swathi on 25 Mar 2018
Sir I want to red mask the object to be removed in all frames.

Sign in to comment.


Chi
Chi on 29 Jul 2018
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!