0

Ich benutzte Python und opencv3, um das größte Bild auf Live-Stream zu konturieren, sieht es aus, dass mein Code falsch ging, kann mir jemand helfenIch habe Python und opencv3 verwendet, um das größte Bild im Live-Stream zu konturieren, sieht mein Code schief, kann mir jemand helfen

import cv2,platform 
import numpy as np 

def larger(x): 
    gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) 
    image = cv2.Canny(gray,35,200) 
    (_,cnts,_) = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
    c = max(cnts, key=cv2.contourArea) 
    return cv2.minAreaRect(c) 
capture = cv2.VideoCapture(0) 
retval, im = capture.read() 
y = larger(im) 
box = np.int0(cv2.boxPoints(y)) 
cv2.drawContours(im,[box],-1,(0,255,0),2) 
cv2.imshow("Image",im) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

ich habe zwei Probleme mit diesem Code:

1) is my web cam was on but cant see any video image, getting this error 

    "Traceback (most recent call last): 
     File "C:\Users\Snehith\Desktop\project vision\cnt.py", line 14, in <module> 
     y = larger(im) 
     File "C:\Users\Snehith\Desktop\project vision\cnt.py", line 8, in larger 
     c = max(cnts, key=cv2.contourArea) 
    ValueError: max() arg is an empty sequence" 
2) i want a continuous video stream along with the contour, please explain the mistakes and also solutions in python and opencv 

Antwort

0

ich hatte nur zwei kleine Änderungen an Ihrem Code zu tun:

1.) Sie können capture.isOpened überprüfen() in o rden Sie, ob eine Kamera angeschlossen ist.

2.) Ich habe eine While-Schleife eingefügt, so dass Sie kontinuierlich Frames ziehen, bis 'q' gedrückt wird.

import cv2,platform 
import numpy as np 

def larger(x): 
    gray = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY) 
    image = cv2.Canny(gray,35,200) 
    (_,cnts,_) = cv2.findContours(image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
    c = max(cnts, key=cv2.contourArea) 
    return cv2.minAreaRect(c) 

capture = cv2.VideoCapture(0) 

if capture.isOpened(): 
    while(True): 
     retval, im = capture.read() 
     y = larger(im) 
     box = np.int0(cv2.boxPoints(y)) 
     cv2.drawContours(im,[box],-1,(0,255,0),2) 
     cv2.imshow("Image",im) 
     if cv2.waitKey(1) & 0xFF == ord('q'): 
      capture.release() 
      break 

else: 
    print "No camera detected." 


cv2.destroyAllWindows()  

Das könnte dir die OpenCV Python Tutorial hilfreich für ähnliche Fragen finden, hier ist ein Link zur PDF-Version: https://media.readthedocs.org/pdf/opencv-python-tutroals/latest/opencv-python-tutroals.pdf

Verwandte Themen