Java – convert image to black and white – failed, bright color

I tried to convert the image to black and white only (not grayscale)

I used this:

BufferedImage blackAndWhiteImage = new BufferedImage(
        dWidth.intValue(),dHeight.intValue(),BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackAndWhiteImage.createGraphics();
graphics.drawImage(colourImage,null);

return blackAndWhiteImage;

Everything was fine until I decided to try brighter colors, such as the Google logo:

It came out:

Then I first try to use through the slot grayscale:

BufferedImage blackAndWhiteImage2 = new BufferedImage(
        dWidth.intValue(),BufferedImage.TYPE_USHORT_GRAY);

And it seems to save blue, but not the brightest (yellow in this case), and you may see a decline in its quality:

Any suggestions are greatly appreciated; I believe what I pursue is to convert every color to black, except white (which will be the background color), which is already applying type_ BYTE_ Binary is completed when the alpha channel is removed

Editor: maybe I didn't explain clearly:

>The final image must have a white background * * 1 > every other color must be converted to black

**1 – in some cases, the image is actually white and black... This is annoying because it complicates the process, and I'll stay later because converting "normal" images is a priority now

What I do is first delete the alpha channel (if it exists) – > so convert the alpha channel to white; Then convert each other color to black

Solution

If you use JavaFX, you can use the coloradjust effect with a brightness of - 1 (minimum), which changes all (non white) colors to black:

public class Main extends Application {

    Image image = new Image("https://i.stack.imgur.com/UPmqE.png");

    @Override
    public void start(Stage primaryStage) {
        ImageView colorView = new ImageView(image);
        ImageView bhView = new ImageView(image);

        ColorAdjust colorAdjust = new ColorAdjust();
        colorAdjust.setBrightness(-1);
        bhView.setEffect(colorAdjust);

        primaryStage.setScene(new Scene(new V@R_967_2419@(colorView,bhView)));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

These effects are optimized, so they may be faster than applying them manually

edit

Since that's what you want

>Any opaque pixels should be converted to white, and > any non white pixels should be converted to black,

As far as I know, pre - designed effects don't suit you - they're too specific You can operate pixel by pixel:

WritableImage writableImage = new WritableImage(image.getPixelReader(),(int) image.getWidth(),(int) image.getHeight());
PixelWriter pixelWriter = writableImage.getPixelWriter();
PixelReader pixelReader = writableImage.getPixelReader();
for (int i = 0; i < writableImage.getHeight(); i++) {
    for (int j = 0; j < writableImage.getWidth(); j++) {
        Color c = pixelReader.getColor(j,i);
        if (c.getOpacity() < 1) {
            pixelWriter.setColor(j,i,Color.WHITE);
        }
        if (c.getRed() > 0 || c.getGreen() > 0 || c.getBlue() > 0) {
            pixelWriter.setColor(j,Color.BLACK);
        }
    }
}
ImageView imageView = new ImageView(writableImage);

Note that the order in which you apply the rules is important If you apply 1 and then 2, the transparent non white pixel will turn white, but if you apply 2 and then 1, it will turn black This is because the predefined white and black colors are opaque Instead of changing the alpha value, you can manually set the red, green, and blue values It all depends on your exact requirements

Remember, you may not find real white at all due to the lossy compression of some file formats, but your eyes will not be able to distinguish near the value of real white

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>