Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a BufferedImage to black and white?

How can I convert an existing colored BufferedImage to monochrome? I want the image to be completely split between only two rgb codes, black and white. So if there's a border around the image which is a lighter or darker shade of the background, and the background is being converted to white, then I want the border to be converted to white as well, and so on.

How can I do this?

If its necessary to save the image / load it from disk, I can do that if necessary.

Edit: Code to test this:

package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashSet;
import javax.imageio.ImageIO;

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            BufferedImage img = ImageIO.read(new File("http://i.stack.imgur.com/yhCnH.png") );
            BufferedImage gray = new BufferedImage(img.getWidth(), img.getHeight(),
                    BufferedImage.TYPE_BYTE_GRAY);

            Graphics2D g = gray.createGraphics();
            g.drawImage(img, 0, 0, null);

            HashSet<Integer> colors = new HashSet<>();
            int color = 0;
            for (int y = 0; y < gray.getHeight(); y++)
            {
                for (int x = 0; x < gray.getWidth(); x++)
                {
                    color = gray.getRGB(x, y);
                    System.out.println(color);
                    colors.add(color);
                }
            }

            System.out.println(colors.size() );
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Image used:

image

Using this, I get the output of 24 as the last output, i.e there were 24 colors found in this image, when there should be 2.

like image 428
user2790209 Avatar asked Sep 21 '13 12:09

user2790209


People also ask

How do I change a buffered image to an image?

BufferedImage buffer = ImageIO. read(new File(file)); to Image i.e in the format something like : Image image = ImageIO.

How do you change a picture to black and white in Java?

Java | Converting an Image into Grayscale using cvtColor() Syntax: File input = new File("digital_image_processing. jpg"); BufferedImage image = ImageIO. read(input); To transform the image from RGB to Grayscale format by using method cvtColor() in the Imgproc class.

What does BufferedImage mean?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).

What is the difference between image and BufferedImage in Java?

Image is an abstract class. You can't instantiate Image directly. BufferedImage is a descendant, and you can instantiate that one. So, if you understand abstract classes and inheritance, you'll understand when to use each.


1 Answers

The solution was to use BufferedImage.TYPE_BYTE_BINARY as the type rather than TYPE_BYTE_GRAY. Using BINARY causes a pure black & white copy of the image to be created, with no colors except b & w. Not pretty looking, but perfect for things like OCR where the colors need to be exact.

like image 134
user2790209 Avatar answered Nov 10 '22 12:11

user2790209