简体   繁体   中英

OpenCV Python - Draw circle at random position in webcam video feed

I have the following code:

import cv2
from random import randrange

cap = cv2.VideoCapture(0) #record webcam

while True:
    ret, frame = cap.read() #get feed from webcam
    h, w = frame.shape[:2] #height, width

    randomWidth = randrange(30,w-30)
    randomHeight = randrange(70,h-30)
    coordinates = (randomWidth, randomHeight)
    circle = cv2.circle(frame, coordinates, 30, (0,0,255), -1) #circle

    cv2.imshow('webcam', frame) #show webcam
    if cv2.waitKey(1) == ord('q'): #press 'q' to close
        break

cap.release()
cv2.destroyAllWindows()

My goal is to have the circle displayed at a random position every time I run the program. However, with this current code, the circle is jumping all over the screen, being drawn at a random position with every frame. How can I modify this code so that the circle is drawn at a random width and height and stays in that position until I exit the program?

You are recalculating the coordinates in each iteration, which is why the circle keeps moving around. You want to only calculate coordinates once and then keep using that value. Here is one way to do so:

import cv2
from random import randrange

cap = cv2.VideoCapture(0) #record webcam

ret, frame = cap.read() #get feed from webcam
h, w = frame.shape[:2] #height, width
randomWidth = randrange(30, w-30)
randomHeight = randrange(70, h-30)
coordinates = (randomWidth, randomHeight)

while True:
    ret, frame = cap.read() #get feed from webcam
    circle = cv2.circle(frame, coordinates, 30, (0,0,255), -1) #circle

    cv2.imshow('webcam', frame) #show webcam
    if cv2.waitKey(1) == ord('q'): #press 'q' to close
        break

cap.release()
cv2.destroyAllWindows()

The frame is fetched once before the loop, used to get random coordinates and then the loop continues to update the frame with the circle being at fixed coordinates.

In case your requirement may involve a changing frame.shape or there is some situation where coordinates may need to be recalculated in the loop, using a condition like below can be more flexible because coordinates is None can be replaced with different criteria:

import cv2
from random import randrange

cap = cv2.VideoCapture(0) #record webcam
coordinates = None

while True:
    ret, frame = cap.read() #get feed from webcam
    
    if coordinates is None:
        h, w = frame.shape[:2] #height, width
        randomWidth = randrange(30, w-30)
        randomHeight = randrange(70, h-30)
        coordinates = (randomWidth, randomHeight)
    
    circle = cv2.circle(frame, coordinates, 30, (0,0,255), -1) #circle

    cv2.imshow('webcam', frame) #show webcam
    if cv2.waitKey(1) == ord('q'): #press 'q' to close
        break

cap.release()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM