从裁剪图像pytesseract中获取数字

从裁剪图像pytesseract中获取数字

本文介绍了从裁剪图像pytesseract中获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个裁剪的图像,我正在尝试获取裁剪图像上的数字这是我正在使用的代码

image = cv2.imread('Cropped.png')灰色 = cv2.cvtColor(图像,cv2.COLOR_BGR2GRAY)模糊 = cv2.GaussianBlur(gray, (3,3), 0)thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]内核 = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))开场 = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, 内核, 迭代=1)反转 = 255 - 打开数据 = pytesseract.image_to_string(invert, lang='eng', config='--psm 6')打印(数据)

这是裁剪后的示例图像

我得到了一些数字,而不是全部.如何增强这样的图像,使其只能提取数字?

我尝试了这张图片上的代码,但没有返回正确的数字

解决方案

你可以通过三个主要步骤轻松解决这个问题


    1. 上采样

    1. 应用阈值Pytesseract277032200746


现在如果你阅读,结果应该是这样的输出

277032200746

I have a cropped image and I am trying to get the numbers on that cropped imageHere's the code I am using

image = cv2.imread('Cropped.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening
data = pytesseract.image_to_string(invert, lang='eng', config='--psm 6')
print(data)

Here's the sample cropped image

All what I got some numbers and not all of them. How to enhance such an image to be able to extract only the numbers?

I tried the code on this image but doesn't return correct numbers

解决方案

You can easily solve this with three-main steps



Upsampling for accurate recognition. Otherwise tesseract may misterpret the digits.

Threshold Displays only the features of the image.

**Configuration Setting will recognize the digits


Upsampling
Threshold
Pytesseract277032200746


Code:

import cv2
import pytesseract

img1 = cv2.imread("kEpyN.png")  # "FX2in.png"
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*2, h*2))
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
txt1 = pytesseract.image_to_string(thr1, config="digits")
print("".join(t for t in txt1 if t.isalnum()))
cv2.imshow("thr1", thr1)
cv2.waitKey(0)

Update:


Most-probably a version mismatch causes extra words and digits.

One-way to solving is taking a range of the image

For instance, from the thresholded image:

(h_thr, w_thr) = thr1.shape[:2]
thr1 = thr1[0:h_thr-10, int(w_thr/2)-400:int(w_thr/2)+200]

Result will be:

Now if you read, result should be like this output

277032200746

这篇关于从裁剪图像pytesseract中获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 11:32