How to crop an image?

1 vue (au cours des 30 derniers jours)
Xylo
Xylo le 28 Avr 2014
Commenté : Image Analyst le 28 Avr 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!

Réponses (1)

Image Analyst
Image Analyst le 28 Avr 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 commentaires
Xylo
Xylo le 28 Avr 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 le 28 Avr 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by