Home > database >  How to change color of circle in opencv while displaying from webcam feed?
How to change color of circle in opencv while displaying from webcam feed?

Time:03-31

I"m trying to change the color of a circle drawn on the output frame of the live camera feed whenever some key is pressed. When I press 'c' key I want the circle to change its color from blue to green. I'm trying the below approach to achieve this

# import the opencv library
import cv2
global circle_color
circle_color = (255,0,0)
# define a video capture object
vid = cv2.VideoCapture(0)
  
while(True):
      
    # Capture the video frame
    # by frame
    ret, frame = vid.read()
    cv2.circle(img=frame, center = (250,250), radius =100, color =circle_color, thickness=-1)

  
    # Display the resulting frame
    cv2.imshow('frame', frame)
      
    # the 'q' button is set as the
    # quitting button you may use any
    # desired button of your choice
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    elif cv2.waitKey(1) & 0xFF == ord('c'): 
        # circle_color == (0,255,0)
        cv2.circle(img=frame, center = (250,250), radius =100, color =(0,255,0), thickness=-1)
        print("pressed c")
  
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

When I try this code and press key 'c' I only see the console output as pressed c but I don't see any change in circle color on the output frame window it stays blue.

I even tried declaring global variable of color and updating it when the key is pressed this didn't work as well

Any help or suggestion to achieve this will be highly appreciated thanks.

CodePudding user response:

The main issue is that your green circle is always covered by the blue circle before the image is shown.

Move the cv2.imshow('frame', frame) to the end of the while loop.

I would do it like this:

# import the opencv library
import cv2

key_pressed = False

# define a video capture object
vid = cv2.VideoCapture(0)
  
while(True):
      
    # Capture the video frame
    # by frame
    ret, frame = vid.read()
    circle_color = (0, 255, 0) if key_pressed else (255, 0, 0)
    cv2.circle(img=frame, center = (250,250), radius =100, color = circle_color, thickness=-1)
      
    # the 'q' button is set as the
    # quitting button you may use any
    # desired button of your choice
    
    key = cv2.waitKey(1) 
    if key & 0xFF == ord('q'):
        break
    elif key & 0xFF == ord('c'): 
        # circle_color == (0,255,0)
        key_pressed = True
        print("pressed c")
    else:
        key_pressed = False
    
    # Display the resulting frame
    cv2.imshow('frame', frame)
  
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()

If you want to keep the color even after you release the pressed key, you should just overwrite circle_color:

# import the opencv library
import cv2

# define a video capture object
vid = cv2.VideoCapture(0)

circle_color = (255, 0, 0)

while(True):
      
    # Capture the video frame
    # by frame
    ret, frame = vid.read()
    
    cv2.circle(img=frame, center = (250,250), radius =100, color = circle_color, thickness=-1)
      
    # the 'q' button is set as the
    # quitting button you may use any
    # desired button of your choice
    
    key = cv2.waitKey(1) 
    if key & 0xFF == ord('q'):
        break
    elif key & 0xFF == ord('c'): 
        circle_color = (0, 255, 0) 
        print("pressed c")
    
    # Display the resulting frame
    cv2.imshow('frame', frame)
  
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
  • Related