5

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("https://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.

herman
  • 11,740
  • 5
  • 47
  • 58
user2790209
  • 339
  • 2
  • 5
  • 17
  • 1
    Like [this](http://stackoverflow.com/a/14514294/418556)? – Andrew Thompson Sep 21 '13 at 12:28
  • @AndrewThompson Yes, like the grayscale example in that one! Will it completely convert all colors to gray & white? – user2790209 Sep 21 '13 at 12:30
  • @AndrewThompson Well, I've tried it, but other colors within the image remain, its not completely gray / white. Any other suggestions? – user2790209 Sep 21 '13 at 13:58
  • @AndrewThompson Its resolved now anyway. If you want to post an answer with what you've mentioned and TYPE_BYTE_BINARY , I can accept as it was with your help that I found it. – user2790209 Sep 22 '13 at 03:31
  • @AndrewThompson But you were the one who pointed me to it, I won't have gotten it without your help. So, you can post your answer and I'll delete mine and accept yours. :) – user2790209 Sep 22 '13 at 07:46
  • @AndrewThompson I didn't come up with it independently, I found it within the answer that you linked! – user2790209 Sep 22 '13 at 07:59
  • 1
    @AndrewThompson All right, I'll just accept mine when it lets me :) – user2790209 Sep 22 '13 at 08:18
  • @AndrewThompson If you might be able to answer this one, it'd be great. http://stackoverflow.com/questions/18941781/how-to-find-different-shades-of-a-color-in-java – user2790209 Sep 22 '13 at 08:43

1 Answers1

10

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.

user2790209
  • 339
  • 2
  • 5
  • 17
  • You should consider using an advanced binarization method like [Sauvola's](http://www.ee.oulu.fi/research/mvmp/mvg/files/pdf/pdf_24.pdf), if you have poor quality scans. http://fiji.sc/wiki/index.php/Auto_Local_Threshold – pvorb Feb 18 '14 at 14:04