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 :
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.