如何删除图像的白色背景 - java

时间:2016-09-18 05:02:14

标签: java image

我想删除图像的白色背景并将其另存为另一个图像。 我编写了一个代码来提取背景,但它留下了一些像素值。 结帐原始图片:enter image description here

结帐裁剪图片:enter image description here

它仍留下一定量的白色背景。

我也想删除它。

这是我的代码:

db.visitors.aggregate([
{
    $project: {
        activities: {
            $setUnion: ['$activities.visits', '$activities.purchases'], 
        }
    }
}, 
{
    $project:{
        activites: {
            $slice: ["$activities", 0, 2]
        }
    }
}
])

// Widht Removal ...

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

b31有最终影像。

3 个答案:

答案 0 :(得分:2)

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

enter image description here

您需要调整算法,以便在所有3个通道上略微偏离全强度。由你决定允许多少回旋余地。

答案 1 :(得分:1)

正如吉姆所说, 身体附近的颜色不是纯白色。 所以你修改了你的代码的以下声明&amp;它会对你有用。 替换以下命令行

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

通过

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

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

答案 2 :(得分:0)

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

    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;
}
相关问题