且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS和Android算法或库羽化类似Photoshop的图像的边缘

更新时间:2023-02-17 18:50:42

假设你有alpha通道(像照片上用透明背景)似乎经常convul​​tion模糊矩阵应满足你。

Assuming that you have alpha channel (like on photo with transparent background) it seems that regular convultion blur matrix should satisfy you.

不过 - 你应该通过唯一的alpha通道

However instead of going through RGB channels - you should go through ALPHA channel only.

检查模糊滤镜的位置: https://en.wikipedia.org/wiki/Kernel_%28image_processing%29

Check the blur filter here: https://en.wikipedia.org/wiki/Kernel_%28image_processing%29

您有兴趣的盒子模糊/高斯模糊。然而,为了使这种效果更加流畅 - 你应该使用更大尺寸的基体

You are interested in box blur/gaussian blur. However to make this effect more smooth - you should use matrix of bigger size.

这算法将满足您的需求的原因是,如果所有周围的像素具有阿尔法0 - 这将是仍为0。如果255 - 它会留下255只是像素阿尔法0/255之间的边界区域将受到影响。

The reason that algorithm will satisfy your needs is that if all surrounding pixels have alpha 0 - it will be still 0. If 255 - it will stay 255. Just pixels in area of border between alpha 0/255 will be affected.

编辑:

请检查该拨弄铬(在FF这是很慢): http://jsfiddle.net/5L40ms65/

Please check this fiddle with chrome (in ff that's really slow): http://jsfiddle.net/5L40ms65/

您可以看看到算法中的code结束。由于实现我注意的是: - 无需模糊,如果所有neigbour像素是255或0(alpha通道) - 它需要也模糊的RGB在其他情况

You can take a look into algorithm in the end of code. Since implementation i noted that: - no need to blur if all neigbour pixels are 255 or 0 (alpha channel) - it is required to blur also RGB in other case

在一般的:

RADIUS = 2 (makes total width of matrix = 5)
For x = 0..width
  for y = 0..width
    if all pixels in square of radius 2 are alpha = 0
      do nothing
    elsif all pixels in square have alpha = 255
      do nothing
    else
      pixel[x][y].RGB = average RGB of adjacent pixels where alpha != 0
      pixel[x][y].ALPHA = average ALPHA in square

半径实例结果= 2

Example result with radius=2

当然,这是相当的概念方案,有很多的地方,记忆化和调整该脚本但它应该做一个大画面清晰

Of course this is rather concept program, there is a lot of place for memoization and tuning this script however it should make a big picture clear

iOS和Android算法或库羽化类似Photoshop的图像的边缘