[Opencv-python] Gui in OpenCV (v) video

Source: Internet
Author: User
Tags ord

5 Video


Goal
? Learn to read video files, display videos, save video files
? Learn to get and display video from the camera
? You will learn these functions: Cv2. Videocapture (), Cv2. Videowrite ()


5.1 Capturing video with the camera
We often need to use the camera to capture live images. The OpenCV provides this application with a
A very simple interface. Let's use the camera to capture a video and convert it into grayscale video
Show up. Let's start with this simple task.
In order to get the video, you should create a Videocapture object. His parameters could be
The index number of the device, or a video file. The device index number is the camera that you specify to use.
Typical laptops have built-in cameras. So the parameter is 0. You can do this by setting it to 1 or
Others to choose a different camera. After that, you can capture the video in one frame at a frame. But the most
, don't forget to stop capturing the video.

ImportNumPy as NPImportCv2cap=Cv2. Videocapture (0) while(True):#Capture Frame-by-frameRET, frame =Cap.read ()#Our operations in the frame come hereGray =Cv2.cvtcolor (frame, cv2. Color_bgr2gray)#Display the resulting frameCv2.imshow ('Frame', Gray)ifCv2.waitkey (1) & 0xFF = = Ord ('Q'):         Break#When everything-done, release the capturecap.release () cv2.destroyallwindows ( )

Cap.read () returns a Boolean value (True/false). If the frame is read correctly,
Is True. So finally you can check his return value to see if the video file has reached
Finish the tail.
Sometimes the cap may not successfully initialize the camera device. In this case, the above code will report
Wrong. You can use cap.isopened () to check whether the initialization was successful. If the return value is
True, then there is no problem. Otherwise you will use the function Cap.open ().
You can use the function Cap.get (propid) to get some parameter information for the video. Over here
Propid can be any integer between 0 and 18. Each number represents a property of the video, see
The following table
Some of these values can be modified using Cap.set (Propid,value), and value is
The new value you want to set.
For example, I can use Cap.get (3) and Cap.get (4) to see the width and height of each frame.
The value obtained by default is 640x480. But I can use Ret=cap.set (3,320)
and Ret=cap.set (4,240) to change the width and height into a.
Note: When your program is an error, you should first check if your webcam is able to
(such as Cheese under Linux).


5.2 Playing video from a file
As with capturing from the camera, you only need to change the device index number to the name of the video file. In
Use Cv2.waikey () to set the appropriate duration when each frame is played. If you set the too low view
The frequency will play very fast, if set too high will play very slowly (you can use this method
Controls the playback speed of the video). Typically, 25 milliseconds is the case.

ImportNumPy as NPImportCv2cap= Cv2. Videocapture ('Vtest.avi') while(cap.isopened ()): RET, Frame=Cap.read () Gray=Cv2.cvtcolor (frame, cv2. Color_bgr2gray) Cv2.imshow ('Frame', Gray)ifCv2.waitkey (1) & 0xFF = = Ord ('Q'):         Breakcap.release () cv2.destroyallwindows ( )

Note: You should make sure that you have installed the appropriate version of FFmpeg or GStreamer. If you install
Wrong, it's more headache.


5.3 Save Video
We want to save this video after we capture the video and process each frame. Right
It is simple to use cv2.imwrite () when it comes to pictures. But for the video, we need to do more work.
For
This time we're going to create a Videowriter object. We should determine an output file
's name. Next, specify the FourCC encoding (described below). Both the playback frequency and the frame size are also
Need to be determined. The last one is the iscolor tag. If True, each frame is a color map, no
is the grayscale image.
FourCC is a 4-byte code used to determine the encoding format of the video. List of available encodings
Can be found from the fourcc.org. This is platform-dependent. The following encoders are useful for me.
? In Fedora:divx, XVID, MJPG, X264, WMV1, WMV2. (XVID is
More preferable. MJPG results in high size video. X264 gives
Very small size video)
? In Windows:divx (tested and added)
? In OSX: (I don ' t has access to OSX. Can Some one fill this?)
FourCC code is passed to the program in the following format, taking MJPG as an example:
Cv2.cv.FOURCC (' M ', ' J ', ' P ', ' G ') or Cv2.cv.FOURCC (* ' MJPG ').


The following code captures the video from the camera, rotates each frame horizontally and saves it.

ImportNumPy as NPImportCv2cap=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 ()ifret==True:frame=Cv2.flip (frame,0)#write the flipped frameOut.write (frame) cv2.imshow ('Frame', frame)ifCv2.waitkey (1) & 0xFF = = Ord ('Q'):             Break    Else:         Break#Release Everything if job is finishedcap.release () out.release () cv2.destroyallwindows ( )


More Resources
Practice

[Opencv-python] Gui in OpenCV (v) video

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.