出现openCV错误:断言失败 [英] Getting openCV error: Assertion Failed

查看:408
本文介绍了出现openCV错误:断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RaspberryPi 3中使用opencv 3.1.我正在尝试运行以下Hough Circle检测算法

I'm using opencv 3.1 in RaspberryPi 3. I,m trying to run the following Hough Circle detection algorithm

#! /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错误:在cvtColor中断言失败(scn == 3 || scn == 4), 文件/home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp,第8000行 追溯(最近一次通话):文件"HoughCircles.py",第70行, 在 imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)cv2.error:/home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8000:错误: (-215)scn == 3 ||函数cvtColor中的scn == 4

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

任何人都可以检查和帮助!

Can anyone please check and help!

推荐答案

这意味着输入图像无效,这就是为什么您需要在循环中检查ret值的原因!

This means the input image is invalid, which is why you need to check the ret value in your loop!

错误和问题标题与您的Hough圈子没有任何关系,因此我将浓缩我的答案以解决断言失败(稍后再添加到您的内容中!):

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!):

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

这篇关于出现openCV错误:断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆