2016-07-23 24 views
1

Ich verwende opencv 3.1 in Raspberry Pi 3. Ich, versuche die folgenden Hough Kreis-ErkennungsalgorithmusErste OpenCV Fehler: Assertionsfehler

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

Aber ich bin immer Assertionsfehler Fehler, Details sind unten zu laufen .

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

Kann jemand bitte überprüfen und helfen!

Antwort

1

Fehlercode "Assertion fehlgeschlagen (scn == 3 || scn == 4) in cvtColor" bedeutet, dass das Eingabe- (Quell-) Bild in Ihrer cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)-Methode nicht 3 oder 4 Kanäle hat, die für diesen Typ erforderlich sind der Umwandlung. Wahrscheinlich ist Ihr Eingabebild bereits im Graustufenformat. Versuchen Sie nur, diese Methode nicht zu verwenden und Ihr Problem sollte gelöst werden. Wenn es andere unlösbare Fehler wirft oder das Problem nicht löst, poste deine Probleme in Kommentaren.

+0

Dank Dainius, wird es versuchen und dich wissen lassen. –

0

Dies bedeutet, dass das Eingabebild ungültig ist, weshalb Sie den Wert ret in Ihrem Loop überprüfen müssen!

Der Fehler und Frage Titel mit Hough Kreisen nichts zu tun haben, also werde ich meine Antwort kondensieren die Assertionsfehler zu adressieren (später wieder hinzufügen in Ihrem Material!):

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