[英]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:
generate a grayscale image using rgb2gray function. 使用rgb2gray函数生成灰度图像。
generate a histogram from the grayscale image, and ignore the transparent pixels. 从灰度图像生成直方图,并忽略透明像素。 you can use imhist function. 您可以使用imhist功能。
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. 该解决方案断言图像足够简单,即除了字母之外没有很多连接的组件。
good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.