How to crop an image?

3 Ansichten (letzte 30 Tage)
Xylo
Xylo am 28 Apr. 2014
Kommentiert: Image Analyst am 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!

Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Xylo
Xylo am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by