我有很多不重叠的彩色矩形的图像。每个矩形都是一种独特的颜色,我提前知道颜色。(奇怪的情况,我知道。)我试图找到每个矩形的像素位置和大小,我需要尽可能快。我可以用 RMagick 或类似的库做任何有趣的技巧来使这比遍历每个像素更容易吗?
我目前的计划是这样的:
for each pixel (moving left-to-right, top-to-bottom):
if pixel color's in our list and we haven't seen it yet:
save pixel location as starting location for that color
else if pixel color's in our list and we've already seen it:
save pixel location as ending location for that color
(是的,如果我们知道它们在矩形中,我们可以优化和跳过某些像素区域。)在循环结束时,我们应该有每个矩形的第一个和最后一个像素,我们可以用它们来推断矩形方面。但这对我来说似乎有些难看。
我能做得更好吗?