简体   繁体   English

用于抗锯齿灰度图像的洪水填充算法 Java

[英]Flood Fill algorithm for an anti-aliasing greyscale image Java

I need to fill this greyscale picture with some color , for example red.我需要用某种颜色填充这张灰度图片,例如红色。 I thought to use Flood Fill algorithm because I need to fill at some certain points.我想使用 Flood Fill 算法,因为我需要在某些点进行填充。

I found this method.But the result has some ugly white parts, because of the anti aliasing of the picture's lines.我找到了这个方法。但结果有一些难看的白色部分,因为图片线条的抗锯齿。

in my code : Color targetColor= White, Color replacementColor=Red.在我的代码中:颜色目标颜色=白色,颜色替换颜色=红色。

I think I need to change the replacement into more grey colors, not only white.我想我需要将替换更改为更多的灰色,而不仅仅是白色。

Should I stick with this method and change a little?我应该坚持这个方法并稍微改变一下吗? or to find something else?或者找别的东西? If yes, what to change?如果是,要改变什么?

I did tried also this link ,but it doesn't work:我也试过这个链接,但它不起作用:

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

link for the images:图片链接:

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

This is what I did at the end and this is the final picture.这就是我最后所做的,是最终的照片。

distanceOfColor is a parameter, in my case a good number was in between 300-400 more than that it erased all the grey color anti-aliasing. 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);
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM