【问题标题】:CV_8UC1 (Error-215) in function adaptiveThreshold函数adaptiveThreshold中的CV_8UC1(错误215)
【发布时间】:2017-08-28 13:14:39
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 opencv numpy python-imaging-library


    【解决方案1】:

    Threshold 方法需要 1 个通道的图像作为输入,而你给了 3 个通道,这就是错误消息中显示的问题。

    【讨论】:

    • 好的,@AndreySmorodov 你能给我一个使用上面代码的例子吗?
    • 只需修复代码,它就会起作用,我确实指出了那里有什么问题。我不知道这是什么,它应该做什么,对每个通道应用阈值,或者将其转换为灰色然后阈值,可能是别的什么?到处都有同样的错误。我不知道你在这段代码中是什么意思。
    • 只有一个通道时出现错误 Traceback(最近一次调用最后一次):文件“C:\Automation\OCR\images\OCR_Preprocessing_RGB.py”,第 19 行,在 contrast = cv2. merge([x]) TypeError: mv 不是 numpy 数组,也不是标量
    • merge 从 3 个单独的单通道图像中生成 3 个通道图像。 split 方法通过通道分割图像,从多通道图像中产生 n 个单独的图像。而你试图合并一个频道,因为你得到了一个错误。
    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2017-02-07
    • 2022-10-15
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多