获取openCV错误:断言失败

时间:2016-07-23 14:00:44

标签: python opencv raspberry-pi assertions

我在RaspberryPi中使用opencv 3.1 3.我试图运行以下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

但我收到Assertion Failed错误,详情如下。

  

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 || scn == 4 in function cvtColor

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

2 个答案:

答案 0 :(得分:1)

错误代码&#34;断言失败(scn == 3 || scn == 4)在cvtColor&#34;表示public class CompositeProduct` extends Product { private List<Product> products; public CompositeProduct(List<Product> products) { this.products = products } public String preview() { String previewText = ""; for(Product product : products) { previewText+=product.preview(); } return preview; } } 方法中的输入(源)图像没有3或4个通道,这是此类转换所必需的。可能您的输入图像已经是灰度格式。尽量不要使用该方法,您的问题应该得到解决。如果它确实抛出其他无法解决的错误或无法解决问题,请在评论中发布您的问题。

答案 1 :(得分:1)

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

错误和问题标题与你的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()