Mis*_*ter 4 python opencv tesseract python-tesseract
我正在尝试使用 Tesseract-OCR 来读取下面图像的读数,但在获得与发现的背景一致的结果时遇到问题。我的 pytesseract 上有以下配置
\nCONFIG = f"\xe2\x80\x94psm 6 -c tessedit_char_whitelist=01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ\xc3\x85\xc3\x84abcdefghijklmnopqrstuvwxyz\xc3\xa5\xc3\xa4\xc3\xb6.,-"
我也尝试过下面的图像预处理,取得了一些不错的结果,但仍然不是完美的结果
\nblur = cv2.blur(img,(4,4))\n(T, threshInv) = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)\nRun Code Online (Sandbox Code Playgroud)\n我想要的是始终能够识别数字和小数点分隔符。什么样的图像预处理可以帮助获得一致的图像结果,如下所示?
\n\n您可以通过在频域而不是空间域中进行滤波来找到使用稍微复杂的方法的解决方案。阈值可能需要一些调整,具体取决于超立方体对输出图像的执行方式。
执行:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Test\\number.jpg', cv2.IMREAD_GRAYSCALE)
# Perform 2D FFT
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
# Squash all of the frequency magnitudes above a threshold
for idx, x in np.ndenumerate(magnitude_spectrum):
if x > 195:
fshift[idx] = 0
# Inverse FFT back into the real-spatial-domain
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.real(img_back)
img_back = cv2.normalize(img_back, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
out_img = np.copy(img)
# Use the inverted FFT image to keep only the black values below a threshold
for idx, x in np.ndenumerate(img_back):
if x < 100:
out_img[idx] = 0
else:
out_img[idx] = 255
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(img_back, cmap = 'gray')
plt.title('Reversed FFT'), plt.xticks([]), plt.yticks([])
plt.subplot(133),plt.imshow(out_img, cmap = 'gray')
plt.title('Output'), plt.xticks([]), plt.yticks([])
plt.show()
Run Code Online (Sandbox Code Playgroud)
输出:
中值模糊实现:
import cv2
import numpy as np
img = cv2.imread('C:\\Test\\number.jpg', cv2.IMREAD_GRAYSCALE)
blur = cv2.medianBlur(img, 3)
for idx, x in np.ndenumerate(blur):
if x < 20:
blur[idx] = 0
cv2.imshow("Test", blur)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)
输出:
最终编辑:
因此,使用 Eumel 的解决方案并将这段代码组合在其底部会产生 100% 成功的结果:
img[pat_thresh_1==1] = 255
img[pat_thresh_15==1] = 255
img[pat_thresh_2==1] = 255
img[pat_thresh_25==1] = 255
img[pat_thresh_3==1] = 255
img[pat_thresh_35==1] = 255
img[pat_thresh_4==1] = 255
# Eumel's code above this line
img = cv2.erode(img, np.ones((3,3)))
cv2.imwrite("out.png", img)
cv2.imshow("Test", img)
print(pytesseract.image_to_string(Image.open("out.png"), lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789.,'))
Run Code Online (Sandbox Code Playgroud)
输出图像示例:
将超立方体字符列入白名单似乎也有助于防止错误识别。
这是一个挑战,但我认为我有一个有趣的方法:模式匹配
如果放大,您会发现背面的图案只有 4 个可能的点:一个全像素、一个双全像素和一个中左或右的双像素。所以我所做的就是从 17.160.000,00 的图像中获取这 4 个模式并开始工作。保存这些以再次加载,我只是即时抓住它们
img = cv2.imread('C:/Users/***/17.jpg', cv2.IMREAD_GRAYSCALE)
pattern_1 = img[2:5,1:5]
pattern_2 = img[6:9,5:9]
pattern_3 = img[6:9,11:15]
pattern_4 = img[9:12,22:26]
# just to show it carries over to other pics ;)
img = cv2.imread('C:/Users/****/6.jpg', cv2.IMREAD_GRAYSCALE)
Run Code Online (Sandbox Code Playgroud)
实际模式匹配
接下来,我们匹配所有模式和阈值来查找所有出现的情况,我使用了 0.7,但您可以稍微尝试一下。这些图案去掉了侧面的一些像素,并且只匹配左侧的一个像素,因此我们填充两次(一个额外)以同时命中前 3 个图案。最后一个是单个像素,所以不需要它
res_1 = cv2.matchTemplate(img,pattern_1,cv2.TM_CCOEFF_NORMED )
thresh_1 = cv2.threshold(res_1,0.7,1,cv2.THRESH_BINARY)[1].astype(np.uint8)
pat_thresh_1 = np.pad(thresh_1,((1,1),(1,2)),'constant')
pat_thresh_15 = np.pad(thresh_1,((1,1),(2,1)), 'constant')
res_2 = cv2.matchTemplate(img,pattern_2,cv2.TM_CCOEFF_NORMED )
thresh_2 = cv2.threshold(res_2,0.7,1,cv2.THRESH_BINARY)[1].astype(np.uint8)
pat_thresh_2 = np.pad(thresh_2,((1,1),(1,2)),'constant')
pat_thresh_25 = np.pad(thresh_2,((1,1),(2,1)), 'constant')
res_3 = cv2.matchTemplate(img,pattern_3,cv2.TM_CCOEFF_NORMED )
thresh_3 = cv2.threshold(res_3,0.7,1,cv2.THRESH_BINARY)[1].astype(np.uint8)
pat_thresh_3 = np.pad(thresh_3,((1,1),(1,2)),'constant')
pat_thresh_35 = np.pad(thresh_3,((1,1),(2,1)), 'constant')
res_4 = cv2.matchTemplate(img,pattern_4,cv2.TM_CCOEFF_NORMED )
thresh_4 = cv2.threshold(res_4,0.7,1,cv2.THRESH_BINARY)[1].astype(np.uint8)
pat_thresh_4 = np.pad(thresh_4,((1,1),(1,2)),'constant')
Run Code Online (Sandbox Code Playgroud)
编辑图像
现在唯一要做的就是从图像中删除所有匹配项。由于我们的背景大部分为白色,因此我们只需将它们设置为 255 即可融入。
img[pat_thresh_1==1] = 255
img[pat_thresh_15==1] = 255
img[pat_thresh_2==1] = 255
img[pat_thresh_25==1] = 255
img[pat_thresh_3==1] = 255
img[pat_thresh_35==1] = 255
img[pat_thresh_4==1] = 255
Run Code Online (Sandbox Code Playgroud)
输出
编辑:
请查看 Abstracts 答案,以改进此输出和超正方体微调
| 归档时间: |
|
| 查看次数: |
668 次 |
| 最近记录: |