How do you find the largest empty black rectangle coordinates, width and height #7127
Replies: 6 comments 3 replies
-
That is not a trivial problem. You can solve it relatively easily for the largest circle by finding the maximum brightness from distance morphology. See https://imagemagick.org/Usage/morphology/#distance A rectangle solution has been implemented in Python. See https://github.com/OpenStitching/lir |
Beta Was this translation helpful? Give feedback.
-
Morphology distance directly gives us the largest circle that is available at each pixel, so then we can simply find which pixel has the largest radius. For rectangles, the task is much harder. Each pixel can be the centre of multiple rectangles with different widths and heights, each of which is a maximum (it can't be extended in any direction). So at each pixel, we need to find which of those maximum rectangles has the maximum area. Then, we can find which pixel has the greatest maximum. Integral images can be used to find at each pixel, at each possible width, the tallest rectangle, and hence the largest area at that width. Explore the different widths to find the largest area of rectangle centred on that pixel. Explore all the pixels to find which has the largest area. Doing this in a script would be horribly slow. Some optimisations may be possible. For example, when we know the largest rectangle centred at a pixel, the largest rectangle centred at each of its is likely (but not certain) to have similar dimensions. The problem can be re-cast. First, generate versions of the input image at all reasonable aspect ratios. Then the task is to find the maximal square at each pixel, at each aspect ratio. The search space for that has only one dimension (the "radius" of the square), so searching is easier. Then we find, for each aspect ratio, which pixel has the largest square. Then we need to examine the solutions found for all the aspect ratios. See also https://en.wikipedia.org/wiki/Maximum_subarray_problem and https://theory.stanford.edu/~virgi/cs367/papers/maxsubarray.pdf . (A similar problem is to find the maximal ellipse, which has the same difficulties as the maximal rectangle.) |
Beta Was this translation helpful? Give feedback.
-
From say top 25 circles, get the square. Can we just expand the squares to a rectangles (both directions) to encompass empty space? And take the max area? That will work for all my images. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/gusenov/max-empty-rect-py Slow but worked good |
Beta Was this translation helpful? Give feedback.
-
I have written a Windows BAT script to find the maximal rectangle by exhaustive search using IM's For the first supplied image, I run this command:
The text result gives the details (width, height, coords of top-left, coords of bottom-right, etc) of the largest possible black rectangle:
The debugging On my laptop, that command took 41 minutes. Yikes. We can get a faster result that is probably not a global maximum like this:
The text output is:
That faster command took 29 seconds. The found rectangle is slightly smaller than the maximum. If that isn't fast enough, then we can use "100,100", which takes 5 seconds, making a rectangle area of 105791 pixels. |
Beta Was this translation helpful? Give feedback.
-
Yeah, it took a few mins. Most of the script is to process the input parameters. The important stuff is in the two In Windows BAT, environment variables work like this:
In bash:
Windows escape char is caret ^. Bash escape char is backslash \. In Windows BAT, If you need more help, just ask. If you get a bash version working, I encourage you to post it here. |
Beta Was this translation helpful? Give feedback.
-
Centos 8.0
Version: ImageMagick 7.1.1-6 Q16-HDRI x86_64 21020 https://imagemagick.org/
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): bzlib djvu fontconfig freetype jbig jng jp2 jpeg lzma pangocairo png tiff webp x xml zip zlib
Compiler: gcc (8.5)
I have a bunch of images. I want to know the largest empty black rectangle coordinates, width and height so that I can draw a rectangle and put text in them
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions