Reputation: 868
I would like to remove white background of an image and save it as another image.
I have wrote a code which extracts the background but it leaves some of the pixel value.
Checkout original Image :
It still leaves some amount of white background.
I want to remove that also.
Here is my code :
int x1=0;
int y1=0;
boolean res = false;
System.out.println("in formatImage");
//Widht Removal...
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;
}
}
}
//Height Removal
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 has final image.
Upvotes: 2
Views: 3536
Reputation: 14618
I did create a function to convert white background to transparent
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);
}
Now, if we want to convert this Image
to BufferedImage
then use this function
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;
}
Upvotes: 0
Reputation: 924
As Jim said, the colour near body are not pure white. So you modify the following statement of your code & it will work great for you. Replace following command line
if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE))
by
if (new Color(b21.getRGB(x, y)).getRGB()<-1000000)
This will give you the output which you want. You can vary the shades of white & gray from -1000000 to -2000000
Upvotes: 1
Reputation: 86774
If you examine the image with any decent image editor you will find that pixels near the model's head, left hand and right elbow are not pure white (0xFFFFFF).
You need to adjust your algorithm to allow some slight deviation from full intensity on all 3 channels. It's up to you to decide how much leeway to allow.
Upvotes: 2