【问题标题】:Flood Fill algorithm for an anti-aliasing greyscale image Java用于抗锯齿灰度图像 Java 的 Flood Fill 算法
【发布时间】:2016-05-12 06:28:20
【问题描述】:

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

我找到了这个方法。但是结果有一些难看的白色部分,因为图片线条的抗锯齿。

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

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

我应该坚持这个方法并稍微改变一下吗?还是找别的东西? 如果是,要改变什么?

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

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

图片链接:

image without red color

image with filled red color

 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);
        }
      }

【问题讨论】:

  • 您可能不应该将它与“精确”的白色进行比较。尝试用白色计算像素的距离,如果它小于某个 X 值,你也应该把它涂成红色。
  • 好吧,它可以工作,但我仍然错过了一些东西。如果我想从白色变为透明。现在无法完成颜色距离,因为 rgb 没有考虑 alpha 分量。但我认为这是另一个问题..

标签: java image bufferedimage grayscale flood-fill


【解决方案1】:

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

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);
        }
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多