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

后端 未结 5 977
悲哀的现实
悲哀的现实 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: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.

提交回复
热议问题