python+opencv运行报错 Invalid number of channels in input image:

想把剩下的图像处理小demo跑一趟,在做Canny边缘提取处理时报错
程序如下:

import cv2 as cv
import numpy as np

def edge_demo(image):
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_GRAY2BGR)
    # X Grodient
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
    # Y grodient
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
    #edge
    edge_output = cv.Canny(xgrad, ygrad, 50, 150)
    cv.imshow("Canny Edge", edge_output)

    dst = cv.bitwise_and(image, image, mask=edge_output)
    cv.imshow("Color Edge", dst)

print("------------come on baby-------------")
src = cv.imread("D:/sbilibili/lena.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
edge_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

错误信息如下:

Traceback (most recent call last):
  File "E:/untitled1/tutorial_17.py", line 22, in 
    edge_demo(src)
  File "E:/untitled1/tutorial_17.py", line 6, in edge_demo
    gray = cv.cvtColor(blurred, cv.COLOR_GRAY2BGR)
cv2.error: OpenCV(4.1.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper,struct cv::impl::A0xe227985e::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 3

各种谷歌之后问题大概是我的opencv-contrib-python和opencv-python版本太高,把两个库重新加载,选择3.4.2.16
在这里插入图片描述
之后运行报错
Traceback (most recent call last): File "E:/untitled1/tutorial_17.py", line 23, in edge_demo(src) File "E:/untitled1/tutorial_17.py", line 7, in edge_demo gray = cv.cvtColor(blurred, cv.COLOR_GRAY2BGR) cv2.error: OpenCV(3.4.2) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'
又是一圈谷歌。。没啥用,结果仔细看一下代码,发现line7 cv.COLOR_BGR2GRAY写成了cv.COLOR_GRAY2BGR
行吧。。能出来就行
python+opencv运行报错 Invalid number of channels in input image:_第1张图片

你可能感兴趣的:(opencv)