简体   繁体   English

出现openCV错误:断言失败

[英]Getting openCV error: Assertion Failed

I'm using opencv 3.1 in RaspberryPi 3. I,m trying to run the following Hough Circle detection algorithm 我在RaspberryPi 3中使用opencv 3.1。我正在尝试运行以下Hough Circle检测算法

#! /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

But I'm getting Assertion Failed error, details are below. 但是我收到断言失败错误,详细信息如下。

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 || OpenCV错误:在cvtColor,文件/home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp,第8000行回溯中断言失败(scn == 3 || scn == 4) ):在imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)cv2.error:/home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp中,文件“ HoughCircles.py”,第70行: 8000:错误:(-215)scn == 3 || scn == 4 in function cvtColor scn == 4在函数cvtColor中

Can anyone please check and help! 任何人都可以检查和帮助!

Error code "Assertion failed (scn == 3 || scn == 4) in cvtColor" means that the input (source) image in your cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) method does not have 3 or 4 channels, which are necessary for this type of conversion. 错误代码“ cvtColor中的断言失败(scn == 3 || scn == 4)”表示cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)方法中的输入(源)图像没有3或4个通道,对于这种类型的转换是必需的。 Probably your input image is already grayscale format. 您的输入图像可能已经是灰度格式。 Try just not to use that method and your problem should be solved. 尝试不使用该方法,则应该解决您的问题。 If it does throw other unsolvable errors or does not solve the problem, post your issues in comments. 如果它确实引发了其他无法解决的错误或不能解决问题,请在评论中发布您的问题。

This means the input image is invalid, which is why you need to check the ret value in your loop! 这意味着输入图像无效,这就是为什么您需要在循环中检查ret值的原因!

The error and question title doesn't have anything to do with your Hough circles, so I'll condense my answer to address the assertion failure (add back in your stuff later!): 错误和标题与您的Hough圈子没有任何关系,因此我将浓缩我的答案以解决断言失败的问题(稍后再添加到您的内容中!):

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

VInstance = cv2.VideoCapture(0)

if __name__ == '__main__':

    while True:
        ret,img = VInstance.read()

        # Confirm we have a valid image returned
        if not ret:
            break

        imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

        cv2.imshow("result",img)

        k = cv2.waitKey(1)
        if k == 27:
            break

    cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM