MATLAB Answers

Programmatically crop rough image to rectangle

14 views (last 30 days)
Samantha Sheppard
Samantha Sheppard on 28 Feb 2021
Answered: Image Analyst on 28 Feb 2021
Hello MathWorks community,
I could use your help programatically cropping a matrix / image I have. I would like to find the inner bounding box, such that the box only contains the image with no 'dark' regions.
I've tried using the regionprops function already (extrema and bounding box), however the result isn't exactly what I want.
frame = img>0;
extrema = regionprops(frame,'extrema');
box = regionprops(frame,'BoundingBox');
Extrema unreliably returns the bounding edges, and BoundingBox returnes the outer bonding box which includes 'dark' regions.
Any thoughts on how to tackle this problem would be greatly appreciated!

Answers (1)

Image Analyst
Image Analyst on 28 Feb 2021
There is no function to find the largest interior rectangle for arbitraily shaped blobs.
You can try scanning down with a line and note when the length of the non-zero line exceeds some length. Then do the same in the horizontal direction.
In genreal for some amoeba-shaped blob, you'd have to compute the Euclidean distance transform to find the point furthest away from any boundary. Then, for every point along the perimeter, take two perpendicular cross sections that run through the center to find the edges. Then construct a box from it with poly2mask() and see of any of the points in the rectangular mask you just constructed are black. If there are any black pixels in the box, then it's not 100% inside.
Submit your request to the Mathworks for afeature enhancement.

Community Treasure Hunt

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

Start Hunting!

Translated by