Largest inscribed rectangle, square or circle

The included functions will find the largest inscribed rectangle, square or circle of any orientation and in any arbitrary image.
487 Downloads
Updated 12 Oct 2020

View License

Included are also functions for parallel processing (not for circles) and GUI 'LRC_Demo.m' for easy use.

LARGEST RECTANGLE OR SQUARE
INPUT for LargestRectangle and LargestSquare:
The input has a minimum of one entry and maximum of 6 entries in following order:
1.) image: Image, RGB, grey or BW. By preference BW.
E.g.: image=imread(C:\MyImage.tif);
2.) RotationStep: Default: 5°
Rotation Step in degrees. In order to find tilted rectangles, the image is rotated.
Range: 0 < RotationStep <= 90.
If RotationStep>(LastAngle-FirstAngle) then the image is rotated once.
3.) iterate: Default: 1 (with iteration of rotation)
If 0 then no iteration, if 1 then iteration.
No iteration if (FirstAngle == LastAngle)
Iterate refines the rotation steps with the goal to find the largest possible rectangle,
In some cases, it might lock on another smaller local maximum. Avoided by smaller rotation steps.
4.) FirstAngle: Default: 0°, first angle for detection of rectangle with any orientation.
First angle: 0 <= FirstAngle < 90
5.) LastAngle: Default: 89.9999°, last angle for detection of rectangle with any orientation.
Last angle: FirstAngle <= LastAngle <= 90
6.) Graphic: Default: 1 (Plot graphic).
The included GUI 'LRC_Demo.m' simplifies those settings.

OUTPUT LRout:
1st row: Area of the largest rectangle in px, Rotation angle in degrees counterclockwise
2nd row: x,y of top corner of the largest rectangle
3rd row: x,y of right corner of the largest rectangle
4th row: x,y of bottom corner of the largest rectangle
5th row: x,y of left corner of the largest rectangle

EXAMPLES:
LRout=LargestRectangle(myImage) %Run with default values
LRout=LargestRectangle(myImage,0.1,0) %Run with small rotation steps, no iteration.
LRout=LargestRectangle(myImage,0,0,0,0) %For axis parallel rectangle
LRout=LargestRectangle(myImage,5,0,10,20,0),
Example for rotation step=5, no iteration, starting at 10°, ending at 20°, no graphic.

REMARK:
Any hole (black pixel), even only one pixel wide, will not be inside the largest rectangle

METHOD:
The image is rotated in order to detect arbitrary oriented rectangles.
Each right white pixel that is next to a left black pixel is searched for the maximum sized rectangle. If its size is larger than the previous largest rectangle, it is the new largest rectangle LR.
If all left border pixels are checked, the image is rotated by the rotation step given by the user.
Again all left border pixels are checked for the largest rectangle. After all rotations up to nearly 90° are done, the largest rectangle and its orientation is the result.
For more details see ReadMeLargestRectangle.pdf.

LARGEST CIRCLE
INPUT for LargestCircle:
The input has a minimum of one entry and maximum of 2 entries in following order:
1.) image: Image, RGB, grey or BW. By preference BW.
E.g.: image=imread(C:\MyImage.tif);
2.) Graphic: Default: 1 (Plot graphic).

OUTPUT LCout:
1st value: Area of the largest circle in px. If area=0, black image, no circle found
2nd value: radius in px
3rd and 4th value: x,y of the circle center. x: from left, y: from top of image

EXAMPLES:
LCout=LargestCircle(myImage)% Run and plot graphic
LCout=LargestCircle(myImage,0)% Run and do not plot graphic

METHOD:
Each white pixel is set as circle center. The shortest distance from this center to any white border pixel is the largest current radius.
The center with the largest radius is the result for the largest circle.
For more details see ReadMeLargestCircle.pdf.

Cite As

Peter Seibold (2020). Largest inscribed rectangle square or circle, MATLAB Central File Exchange.

MATLAB Release Compatibility
Created with R2016a
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!
Version Published Release Notes
2.0.1

Added 'circle' in title

2.0.0

Largest circle implemented

1.2.0

Removed failure with white pixel in the top row or left column of the image. (Thanks to Andrew B., who discovered the problem)

1.0.1

Changed description

1.0.0