find largest rectangle not (necessary) aligned with image boundary in binary matrix

后端 未结 1 359
走了就别回头了
走了就别回头了 2021-02-06 18:23

I am using this solution to find rectangles aligned with the image border in a binary matrix. Suppose now I want to find a rectangle that is not aligned with the image border, a

1条回答
  •  滥情空心
    2021-02-06 19:05

    I only have a partial answer for this question, and only a few thoughts on complexity or speed for what I propose.

    Brute Force

    The first idea that I see is to use the fact that your problem is discrete to implement a rotation around the center of the image and repeat the algorithm you already use in order to find the axis aligned solution.

    This has the downside of checking a whole lot of candidate rotations. However, this check can be done in parallel since they are indepedant of one another. This is still probably very slow, although implementing it (shouldn't be too hard) and would provide a more definite answer to the question speed once parallelized.

    Note that your work-space being a discrete matrix, there is only a finite number of rotation to browse through.

    Other Approach

    The second solution I see is:

    1. To cut down your base matrix so as to separate the connected components [1] (corresponding to the value set you're interested in).
    2. For each one of those smaller matrices -- note that they may be overlapping depending on the distribution -- find the minimum oriented bounding box for the value set you're interested in.
    3. Still for each one of those, rotate your matrix so that the minimum oriented bounding box is now axis-aligned.
    4. Launch the algorithm you already have to find the maximum axis-aligned rectangle containing only values from your value set.
    5. The solution found by this algorithm would be the largest rectangle obtained from all the connected components.

    This second solution would probably give you an approximation of the soluiton, but I believe it might prove to be worth trying.

    For reference

    The only solutions that I have found for the problem of the maximum/largest empty rectangle are axis-aligned. I have seen many unanswered questions corresponding to the oriented version of this problem on 2D continuous space.

    EDIT:

    [1] Since what we want is to separate the connected component, if there is a degree of overlap, you should do as in the following example:

    0 1 0 0
    0 1 0 1
    0 0 0 1
    

    should be divided into:

    0 0 0 0
    0 0 0 1
    0 0 0 1
    

    and

    0 1 0 0
    0 1 0 0
    0 0 0 0
    

    Note that I kept the original dimensions of the matrix. I did that because I'm guessing from your post it has some importance and that a rectangle expanding further away from the boundaries would not be found as a solution (i.e. that we can't just assume there are zero values beyond the border).

    EDIT #2:

    The choice of whether or not to keep the matrix dimensions is debatable since it will not directly influence the algorithm.

    However, it is worth noting that if the matrices corresponding to connected components do not overlap on non-zero values, you may choose to store those matrices "in-place".

    You also need to consider the fact that if you wish to return as output the coordinates of the rectangle, creating a matrix with different dimensions for each connected component, this will force you to store the coordinates of your newly created matrix in the original one (actually, one point, say for instance the up-left one, should be enough).

    0 讨论(0)
提交回复
热议问题