Algorithm to split an image into smaller images reducing the amount of whitespace and specifying maximum amount of rectangles

后端 未结 5 976
悲哀的现实
悲哀的现实 2021-01-03 01:08

I am looking for an algorithm which can split an image into smaller images, with some constraints. One constraint is to use the least amount of \"whitespace\" meaning empty

相关标签:
5条回答
  • 2021-01-03 01:43

    Sorry for the late comment but it took me some time to find a "good" algorithm.

    After some research i am going for the following solution. First i use a Quadtree and do a SplitAndMerge. i Split on "Whitespace" first. Then i am merging all the rectangles together into the largest area rectangles.

    After that i sort the quadtree on area size, only keeping the largest x area's. (So essentialy keeping the largest whitespace areas). But i don't want the whitespace, i want everything except the whitespace so i invert the Quadtree, and do a SplitAndMerge Again. Then extracting the remaining rectangles out of the image, and binpacking them in the final image.

    This has given me some excellent results, reducing the image size drastically (because my images had a lot of whitespace in it), and keeping the time to draw them to a minimum.

    0 讨论(0)
  • 2021-01-03 01:47

    You want to write a run-lenght or a delta compression algorithm. Or you want to use a space-filing-curve or a spatial-index. A sfc recursively subdivide the surface into smaller 4 tiles and reduce the complexity of 2 dimension to 1 dimension thus it makes it easier to identify white-space. You want to look for Nick's hilbert-curve quadtree spatial index blog. You want to download my php class hilbert curve at phpclasses.org.

    0 讨论(0)
  • 2021-01-03 01:56

    I'd go with the same algorithm as ravloony, but with a slight and important modification, using a "crop" operation that looks for the minimal/maximal columns and rows that aren't completely empty and discarding the rest.

    In practice, the crop operation would get a X*Y region as input and would output 4 integers - the coordinates of the smallest rectangle that contains all the used pixels of the region. This can also be used to detect and discard empty regions.

    Example

    ....................
    .xxxxxxxxxxx........     xxxxxxxxxxx.......
    ...xxxx...xxxxxx....     ..xxxx...xxxxxx...
    .............xxxxx..     ............xxxxx.
    ...............xxx.. =>  ..............xxx. (first crop)
    ...............xxx..     ..............xxx.
    ....................     ..................
    ..xxxxxx............     .xxxxxx...........
    .....xxxxxxxxxxx....     ....xxxxxxxxxxx...
    .........xxxxxxxxxx.     ........xxxxxxxxxx
    ....................
    

    Now divide the image into NxN parts (using N=4 here) and use the crop operation on each of the parts:

    xxxxx|xxxxx|x....|
    ..xxx|x...x|xxxxx|
    ---------------------
         |     |  xxx|xx
         |     |  ..x|xx
    ---------------------
         |     |    x|xx
         |     |     |
    ---------------------
     xxxx|xx...|     |
     ...x|xxxxx|xxxxx|
         |...xx|xxxxx|xxx
    

    For this example, we get 10+10+10+6+4+1+2+8+15+10+3=79 pixels instead of 21*11=231 which is only 34,2%. Note that this happens to be the same amount as with your handcrafted 4-part segmentation (30+15+14+20=79)!

    Conclusions

    Of course there will be some additional data to keep track of the position and size of the 16 parts for each and it won't always give best results, but I think it's a nice compromise between speed and savings and the algorithm is easy to write and maintain.

    About the additional data: Images of size 1024x1024 and splitting into 4x4 parts would give you the possibility to use 4 byte values to store each rectangle, so additional data size would be only 16*4 = 64 bytes - regarding this, you should perhaps consider to increase your 16 part maximum unless it will slow down some other part like the drawing heavily.

    Worst cases

    Worst cases for this algorithm would be parts with some pixels at or near the edges set, like these:

    x......x    xxxxxxxx    xx......
    ........    ........    x.......
    ........    ........    ........
    x......x    ...x....    .......x
    

    Several solutions for these come to my mind:

    • Splitting the region again (ending up with a quadtree implementation)
    • Using some additional step to detect completely empty rectangles in the inside.
    • Translating the grid that defines the parts a bit
    0 讨论(0)
  • 2021-01-03 01:56

    My gut says that an ideal solution is akin to the knapsack problem and is thus computationally impractical. You may be able to use some sort of heuristic to generate a "good-enough" solution.

    You could use a flood-fill algorithm to select connected regions of non-transparent pixels. As a first cut, that would give you a rectangle for each disjoint area of color. If you have more rectangles available in your budget, you could try cutting them in different ways to see which gives you the highest "density" of colored pixels.

    0 讨论(0)
  • 2021-01-03 02:00

    I would look at doing it recursively, each time splitting in half or into four, until you get to the level you want (for you 2 -> 4^2 = 16). At the bottom level check for empty squares and discard them. Of course this gives you a grid of rectangles proportional to the shape of the original image, rather than optimally placed rectangles, but it might start you off on the right track.

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