【问题标题】:How to remove white background of an image - java如何去除图像的白色背景 - java
【发布时间】:2020-03-02 16:34:36
【问题描述】:

我想删除一张图片的白色背景并将其另存为另一张图片。 我编写了一个提取背景的代码,但它留下了一些像素值。 查看原图:

结帐裁剪图像:

它仍然留下一些白色背景。

我也想删除它。

这是我的代码:

       int x1=0;
    int y1=0;
    boolean res = false;
    System.out.println("in formatImage");

//宽度去除...

  for (int x = 0; x <= w-1; x++) {
            for (int y = 0; y <= h-1; y++) {
                if(new Color(bi1.getRGB(x, y)).getRGB()==-1)
                {res=false;}
                else if (!new Color(bi1.getRGB(x, y)).equals(Color.WHITE)) {
                    res = true;
                }
                if (res) {
                    for (int p = y; p <= h-1; p++) {
                        b21.setRGB(x1,p,new Color(bi1.getRGB(x, p)).getRGB());                        
                    }
                    x1++;
                    res = false;
                    break;
                }
            }
        }
        b21=new BufferedImage(x1,h,BufferedImage.TYPE_INT_RGB);
        x1=0;
        for (int x = 0; x <= w-1; x++) {
            for (int y = 0; y <= h-1; y++) {
                if(new Color(bi1.getRGB(x, y)).getRGB()==-1)
                {res=false;}
                else if (!new Color(bi1.getRGB(x, y)).equals(Color.WHITE)) {
                    res = true;
                }
                if (res) {
                    for (int p = 0; p <= h-1; p++) {
                        b21.setRGB(x1,p,new Color(bi1.getRGB(x, p)).getRGB());
                    }
                    x1++;
                    res = false;
                    break;
                }
            }
        }

//高度去除

for (int y = 0; y <= h-1; y++) {
    System.out.println("Y = "+y);
    for (int x = 0; x <= x1-1; x++) {
        System.out.println("("+x+","+y+") : "+b21.getRGB(x, y));
        if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE)) {
            res = true;
        }
        if (res) {
            for (int p = 0; p <= x1-1; p++) {
               b31.setRGB(p,y1,new Color(b21.getRGB(p, y)).getRGB());

            }
            y1++;
            res = false;
            break;
        }
    }
}
b31=new BufferedImage(x1,y1,BufferedImage.TYPE_INT_RGB);
int ty=y1;
y1=0;
for (int y = 0; y <= h-1; y++) {
    for (int x = 0; x <= x1-1; x++) {
        if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE)) {
            res = true;
        }
        if (res) {
            for (int p = 0; p <= x1-1; p++) {
               b31.setRGB(p,y1,new Color(b21.getRGB(p, y)).getRGB());
            }
            y1++;
            res = false;
            break;
        }
    }
}

b31 有最终图像。

【问题讨论】:

  • “去除白色背景”是什么意思?您真的在问如何crop 图像吗?这似乎是你的代码所做的。什么不工作?白色背景的哪一部分没有被删除?您的问题不清楚。
  • 手边的背景没有被移除。你可以通过下载图片来检查

标签: java image


【解决方案1】:

如果您使用任何像样的图像编辑器检查图像,您会发现靠近模型头部、左手和右肘的像素不是纯白色 (0xFFFFFF)。

您需要调整算法,以允许所有 3 个通道的全强度略有偏差。允许多少余地由您决定。

【讨论】:

    【解决方案2】:

    正如吉姆所说, 靠近身体的颜色不是纯白色。 因此,您修改了代码的以下语句,它将非常适合您。 替换以下命令行

    if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE)) 
    

    通过

     if (new Color(b21.getRGB(x, y)).getRGB()<-1000000) 
    

    这将为您提供所需的输出。 您可以在 -1000000 到 -2000000 之间改变白色和灰色的色调

    【讨论】:

      【解决方案3】:

      我确实创建了一个将白色背景转换为透明的函数

          public Image makeWhiteBGToTransparent(final BufferedImage im) {
          final ImageFilter filter = new RGBImageFilter() {
              public int markerRGB = 0xFFFFFFFF;
      
              public final int filterRGB(final int x, final int y, final int rgb) {
                  if ((rgb | 0xFF000000) == markerRGB) {
                      return 0x00FFFFFF & rgb;
                  } else {
                      return rgb;
                  }
              }
          };
      
          final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
          return Toolkit.getDefaultToolkit().createImage(ip);
      }
      

      现在,如果我们想将此Image 转换为BufferedImage,请使用此函数

      public BufferedImage toBufferedImage(Image img) {
          if (img instanceof BufferedImage) {
              return (BufferedImage) img;
          }
      
          BufferedImage bImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                  BufferedImage.TYPE_INT_ARGB);
          Graphics2D bGr = bImage.createGraphics();
          bGr.drawImage(img, 0, 0, null);
          bGr.dispose();
          return bImage;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-31
        • 2017-07-09
        • 1970-01-01
        • 2020-01-13
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        相关资源
        最近更新 更多