抗锯齿灰度图像的整体填充算法Java



我需要用一些颜色填充这张灰度图片,例如红色。我想使用Flood fill算法,因为我需要在某些特定点进行填充。

我找到了这个方法。但由于图片线条的抗锯齿,结果会出现一些难看的白色部分。

在我的代码中:Color targetColor=白色,颜色替换颜色=红色。

我想我需要把替代品换成更多的灰色,而不仅仅是白色。

我应该坚持这种方法并稍作改变吗?还是去找别的?如果是,要更改什么?

我也尝试过这个链接,但它不起作用:

noblemaster.com/public/download/FloodFill.java.html

图像链接:

没有红色的图像

带有填充红色的图像

 public void floodFill(BufferedImage image, Point node, Color targetColor, Color replacementColor) {
        int width = image.getWidth();
        int height = image.getHeight();
        int target = targetColor.getRGB();
        int replacement = replacementColor.getRGB();
        if (target != replacement) {
          Deque<Point> queue = new LinkedList<Point>();
          do {
            int x = node.x;
            int y = node.y;
            while (x > 0 && image.getRGB(x - 1, y) == target) {
              x--;
            }
            boolean spanUp = false;
            boolean spanDown = false;
            while (x < width && image.getRGB(x, y) == target) {
              image.setRGB(x, y, replacement);
              if (!spanUp && y > 0 && image.getRGB(x, y - 1) == target) {
                queue.add(new Point(x, y - 1));
                spanUp = true;
              } else if (spanUp && y > 0 && image.getRGB(x, y - 1) != target) {
                spanUp = false;
              }
              if (!spanDown && y < height - 1 && image.getRGB(x, y + 1) == target) {
                queue.add(new Point(x, y + 1));
                spanDown = true;
              } else if (spanDown && y < height - 1 && image.getRGB(x, y + 1) != target) {
                spanDown = false;
              }
              x++;
            }
          } while ((node = queue.pollFirst()) != null);
        }
      }

这是我最后所做的,这是最后的图片。

distanceOfColor是一个参数,在我的情况下,一个好的数字在300-400之间,比它擦除所有灰色抗锯齿多。

 public void floodFill(BufferedImage image, Point node, Color targetColor, Color replacementColor) {
        int width = image.getWidth();
        int height = image.getHeight();
        int target = targetColor.getRGB();
        int replacement = replacementColor.getRGB();
        int distanceOfColor=320;
        if (target != replacement) {
          Deque<Point> queue = new LinkedList<Point>();
          do {
            int x = node.x;
            int y = node.y;
            while (x > 0 && ColorDistance(image.getRGB(x - 1, y), target)<=distanceOfColor) {
              x--;
            }
            boolean spanUp = false;
            boolean spanDown = false;
            while (x < width && ColorDistance(image.getRGB(x, y), target) <=distanceOfColor) {
              image.setRGB(x, y, replacement);
              if (!spanUp && y > 0 && image.getRGB(x, y - 1) == target) {
                queue.add(new Point(x, y - 1));
                spanUp = true;
              } else if (spanUp && y > 0 && ColorDistance(image.getRGB(x, y - 1), target) >distanceOfColor) {
                spanUp = false;
              }
              if (!spanDown && y < height - 1 && ColorDistance(image.getRGB(x, y + 1), target) <=distanceOfColor) {
                queue.add(new Point(x, y + 1));
                spanDown = true;
              } else if (spanDown && y < height - 1 && ColorDistance(image.getRGB(x, y + 1), target) >distanceOfColor) {
                spanDown = false;
              }
              x++;
            }
          } while ((node = queue.pollFirst()) != null);
        }
      }

最新更新