Java 一种抗混叠灰度图像的泛光填充算法

Java 一种抗混叠灰度图像的泛光填充算法,java,image,bufferedimage,grayscale,flood-fill,Java,Image,Bufferedimage,Grayscale,Flood Fill,我需要用一些颜色来填充这张灰度图,例如红色。我想使用洪水填充算法,因为我需要在某些特定点进行填充 我找到了这个方法。但是结果有一些难看的白色部分,因为图片线条的抗锯齿 在我的代码中: 颜色目标颜色=白色, 颜色替换颜色=红色 我想我需要换成更多的灰色,而不仅仅是白色 我应该坚持这个方法并做一些改变吗?还是找点别的? 如果是,更改什么 我也尝试过这个链接,但不起作用: noblemaster.com/public/download/FloodFill.java.html 图片链接: 公共空白泛

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

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

在我的代码中: 颜色目标颜色=白色, 颜色替换颜色=红色

我想我需要换成更多的灰色,而不仅仅是白色

我应该坚持这个方法并做一些改变吗?还是找点别的? 如果是,更改什么

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

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

图片链接:

公共空白泛光填充(BuffereImage图像、点节点、颜色targetColor、颜色替换颜色){
int width=image.getWidth();
int height=image.getHeight();
int target=targetColor.getRGB();
int replacement=replacementColor.getRGB();
如果(目标!=更换){
Deque queue=new LinkedList();
做{
int x=node.x;
int y=node.y;
而(x>0&&image.getRGB(x-1,y)=目标){
x--;
}
布尔spanUp=false;
布尔值=假;
而(x0&&image.getRGB(x,y-1)==target){
添加(新点(x,y-1));
spanUp=true;
}else if(spanUp&&y>0&&image.getRGB(x,y-1)!=target){
spanUp=false;
}
如果(!spanDown&&y
这是我在结尾所做的,也是最后一张图片

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);
        }
      }
公共空白泛光填充(BuffereImage图像、点节点、颜色targetColor、颜色替换颜色){
int width=image.getWidth();
int height=image.getHeight();
int target=targetColor.getRGB();
int replacement=replacementColor.getRGB();
颜色的整数距离=320;
如果(目标!=更换){
Deque queue=new LinkedList();
做{
int x=node.x;
int y=node.y;
而(x>0&&colordance(image.getRGB(x-1,y),target)0&&colordance(image.getRGB(x,y-1,target)>distanceOfColor){
spanUp=false;
}
如果(!spanDown&&y<高度-1&&ColorDistance(image.getRGB(x,y+1),目标)颜色的距离){
spanDown=false;
}
x++;
}
}而((node=queue.pollFirst())!=null);
}
}

您可能不应该将其与“精确”的白色进行比较。试着用白色计算像素的距离,如果它小于某个X值,你也应该把它涂成红色。这可能会有帮助:好的,它可以工作,但我还是遗漏了一些东西。如果我想从白色变为透明。现在颜色距离无法计算,因为rgb不考虑alpha分量。但我想这是另一个问题。。
 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);
        }
      }