How to find largest axis aligned rectangle inside a contour

asked 2015-06-05 15:24:10 -0500

MrVincenzo gravatar image

updated 2015-06-07 01:16:05 -0500


I want to find largest axis aligned rectangle inside a contour? I've found the contour (findContours) and approximated it (approxPolyDP). Now I need to find biggest axis aligned rectangle that fits inside the contour's approximated polygon.

UPDATE: The contour is approximately a rectangle. The problem I want to solve it to remove black areas around a stitched panoramic image.

I tried:

Approach #1 This approach fits my needs but I don't like the heuristic.

Approach #2 This approach assumes that the image has no black areas to the top and to the left of the image which is not acceptable in my case.

Any help will be appreciated. Thanks!

edit retag flag offensive close merge delete



This is a quite difficult task.

Could be a bit more specific? What do you want to achieve? Is your polygon convex (of even approximately a rectangle)? What is your needed accuracy?

FooBar gravatar imageFooBar ( 2015-06-05 16:42:22 -0500 )edit

@FooBar Thanks for the link. I updated the question.

MrVincenzo gravatar imageMrVincenzo ( 2015-06-05 23:59:42 -0500 )edit

@MrVincenzo what you do not like in the first approach?

theodore gravatar imagetheodore ( 2015-06-06 08:27:45 -0500 )edit

@theodore It is good only for some usecases and not for all. Consider the case where an image has a black area in it's interior near one of the boundaries. In this case, the largest rectangle will be reduced unnecessarily.

MrVincenzo gravatar imageMrVincenzo ( 2015-06-07 01:15:18 -0500 )edit