洪水填充算法是否考虑了 alpha 且不会在抗锯齿线周围留下边缘?
我已经实现了一个典型的洪水填充算法,当使用纯色时,它按预期工作,我使用 ARGB 分量之间的欧几里得距离来比较颜色。
我的问题是,如果您在透明背景上绘制类似抗锯齿红线的内容,我的洪水填充算法将不会填充大多数半透明像素,从而在对象周围留下条纹。具体来说,对于此示例,线条的中心为纯红色(即 ARGB 格式中的 (255, 255, 0, 0)),线条的边缘也是纯红色,但这些像素的 alpha 范围低至 1% alpha (即(1, 255, 0, 0))。一般来说,我希望能够绘制一个抗锯齿的圆形轮廓,在中心进行洪水填充,并且不让它留下边缘。
我应该使用什么函数来比较颜色和/或如何调整洪水填充算法,以便在对象周围不会留下此类条纹?
我尝试告诉算法,如果 alpha 值小于 90%,则始终填充像素,这有时看起来不错,但在填充微弱线条时过于激进。
编辑:为了详细说明它的外观,其中 0 - 5 代表红色像素 a 0、20、40、60、80 和 100% alpha,抗锯齿红线的图像可能如下所示:
0 0 0 0 0 0 0
0 0 0 3 0 0 0
0 0 3 5 3 0 0
0 0 3 5 3 0 0
0 0 3 5 3 0 0
0 0 0 3 0 0 0 0
0 0 0 0 0 0
我当前的洪水填充算法在左上角填充红色将用 5 替换所有 0。剩下的 3 形成一个难看的样子晕绕线。
I've implemented a typical flood fill algorithm and it works as expected when solid colors are used where I use the Euclidean distance between the ARGB components to compare colors.
My problem is that if you draw something like an anti-aliased red line on a transparent background my flood fill algorithm will not fill over most semi-transparent pixels, leaving fringes around objects. Specifically for this example, the center of the line is solid red (i.e. (255, 255, 0, 0) in ARGB format) and the fringes of the line are also solid red but the alpha of these pixels ranges down to 1% alpha (i.e. (1, 255, 0, 0)). Generally, I want to be able to draw, say, a anti-aliased circle outline, flood fill in the center and not have it leave fringes.
What function do I use to compare colors and/or how do I adapt the flood fill algorithm so such fringes are not left around objects?
I've tried telling the algorithm to always fill over a pixel if it's alpha is <90% and this sometimes looks OK but it's too aggressive at filling over faint lines.
Edit: To elaborate what this looks like where 0 - 5 represents a red pixels a 0, 20, 40, 60, 80 and 100% alpha, an image of an anti aliased red line might look like this:
0 0 0 0 0 0 0
0 0 0 3 0 0 0
0 0 3 5 3 0 0
0 0 3 5 3 0 0
0 0 3 5 3 0 0
0 0 0 3 0 0 0
0 0 0 0 0 0 0
My current flood fill algorithm filling red in the top-left corner will replace all the 0s with a 5. The remaining 3s form an ugly looking halo around the line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的洪水填充应替换颜色的 RGB 部分,但保留原始 Alpha。这样您就可以在不改变混合比例的情况下获得新的颜色。
(如果您的位图使用预乘 Alpha,那么您应该按现有 Alpha 缩放替换 RGB,但在您的示例中,您没有使用预乘 Alpha。)
Your flood fill should replace the RGB portions of the colors, but leave the original alpha. That way you get the new color without changing the blending ratio.
(If your bitmap uses pre-multiplied alpha, then you should scale the replacement RGB by the existing alpha, but in your examples, you aren't using pre-multiplied alphas.)