python - 获取 openCV 错误 : Assertion Failed

标签 python opencv raspberry-pi assertions

我在 RaspberryPi 3 中使用 opencv 3.1。我正在尝试运行以下霍夫圆检测算法

#! /usr/bin/python
import numpy as np
import cv2
from cv2 import cv

VInstance = cv2.VideoCapture(0)
key = True


"""
params = dict(dp,
              minDist,
              circles,
              param1,
              param2,
              minRadius,
              maxRadius)
"""
def draw_circles(circles, output):

    if circles is not None:

        for i in circles[0,:]:
            #draw the outer circle
            cv2.circle(output,(i[0],i[1]),i[2],(0,255,0),2)
            #draw the centre of the circle
            cv2.circle(output,(i[0],i[1]),2,(0,0,255),3)
            print("The number of circles if %d" %(circles[0].shape[0]))      
    elif circles is None:
        print ("The number of circles is 0")

if __name__ == '__main__':

    while key:
        ret,img = VInstance.read()
        ## Smooth image to reduce the input noise

        imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        imgSmooth = cv2.GaussianBlur(imgGray,(5,5),3)

        ## Compute Hough Circles
        circles = cv2.HoughCircles(imgSmooth,cv2.cv.CV_HOUGH_GRADIENT,1,100,
                                   param1=80,
                                   param2=50,
                                   minRadius=50,
                                   maxRadius=100)
        draw_circles(circles,img)

        ## Display the circles
        cv2.imshow('detected circles',imgGray)
        cv2.imshow("result",img)
        k = cv2.waitKey(1)
        if k == 27:
            cv2.destroyAllWindows()
            break

但我收到断言失败错误,详细信息如下。

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp, line 8000 Traceback (most recent call last): File "HoughCircles.py", line 70, in imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor

任何人都可以检查并提供帮助吗!

最佳答案

错误代码“cvtColor 中断言失败 (scn == 3 || scn == 4)”表示 cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 中的输入(源)图像方法没有 3 或 4 个 channel ,而这是此类转换所必需的。可能您的输入图像已经是灰度格式。尝试不要使用该方法,您的问题应该可以解决。如果它确实引发其他无法解决的错误或无法解决问题,请在评论中发布您的问题。

关于python - 获取 openCV 错误 : Assertion Failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38542599/

相关文章:

Python OpenCV 模板匹配和特征检测无法正常工作

c - 覆盆子 : how does the PWM via DMA work?

python - Python音频功能无法正常工作

ssl - 带有 vsftpd : configuring SSL/TLS 的 FTP 服务器

python - 如何使用特定的 dtype 填充现有的 numpy 数组

python - 将 python 操作保留为二进制

python - Gtk-警告 ** : cannot open display:

python - 是否可以旋转数据框并获得每月聚合?

python - 在 Python 中使用多行正则表达式?

opencv - 使用Python在OpenCV中检测模板的坐标