在此代码中,我们使用cv2,NumPy和PIL等工具预处理Tesseract OCR的RGB图像。当在Python 2.7.13 Shell中执行此代码时,我收到以下错误消息。
Traceback (most recent call last): File "C:\Automation\OCR\images\OCR_Preprocessing_ RGB.py", line 23, in <module> cv2.THRESH_BINARY,11,2) error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\thresh.cpp:1446: error: (-215) src.type() == CV_8UC1 in function cv::adaptiveThreshold
以下是生成错误的代码。我有标记的代码行,我认为问题可能就是这样。
import cv2
import numpy as np
from matplotlib import pyplot as plt
from cycler import cycler
from PIL import Image, ImageEnhance
# Loads the image then enhances it
image = Image.open('teleCapture.png')
contrast = ImageEnhance.Contrast(image)
img = contrast.enhance(2)
img = np.asarray(img)
r,g,b,a = cv2.split(img) // I know the issue is here, I have too many channels for an RGB image or I am merginf them wrong.
contrast = cv2.merge([b,g,r]) //"Contrast" was used as a src during Thresholding, is this what it should be?
# Adaptive Gaussian Thresholding //The problem may be within the thresholding, does this thresholding function only work using grayscale images?
th1 = cv2.adaptiveThreshold(contrast,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
# Otsu's thresholding
ret2,th2 = cv2.threshold(contrast,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(contrast,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# writes enhanced and thresholded img
cv2.imwrite('preprocessedTeleCapture.png', th2)
答案 0 :(得分:2)
阈值方法需要1个通道图像作为输入,你给3个通道,这就是错误信息中显示的问题。