简体   繁体   English

如何将图像与文本二值化? [MATLAB]

[英]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. 我希望文本最终变为白色,但是有些图像的文本比背景暗,有些图像的文本比背景暗。 在此处输入图片说明 在此处输入图片说明

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 非常感谢您的帮助

there are 2 possible solutions which I had in mind: 我想到了2种可能的解决方案:

solution1: 解决方案1:

  1. generate a grayscale image using rgb2gray function. 使用rgb2gray函数生成灰度图像。

  2. generate a histogram from the grayscale image, and ignore the transparent pixels. 从灰度图像生成直方图,并忽略透明像素。 you can use imhist function. 您可以使用imhist功能。

  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: 解决方案2:

the solution asserts that the image is simple enough, ie 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. 使用bwconncomp函数将图像划分为连接的组件。
  3. for each connected component find it's representative value, it can either be 0 or 1 对于每个连接的组件,找到其代表值,可以为0或1
  4. check what is the most common represantative value. 检查最常见的代表值。 if it is 1 - the letters are dark. 如果为1,则字母为暗。 in this case take the negative image and than binarize. 在这种情况下,拍摄负片,然后进行二值化处理。 otherwise - binarize the input image as is. 否则-按原样对输入图像进行二值化处理。

good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM