python cv2 camera capture video color grayscale

The camera captures color video:

import cv2
 # import numpy
 # import matplotlib.pyplot as plot
 # Create a camera object
 # Use the Videocapture() function that comes with opencv to define the camera object, 0 means the first camera, usually the camera in the notebook
 cap = cv2. VideoCapture ( 0 )
 # Display the video frame by frame
 while ( 1 ) :
 # Get the image frame by frame
     # cap.read() returns a boolean value (True/False).     # is True if the frame was read correctly
 . So finally you can see if the video file has reached the end by checking its return value
 ret,frame = cap.read()
     # Display the image frame by frame
 cv2.imshow ( " capture" ,frame)
     if cv2.waitKey (
            1)&0xFF == ord('q'):
break
cap.release()        
cap.destroyAllWindows()

Reference: https://blog.csdn.net/huanglu_thu13/article/details/52337013

The camera captures grayscale video:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret,frame = cap.read()
    # our operations on the frame come here
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    # display the resulting frame
cv2.imshow('fram',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break
# when everything done,release the capture
cap.release()

cv2.destroyAllwindows()

Reference: OpenCV-Python Chinese Tutorial (translated by Duan Lihui)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370450&siteId=291194637