0
在此代碼中,我們使用諸如cv2,NumPy和PIL等工具預處理Tesseract OCR的RGB圖像。當這個代碼在Python 2.7.13 Shell中執行時,我收到以下錯誤消息。函數adaptiveThreshold中的CV_8UC1(Error-215)
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)
好的,@AndreySmorodov你能給我一個使用上面代碼的例子嗎? – lizardwizard
只是修復代碼,它會工作,我沒有指出那裏有什麼問題。我不知道這是什麼,它應該做什麼,爲每個頻道應用閾值,還是將其轉換爲灰度,然後是閾值,可能是其他內容?到處都有同樣的錯誤。我不知道你在這段代碼中意味着什麼。 –
與一個信道,收到錯誤回溯(最近最後調用): 文件 「C:\自動化\ OCR \圖像\ OCR_Preprocessing_ RGB.py」,第19行,在 對比度= cv2.merge([X]) TypeError:mv不是一個numpy數組,也不是一個標量 –
lizardwizard