1

I want to find the largest inscribed square in single color image, which could be consists of discontinuous same color areas, for example below:

continues same color

and then I have to find the largest inscribed blue square area

enter image description here

discontinues same color

for this discontinues same color i must find the largest inscribed blue square area

enter image description here

now the current solution is "Maximum size square sub-matrix with all 1s"

First convert the picture to grayscale,and then convert matrix with 0 and 1, and calculate the largest square sub-matrix with all 1s,so we get the square region [left,top,size,size],for example, the first pink color photo 200x200, and then we get the blue square area [50,80,110,110]

as you can see, we can get the pixel range of largest inscribed square, But we get integer units. If the picture is small, we still get integer pixels, and the accuracy is definitely not enough, I want to know if there is a way to figure out the float point area,for example

Suppose the picture size is 10x10

enter image description here

and then we can calculate the largest inscribed square below [2.56, 2.78, 3.25,3.25]:

enter image description here

if the picture is smaller, the float area is very useful,for example, if the inscribed square only have two unit, and the left and top begin is not start with integer, so we lost one unit accurate. if i want to put some text into the inscribed square, for example:

enter image description here

and then the position of the text which is locate in inscribed square is not precise enough!

I have been struggling with this issue for months,I tried opencv but there is no solution or function to meet my requirements And I know there must be good solution to solve this problem,but I have no idea! I don't know how to solve it, please help me, appreciate any prompt or tips

Also, I would like to add more information about my problem:

<svg id="_0110068" data-name="0110068" xmlns="http://www.w3.org/2000/svg" width="500" height="500" viewBox="0 0 500 500">
   <path id="439.97_240.36_3.84_3.84" d="M435.9652,238.4672c.262,5.3118,7.7188,9.656,13.8857,8.1239C446.2334,242.0454,442.6965,237.6206,435.9652,238.4672Z" style="fill: #80d1c0"/>
</svg>

you can copy the svg and put it into this website and press Draw then You can see a green block in the lower right corner

as you can seen, the

d = "M435.9652...Z" can be convert to picture, and the rect="439.97_240.36_3.84_3.84" is the largest inscribed square, that means left = 439.97, top = 240.36 , right = left + 3.84, bottom = top + 3.84, so the size is 3.84 of the largest inscribed square, you can't omitted 0.84 that is very important! but how can i get the float point area

banks
  • 35
  • 7
  • 1
    Does this answer your question? [Find largest rectangle containing only zeros in an N×N binary matrix](https://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-n%c3%97n-binary-matrix) – Miki Dec 16 '19 at 07:30
  • 1
    As indexing works in integers, your source image would need to have a sufficiently large resolution so that the start and stop points are located at different pixels. If your source image is so small that these points land in the same pixel, one option could be to increase the resolution of the image by interpolation via 'imresize' for example. – mkfin Dec 16 '19 at 07:48
  • @Miki I know what you mean, no matter ["Maximum size square sub-matrix with all 1s"](https://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/) or [Find largest rectangle containing ....](https://stackoverflow.com/questions/2478447/find-largest-rectangle-containing-only-zeros-in-an-n%c3%97n-binary-matrix) you mentioned, I can only get the area of integer units, but i want to get the area of float units – banks Dec 16 '19 at 08:01
  • 1
    your 0s and 1s will be at integer coordinates anyway. So just upscale your image – Miki Dec 16 '19 at 08:14
  • @Miki I have added more information Thanks! – banks Dec 16 '19 at 08:44
  • 2
    if you want precision up to the second digit, just scale up by a factor of 100. Or, if you just want the box for the text position, take a rectangle 1px smaller. Or you can try with `getRectSubPix` somehow... – Miki Dec 16 '19 at 09:20
  • @Miki Thanks a lot, I understand what you mean. – banks Dec 16 '19 at 09:50

0 Answers0