我想删除图像的白色背景并将其另存为另一幅图像。 我写了一个提取背景的代码,但它留下了一些像素值。 结帐原始图片:
它仍然会留下一些白色背景。
我也想删除它。
这是我的代码:
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 有最终图像。
最佳答案
如果您使用任何像样的图像编辑器检查图像,您会发现模型头部、左手和右手肘附近的像素不是纯白色 (0xFFFFFF)。
您需要调整您的算法以允许在所有 3 个 channel 上略微偏离全强度。允许多少回旋余地由您决定。
关于java - 如何删除图像的白色背景 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39554300/