The OPENCV version requires more than 2.3.1
This article focuses on how to read, save, and play a video stream in Python with OpenCV and use OpenCV to invoke the camera.
1. Call the camera
In order to capture the image of the camera, we first create a Videocapture object, which can be either a device number or a video file name. The device number can be viewed in/dev and the device name of my camera is video0, so the device number is 0. Once the Videocapture object has been obtained, we can get the image from one frame to the next through the read function of this object.
The effect of the following program is to obtain an image from the camera and convert it to a grayscale image, which is then displayed.
Import NumPy as Npimport Cv2cap = Cv2. Videocapture (0) while (True): # Capture Frame-by-frame ret, frame = Cap.read () # We operations on the frame Co Me here Gray = Cv2.cvtcolor (frame, cv2. Color_bgr2gray) # Display The resulting frame cv2.imshow (' frame ', gray) if Cv2.waitkey (1) & 0xFF = = Ord ( ' Q '): break# When everything done, release the Capturecap.release () cv2.destroyallwindows ()
where Cap.read () also returns a value of type bool that represents whether the returned frame image is correct. We can check this return value to determine if a video has been played to the head.
Sometimes, the cap will fail to initialize capture, and if we want to check if initialization is normal, we can use the cap.isopened () function. If true, it is opened with Cap.open ().
We can also get the properties of some videos through Cap.get (proid) and modify some properties through Cap.set (Proid,value). The value range for Proid is 0 to 18.
For example, we can get the frame width and frame height of the video through Cap.get (3) and Cap.get (4), and then modify them with the Get function.
2. Play a video file
The process of playing the video file is almost the same as calling the camera, but we have an important thing in calling the camera, which is that we have to set a suitable delay for cv.waitkey (), if the delay is too small, then the video will be very fast, if the delay is too large, then the video will become very slow ( This method can be used to see the slow motion), by setting this value of 1 is more appropriate.
Import NumPy as Npimport Cv2cap = Cv2. Videocapture (' Vtest.avi ') while (cap.isopened ()): ret, frame = Cap.read () Gray = Cv2.cvtcolor (frame, cv2. Color_bgr2gray) cv2.imshow (' frame ', gray) if Cv2.waitkey (1) & 0xFF = = Ord (' q '): breakcap.release () Cv2.destroyallwindows ()
3. Save the video file
The process of saving a video file is slightly more complex, first we need to determine some of the following parameters: The FOURCC format of the video, the size of the video, the number of frames per second of the video, and the color of the video. Then, based on these parameters, we generate a Videowriter object to hold the video file.
When setting the FOURCC format, CV2 is used in the original text. VIDEOWRITER_FOURCC () This function, but I am running the program when I show that this function does not exist, so I switched to the
Cv2.cv.CV_FOURCC this function. For FOURCC which format to use, please refer to the bottom of the text.
The example below buys a video from the camera and then holds the video upside down to save it.
Import NumPy as NPImport Cv2Cap = Cv2.videocapture(0)# Define The codec and create Videowriter objectFourCC = Cv2.VIDEOWRITER_FOURCC(*' XVID ') out = Cv2.Videowriter(' Output.avi ',FourCC, 20.0, (640,480)) while(Cap.isopened()): ret, Frame = Cap.Read() if ret==True: Frame = Cv2.Flip(Frame,0) # Write the flipped frame out.Write(Frame) Cv2.Imshow(' Frame ',Frame) if Cv2.Waitkey(1) & 0xFF == Ord(' Q '): Break Else: Break# Release Everything if job is finishedCap.Release() out.Release()Cv2.destroyallwindows()
Translated from Http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
PYTHON-OPENCV Tutorial-2