简体   繁体   English

使用 Python/OpenCV/Tesseract 从图像中提取数据?

[英]Extract Data from an Image with Python/OpenCV/Tesseract?

I'm trying to extract some contents from a cropped image.我正在尝试从裁剪的图像中提取一些内容。 I tried pytesseract and opencv template matching but the results are very poor.我试过pytesseract和opencv模板匹配但是结果很差。 OpenCV template matching sometimes fails due to poor quality of the icons and tesseract gives me a line of text with false characters. OpenCV 模板匹配有时会由于图标质量差而失败,而 tesseract 给了我一行带有错误字符的文本。

I'm trying to grab the values like this:我正在尝试获取这样的值:

0:26 83 1 1 0:26 83 1 1

Any thoughts or techniques?有什么想法或技巧吗?

在此处输入图像描述

A technique you could use would be to blur your image.您可以使用的一种技术是模糊您的图像。 From what it looks like, the image is kind of low res and blurry already, so you wouldn't need to blur the image super hard.从它的外观来看,图像已经有点低分辨率和模糊,所以你不需要非常努力地模糊图像。 Whenever I need to use a blur function in Opencv, I normally choose the gaussian blur, as its technique of blurring each pixel as well as each surrounding pixel is great.每当我需要在 Opencv 中使用模糊 function 时,我通常会选择高斯模糊,因为它模糊每个像素以及每个周围像素的技术都很棒。 Once the image is blurred, I would threshold, or adaptive threshold the image.一旦图像模糊,我将对图像进行阈值或自适应阈值。 Once you have gotten this far, the image that should be shown should be mostly hard lines with little bits of short lines mixed between.一旦你走到这一步,应该显示的图像应该主要是硬线,中间夹杂着少量的短线。 Afterwards, dilate the threshold image just enough to have the bits where there are a lot of hard edges connect.然后,将阈值图像扩大到足以连接有很多硬边缘的位。 Once a dilate has been performed, find the contours of that image, and sort based on their height with the image.执行扩张后,找到该图像的轮廓,并根据它们与图像的高度进行排序。 Since I assume the position of those numbers wont change, you will only have to sort your contours based on the height of the image.由于我假设这些数字的 position 不会改变,因此您只需根据图像的高度对轮廓进行排序。 Afterwards, once you have sorted your contours, just create bounding boxes over them, and read the text from there.之后,一旦你对轮廓进行了排序,只需在它们上面创建边界框,然后从那里读取文本。

However, if you want to do this the quick and dirty way, you can always just manually create your own ROI's around each area you want to read and do it that way.但是,如果您想以快速而肮脏的方式执行此操作,则始终可以在要阅读的每个区域周围手动创建自己的 ROI 并以这种方式进行。

First Method第一种方法

  1. Gaussian blur the image高斯模糊图像
  2. Threshold the image阈值图像
  3. Dilate the image放大图像
  4. Find Contours查找轮廓
  5. Sort Contours based on height根据高度对等高线进行排序
  6. Create bounding boxes around relevent contours在相关轮廓周围创建边界框

Second Method第二种方法

  1. Manually create ROI's around the area you want to read text from在要从中读取文本的区域周围手动创建 ROI

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

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