How to crop an image?

2 views (last 30 days)
Xylo
Xylo on 28 Apr 2014
Commented: Image Analyst on 28 Apr 2014
Here I have some points, lets points=[x1,y1,x2,y2,x3,y3......xn,yn] these points are precalculated based on some mathematical calculation. Now I want to crop an image based on these points, i.e I want take the parts of the image thats are covered in these points. If any one have an idea help me! Thank you!

Answers (1)

Image Analyst
Image Analyst on 28 Apr 2014
What if they are not in a box? Images must be rectangular. If the x and y don't specify a rectangle, then you can use max(x), min(x), max(y), and min(y) to find the bounding box. Then use imcrop. If you want a polygon and not a rectangular crop, then you must mask instead of crop. You can use poly2mask() for that to get a binary image. Then multiply that by the image, or set the inverse of that in the image to zero (there's 2 ways).
out = in * uint8(binaryImage);
or
out = in;
out(binaryImage) = 0;
  2 Comments
Xylo
Xylo on 28 Apr 2014
Yes, your second guess is write. I am talking about polygonal shape, not rectangular. however your poly2mask() is working well. thank you sir! :)
Image Analyst
Image Analyst on 28 Apr 2014
Glad it worked. Can you please mark the Answer as Accepted then? You can actually do both if you want -- you can mask then crop to the bounding box. It just depends on what you want to do. For image segmentation, usually masking is sufficient and cropping is not necessary unless you want to magnify the region of interest in the display just for visual curiosity or inspection.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!