How to get the coordinate of cropped rectangular image?

2 views (last 30 days)
I cropped the image as a box. Then i need to use the coordinate of the cropped image for another task. How to get that coordinate? I am using this code btw from ImageAnalyst.
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units

Accepted Answer

Image Analyst
Image Analyst on 23 Jun 2021
The coordinates are x1,x2,y1, and y2. So to crop:
croppedImage = originalImage(y1:y2, x1:x2, :);
  5 Comments
Image Analyst
Image Analyst on 23 Jun 2021
imcrop() uses [xLeft, yTop, xWidth, yHeight] format, NOT (y1, y2, x1, x2) like you'd do if you were indexing. so
r = [x1, y1, (x2-x1), (y2-y1)];
croppedImage = imcrop(rgbImage, r);
Aminah Zhulaika
Aminah Zhulaika on 23 Jun 2021
Thank you very much! Now I get the answer and result already. Appreciate your help very much.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Jun 2021
I have manually cropped the area in image 1
Then I predict that the issue is that you do not know what coordinates you manually outlined. The way to deal with that is to ask for an additional output from imcrop()
[J1, rect] = imcrop(I1);
J2 = imcrop(I2, rect);

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!