如何使用pytesseract检测黄色立方体内的数字
How to detech numbers inside a yellow cube with pytesseract
所以我一直在尝试检测黄色立方体中的数字 (1-9),但没有固溶体..
这是我的两张照片
这是我一直在尝试的一种解决方案,但没有成功
from PIL import Image
from operator import itemgetter
import numpy as np
import easyocr
import cv2
import re
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
img = cv2.imread("ROI_0.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 59, 88)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6 digits")
txt = txt.strip().split("\n")
print(txt)
cv2.imshow("bnt", bnt)
cv2.waitKey(0)
有没有其他方法可以做到这一点,因为它不起作用?
步骤:
二值化(大津的方法)
使用 minAreaRect
校正倾斜
求最大面积轮廓
裁剪数字
通过裁剪到 pytesseract
image = cv2.imread("y6.png")
# image = image_resize(image,width=480,height=640)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
big = max(contours,key=cv2.contourArea)
(x,y),(w,h),angle = cv2.minAreaRect(big)
print(angle)
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(thresh, M, (w, h),flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT,borderValue=(0,0,0))
big = cv2.findContours(rotated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
big = max(big,key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(big)
# cropped = rotated[y:y+h,x:x+w]
cropped = rotated[y:y+h-h//10,w//6:x+w-w//6]
data = pytesseract.image_to_string(cropped,config='--psm 6 digits')# -c tessedit_char_whitelist = 0123456789')
print(data)
有一些硬编码值,例如 h//10,并且都在裁剪中。所以需要优化。
您需要先移除黑色边框,tesseract 才能正常工作。只需将黑色背景替换为白色,然后应用阈值处理,这样它就可以同时去除白色边框和黄色,然后使用 tesseract 检测字符。
所以我一直在尝试检测黄色立方体中的数字 (1-9),但没有固溶体..
这是我的两张照片
这是我一直在尝试的一种解决方案,但没有成功
from PIL import Image
from operator import itemgetter
import numpy as np
import easyocr
import cv2
import re
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
img = cv2.imread("ROI_0.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 59, 88)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6 digits")
txt = txt.strip().split("\n")
print(txt)
cv2.imshow("bnt", bnt)
cv2.waitKey(0)
有没有其他方法可以做到这一点,因为它不起作用?
步骤:
二值化(大津的方法)
使用 minAreaRect
校正倾斜求最大面积轮廓
裁剪数字
通过裁剪到 pytesseract
image = cv2.imread("y6.png") # image = image_resize(image,width=480,height=640) gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] big = max(contours,key=cv2.contourArea) (x,y),(w,h),angle = cv2.minAreaRect(big) print(angle) (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, angle, 1.0) rotated = cv2.warpAffine(thresh, M, (w, h),flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT,borderValue=(0,0,0)) big = cv2.findContours(rotated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] big = max(big,key=cv2.contourArea) x,y,w,h = cv2.boundingRect(big) # cropped = rotated[y:y+h,x:x+w] cropped = rotated[y:y+h-h//10,w//6:x+w-w//6] data = pytesseract.image_to_string(cropped,config='--psm 6 digits')# -c tessedit_char_whitelist = 0123456789') print(data)
有一些硬编码值,例如 h//10,并且都在裁剪中。所以需要优化。
您需要先移除黑色边框,tesseract 才能正常工作。只需将黑色背景替换为白色,然后应用阈值处理,这样它就可以同时去除白色边框和黄色,然后使用 tesseract 检测字符。