How to crop an image?
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!