[英]Extract number from image with 2D array using Python tesseract
在从包含2D矩阵形式的图像中提取整数时,Tesseract无法给出正确的结果,并且每次执行代码时结果都会有所不同,有人可以告诉我们以下代码中缺少的内容吗
img = cv2.imread(img_path)
rows = img.shape[0]
cols = img.shape[1]
#print rows , cols
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion to remove some noise
#kernel = np.ones((5,5), np.uint64)
#img = cv2.dilate(img, kernel, iterations=1)
#img = cv2.erode(img, kernel, iterations=1)
# Write image after removed noise
cv2.imwrite(src_path + "removed_noise1.png", img)
# Apply threshold to get image with only black and white
#img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,225,95)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C , cv2.THRESH_BINARY ,251,95)
#print cv2.getGaussianKernel(ksize=221,sigma=41)
# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres1.png", img)
# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres1.png"))
输入项
阈值:adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,251,95
阈值输出:
输出形式为1 5 5 7 5 7 3 8 6 4 9 0 2 4 8 6 1 3 0 2 3 9 0 8 9可以是行主形式和列主形式无关紧要,但是我们可以需要将给定的输出保存到变量中
尝试将阈值从251、95更改为251、40。
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C , cv2.THRESH_BINARY ,251,40)
看来您已经拥有完美的图像,不需要任何更改即可从图像中提取字符串。 Pytesseract的image_to_string在我的系统上不起作用,所以我使用了别人手工制作的OCR。 绝对不是世界上最好的解决方案,嘿,如果可行,它就会奏效。 我已附加了几个文件(请参阅下面的Google驱动器链接)
脚步:
请注意:
TrainAndTest.py- https: //drive.google.com/file/d/0B05aeuFExe2Aa3p3SWszN2xqU2c/view ? usp = sharing
slice_image.py- https: //drive.google.com/file/d/0B05aeuFExe2AN0t3UUlGZ3VjcW8/view ? usp = sharing
training_chars.png- https://drive.google.com/file/d/0B05aeuFExe2ANjJNbzV5VTJyRTA/view?usp=sharing
分类.txt- https://drive.google.com/file/d/0B05aeuFExe2AZU91bUpOblB3d2c/view?usp=sharing
flattened_images.txt- https: //drive.google.com/file/d/0B05aeuFExe2AeXVnbXVXVTZ2RTQ/view ? usp =sharing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.