gpt4 book ai didi

python - 函数adaptiveThreshold中的CV_8UC1(错误215)

转载 作者:行者123 更新时间:2023-12-02 17:09:40 28 4
gpt4 key购买 nike

在这段代码中,我们使用 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)

最佳答案

阈值方法需要 1 个 channel 图像作为输入,而您给 3 个 channel ,这就是错误消息中显示的问题。

关于python - 函数adaptiveThreshold中的CV_8UC1(错误215),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com