Crop image with a polygon

I'm new to matlab , i created a function that can detect an object in a picture , after this detection i want to corp the detected part (i drew a line around the detect area). i'm lost , i read some articles online which made a good use of the imcrop() function but i wasn't able to achieve my goal please help.
if true
%Get the bounding polygon of the reference image.
boxPolygon = [1, 1;... % top-left
size(boxImage, 2), 1;... % top-right
size(boxImage, 2), size(boxImage, 1);... % bottom-right
1, size(boxImage, 1);... % bottom-left
1, 1]; % top-left again to close the polygon
% Transform the polygon into the coordinate system of the target image. The transformed polygon indicates the location of the object in the scene.
newBoxPolygon = transformPointsForward(tform, boxPolygon);
%Display the detected object.
figure();
imshow(sceneImage);
hold on;
line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'R');
title('Detected Box');
end
This is my code i hope you direct me to the right track. Thank you

回答 (3 件)

norliana khamisan
norliana khamisan 2015 年 3 月 17 日

1 投票

hello Elie, did you get the answer how to crop the detected part which is the boxPolygon? actually, I face same problem.could you help me how to crop the detected part (boxPolygon only).I've do some coding to crop the boxPolygon but it was error..i don't know why..thank you in advance.

1 件のコメント

Image Analyst
Image Analyst 2015 年 5 月 18 日
You should probably start your own question in a new/different thread. Post your image and show the desired output image.

サインインしてコメントする。

Image Analyst
Image Analyst 2014 年 2 月 9 日

0 投票

I don't understand why you're doing. Why are you transforming points into a target image? What is the target image? If you just want to crops why don't you just call imcrop:
croppedImage = imcrop(yourImage, boxPolygon);

5 件のコメント

Elie
Elie 2014 年 2 月 9 日
my project is similar to this http://www.mathworks.com/help/vision/gs/object-detection-and-tracking.html#btt5qyu where i'm detecting an object , my objective is to crop the area that the polygon encloses as in the link with the staple remover box. i tried imageCropped=imcrop('my image',newBoxpolygon) but im getting an error i dont know why .please help
Image Analyst
Image Analyst 2014 年 2 月 10 日
I don't have that toolbox so I can't follow along with the demo. I think you can do it better than me because of that.
Ravi Singh
Ravi Singh 2019 年 10 月 17 日
Hi Image Analyst,
I am not able to crop like you have suggested above for polygon cropping cropping. Imcrop only allows 4 coordinates which is min X, min Y, Height , Width but i the shape is polygonal it wont fitt while doing it with imcrop. Is there any other method to do the same??
Image Analyst
Image Analyst 2019 年 10 月 17 日
The rectangle 1x4 vector is [minX, minY, width, height], which is not what you had. You can also do cropping with indexing to known rows and columns:
croppedImage = originalImage(row1:row2, column1:column2, :);
D_coder
D_coder 2020 年 3 月 21 日

サインインしてコメントする。

Mehdi Saberioon
Mehdi Saberioon 2015 年 3 月 20 日

0 投票

you can use
roipoly
for cropping image in polygon format

3 件のコメント

norliana khamisan
norliana khamisan 2015 年 3 月 26 日
means that, I need to replace function imcrop to roipoly?
I've try it,but still could not get the detected part(newBoxPolygon).
I change imcrop with roipoly like below:
bearing=roipoly(line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'r')); imshow(bearing);
dhia jamaa
dhia jamaa 2015 年 5 月 18 日
編集済み: dhia jamaa 2015 年 5 月 18 日
  • roipoly returns a binary image that you can use as a mask. so the result is a black and white image. the white region is the region that you selected. you can then replace the white region with the original photo.%%%%% matlab code
input = imread (I)
%coordinates of selected area
r=[40 204 267 102];
c=[136 125 210 219];
BW=roipoly(input,r,c);
%replacing white area with the desired image
out=zeros(size(BW,1),size(BW,2));
for i=1:size(BW,1)
for j=1:size(BW,2)
if BW(i,j)==1
out(i,j)=gray(i,j);
end
end
end
end
imshow(out)
i hope this is helpful
Image Analyst
Image Analyst 2015 年 5 月 18 日
Instead of that for loop, you could have simply done
out = gray .* uint8(BW);

サインインしてコメントする。

質問済み:

2014 年 2 月 9 日

コメント済み:

2020 年 3 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by