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 <module>
    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::`anonymous namespace'::Set<1,-1,-1>,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 <module> 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<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'
又是一圈谷歌。。没啥用,结果仔细看一下代码,发现line7 cv.COLOR_BGR2GRAY写成了cv.COLOR_GRAY2BGR
行吧。。能出来就行
在这里插入图片描述

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值