Color image text for binary image

I am developing and OCR. OCR should accept an image of text with a font of any color against the background of any other color. To do this, I need to convert the input image to binary (1-bit image).

This is trivially easy in the case of a white font over a black (or dark) background. It is still easy if my font color and background color are very contrasted. However, I have difficulty when both colors are quite dark or light at the same time.

I used this code to convert color BufferedImageto binary. It is great for contrasting colors. But it fails when both colors are dark or light.

public static BufferedImage binarizeImage(BufferedImage img_param)
{
    //grey
    BufferedImage img_grey = new BufferedImage(img_param.getWidth(), img_param.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);
    Graphics g1 = img_grey.getGraphics();
    g1.drawImage(img_param, 0, 0, null);
    g1.dispose();
    //showImageBuffer(img_grey);

    //b&w
    BufferedImage image = new BufferedImage(img_grey.getWidth(), img_grey.getHeight(),
            BufferedImage.TYPE_BYTE_BINARY);
    Graphics g2 = image.getGraphics();
    g2.drawImage(img_grey, 0, 0, null);
    g2.dispose();
    //showImageBuffer(img_grey);
    return image;
}

, : ( : 255, : 0) black-over-red .

- . , , .

, , , , .

EDIT: . .

+4

Source: https://habr.com/ru/post/1613833/


All Articles