user5380136
user5380136

Reputation:

How can I binarize images with text? [MATLAB]

I have a problem with a binarization method. I have images with text, which I want to binarize. I want the text ends up being white, but there are images with the text darker than the background and there are images with text less dark than the background. enter image description here enter image description here

I want to binarize images like these, but I want the text in white color in the binarized images.

By the way, I am binarizing images with this code. This code is good for images with text darker than the background but it isn't good for text less darker than the background. I think I need a method to know if the text is more or less dark than the background for to invert or no invert the binarization.

        umb = graythresh(originalImage);
        binaryImage =(~im2bw(originalImage,umb));

How can I do it?

Thanks a lot for the help

Upvotes: 0

Views: 215

Answers (1)

ibezito
ibezito

Reputation: 5822

there are 2 possible solutions which I had in mind:

solution1:

  1. generate a grayscale image using rgb2gray function.

  2. generate a histogram from the grayscale image, and ignore the transparent pixels. you can use imhist function.

  3. check what is the histogram maximal value. if the value is high - the background is probably light and the text should be darker than the background. in this case - take the negative image (for example, by using imcomplement), and then binarize it. otherwise - you can binarize it as is.

solution 2:

the solution asserts that the image is simple enough, i.e. doesn't have a lot of connected components other than the letters.

  1. binarize the input image.
  2. divide the image into connected components using bwconncomp function.
  3. for each connected component find it's representative value, it can either be 0 or 1
  4. check what is the most common represantative value. if it is 1 - the letters are dark. in this case take the negative image and than binarize. otherwise - binarize the input image as is.

good luck!

Upvotes: 1

Related Questions