【发布时间】:2026-01-29 01:45:01
【问题描述】:
我正在使用 Python2.7.12 和 OpenCV 3.0.0-rc1
我正在做一个文本识别项目。
这就是我现在得到的。 Original iamge after findContour, line 34
如您所见,图像包含很多“框”,其中有文字。
我的方法是找到这些框,将它们切割成单独的图像,然后将它们提供给 TesseractOCR。
程序将整个图像视为一个轮廓。 我怎样才能找到里面较小的那个?
或者,如果您有其他方法,欢迎
代码:
import cv2
def threshold(im, method):
# make it grayscale
im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
if method == 'fixed':
threshed_im = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY)
elif method == 'mean':
threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 10)
elif method == 'gaussian':
threshed_im = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7)
else:
return None
return threshed_im
image = cv2.imread('demo4.jpg')
# threshold it
thresh = threshold(image, 'mean')
# find contours
_, cnts, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print len(cnts)
cv2.drawContours(image, cnts, -1, (0, 255, 0), 20)
cv2.imshow('contours', image)
cv2.waitKey()
cv2.drawContours(thresh, cnts, -1, (0, 255, 0), 20)
cv2.imshow('contours', thresh)
cv2.waitKey()
`
【问题讨论】:
标签: python opencv computer-vision ocr tesseract