File Exchange

image thumbnail


version (520 KB) by Jaroslaw Tuszynski
Computer vision functions to locate largest square or rectangle inscribed inside arbitrary shape


Updated 06 Jan 2012

View Version History

View License

Inscribed_Rectangle package provides 2 low level computer vision / image analysis functions able to locate largest square or rectangle inscribed inside arbitrary shape defined by a binary mask (black and white image). Only rectangles with vertical/horizontal edges are considered. The functions proved can be used as tools for larger image segmentation problems

Cite As

Jaroslaw Tuszynski (2021). Inscribed_Rectangle (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

Andrew Poissant

The solution does not work well for pixilated binary images. Get some of the corners on the 0s of the binary image.

liyu yang

Tom Grieve

Agree with Zoé that this would be perfect if it could handle the special case of an entirely white image. Thanks for putting it together!

Kai Xu




Chirag S L

Chirag S L

This code is not working for finding maximum rectangle inside an airfoil shape.

Jaroslaw Tuszynski

Mustafa Khries, I am sorry but it sounds like you need to brush up on basic MATLAB skills. I am sure you can find websites that will be more helpful. You can also try to run and modify my tutorial.

Mustafa Khries

I'm trying to find the largest rectangle in an airfoil shape, I don't know how to get the image of the airfoil in matlab, and use the code for finding the rectangle. help please.


The code produces an incorrect result if the input is an entirely white image. Changing line 25 of FindLargestRectangles.m into "if max(I(:)) == 1" appears to solve this problem.

Peter Seibold


Jaroslaw Tuszynski

No, in the the problems I was working on the rectangles are always parallel to the edges.

Image Analyst

Can it find tilted rectangles, not aligned with the image edges?

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!