简体   繁体   中英

Java- How to remove background color from an image

First off I am new to this and also coding. I apologize in advance for anything that is misleading.

I am currently writing a Java program that uses an image as input. What I have currently is scanning each pixel by the width and height of the image saving the HSB in an array and then outputting the percentage of each color in the image. I now want to omit the background from that calculation. To start off lets just say the background is white. There are also pixels in the image that are not in the background that is white though.

thank you,

Oh, it's not as simple as you hope .
You cannot simply detect what's a background and what is part of image pixel by pixel.

You might try looking at this post to see how to remove one color layer of the image. But detecting if the white pixel is a part of background or already the image?!

There are multiple possible ways :

  • assuming that background is just around and when (looking from any side to the center) color changes, that is the end of the "background". You can check every row and column from side to center and keep record of where the "background" color ends.
  • or similar approach - if at least at one (of four) direction looking from the pixel to the side there is no color changes (it goes white all the way to the side), than it is part of background.
  • Or just take a look at another post . From this you can try working your way up.

Anyway - you have to create a logic of detecting which (ie) white pixels are part of the picture and which are part of background.

I hope this at least gives you a bit more knowledge.

I am not sure what you mean. There is no code, so I can only give you an example. From what I have understood, you want to skip doing a calculation when the color is the same as the background. That is very simple. You can do something like this:

for(int x = 0; x < width; x++) {
    for(int y = 0; y < height; y++) {
        Color pixelColor = get pixel color at x and y;
        Color backgroundColor = the background color;
        if(pixelColor != backgroundColor) {
            //Calculation will be done here
        }
    }
}

If this does not help you or you have any other questions, ask me.

Here is an example of full working code.

You can add a cursor to pick color and jslider for fuzz or threshold. Use backColor and threshold for your needs.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Convert {
    private static final Color backColor = new Color(255,255,255);
    private static final int THRESHOLD = 35;
    private static final int TRANSPARENT = 0;  // 0x00000000;

    static File base  = new File("f://mortar1.png");
    static File base2 = new File("f://outtrans.png");

    public static void main(String[] args) throws IOException {
        System.out.println("Convert.main()");
        for (File file : base.listFiles()) 
        {
            BufferedImage initImage = ImageIO.read(base);
            int width = initImage.getWidth(null),
              height = initImage.getHeight(null);

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = image.getGraphics();
            g.drawImage(initImage, 0, 0, null);

            System.out.println("before: " + image.getRGB(0, 0));
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int pixel = image.getRGB(x, y);
                    Color color = new Color(pixel);

                    int dr = Math.abs(color.getRed()   - backColor.getRed()),
                    dg = Math.abs(color.getGreen() - backColor.getGreen()),
                    db = Math.abs(color.getBlue()  - backColor.getBlue());

                    if (dr < THRESHOLD && dg < THRESHOLD && db < THRESHOLD) {
                        image.setRGB(x, y, TRANSPARENT);
                    }
                }
            }
            System.out.println("   after: " + image.getRGB(0, 0));

           File file = new File("f://outtrans1.png");
            ImageIO.write(image, "png", file);
        }
    }
}

Or simple set a png file with empty fill..

This idea seems good for me. But if you don't agree, please, tell me causes)

I usually use the online background remover. It's "https://removal.ai". It uses AI so the background removal is very fast and efficient. I believe it will be useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM