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

조회 수: 24 (최근 30일)
Jo
Jo 2015년 6월 25일
답변: Chi 2018년 7월 29일
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.
swathi
swathi 2018년 3월 25일
Sir I want to red mask the object to be removed in all frames.

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


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