如何使用OpenCV、Python和Raspberry-Pi检测快速移动的足球?

2024-04-26 23:45:40 发布

您现在位置:Python中文网/ 问答频道 /正文

这是密码。我尝试使用OpenCV和python检测不同类型的足球。足球可以有不同的颜色。如果球不动,我能察觉到。但是,如果球移动很快,代码就不起作用了。在

from picamera.array import PiRGBArray
from picamera import PiCamerafrom picamera.array import PiRGBArray
from picamera import PiCamera
import time, cv2, sys, imutils, cv
import cv2.cv as cv

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (400, 300)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(400, 300))

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array
    img   = cv2.medianBlur(image, 3)
    imgg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #imgg  = cv2.blur(imgg, (3,3))
                #imgg = cv2.dilate(imgg, np.ones((5, 5)))
                #imgg = cv2.GaussianBlur(imgg,(5,5),0)
    circles = cv2.HoughCircles(imgg, cv.CV_HOUGH_GRADIENT, 1, 20, param1=100, param2=40, minRadius=5, maxRadius=90)

    cv2.imshow("Frame", imgg)

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    key = cv2.waitKey(1) & 0xFF    
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

    if circles is None:
        continue        

    for i in circles[0,:]:
        cv2.circle(imgg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
        cv2.circle(imgg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        cv2.imshow("Framed", imgg)

Tags: theinfromimportforarraycv2frame