去除 OCR 图像处理中的背景颜色
我正在尝试删除背景颜色,以提高 OCR 对图像的准确性。示例如下所示:
我会保留后处理图像中的所有字母,同时删除浅紫色带纹理的背景。是否可以使用一些开源软件如Imagemagick将其转换为二值图像(黑/白)来实现这一目标?如果背景有不止一种颜色怎么办?解决方案会一样吗?
此外,如果我还想删除紫色字母(剧院名称)和线条,只保留黑色字母,该怎么办?简单的裁剪可能不起作用,因为紫色字母也可能出现在其他地方。
我正在寻找编程解决方案,而不是通过 Photoshop 等工具。
I am trying to remove background color so as to improve the accuracy of OCR against images. A sample would look like below:
I'd keep all letters in the post-processed image while just removing the light purple color textured background. Is it possible to use some open source software such as Imagemagick to convert it to a binary image (black/white) to achieve this goal? What if the background has more than one color? Would the solution be the same?
Further, what if I also want to remove the purple letters (theater name) and the line so as to only keep the black color letters? Simple cropping might not work because the purple letters could appear at other places as well.
I am looking for a solution in programming, rather than via tools like Photoshop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 GIMP(或任何其他图像编辑工具)来完成此操作。
模糊图像:
差异图像:
二进制:
如果您只是一次性执行此操作,GIMP 可能就足够了。如果您希望多次执行此操作,您可能可以编写一个 imagemagick 脚本或使用 Python 和 OpenCV 等工具编写您的方法。
上述方法的一些问题:
You can do this using GIMP (or any other image editing tool).
Blurred image:
Difference image:
Binary:
If you're doing it as a once-off, GIMP is probably good enough. If you expect to do this many times over, you could probably write an imagemagick script or code up your approach using something like Python and OpenCV.
Some problems with the above approach:
下面显示了处理图像及其 OCR 的可能策略
最后一步是进行 OCR。我的 OCR 例程非常基础,所以我相信您可能会得到更好的结果。
该代码是 Mathematica 代码。
一点也不差!
The following shows a possible strategy for processing your image, and OCR it
The last step is doing an OCR. My OCR routine is VERY basic, so I'm sure you may get better results.
The code is Mathematica code.
Not bad at all!
在 Imagemagick 中,您可以使用 -lat 函数来执行此操作。
In Imagemagick, you can use the -lat function to do that.
您可以对图像应用模糊,这样您就可以获得几乎清晰的背景。然后将原始图像每个像素的每个颜色分量除以背景上像素的相应分量。您将在白色背景上看到文本。额外的后处理可以提供进一步的帮助。
如果文本比背景(在每个颜色分量中)暗,则此方法适用。否则,您可以反转颜色并应用此方法。
You can apply blur to the image, so you get almost clear background. Then divide each color component of each pixel of original image by the corresponding component of pixel on the background. And you will get text on white background. Additional postprocessing can help further.
This method works in the case if text is darker then the background (in each color component). Otherwise you can invert colors and apply this method.
如果您的图像被捕获为 RGB,只需使用绿色图像或快速转换拜耳模式,这可能是 @misha 转换为灰度解决方案 可能可以。
If your image is captured as RGB, just use the green image or quickly convert the bayer pattern which is probably @misha's convert to greyscale solutions probably do.
希望这对某人有帮助
使用您可以获得的一行代码是使用 OpenCV 和 python
这是结果
这是 图像阈值
Hope this helps someone
Using one line code you can get is using OpenCV and python
Here's the result
Here's the link for Image Thresholding in OpenCV