Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize OpenCV4.X Video capture and display

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to achieve OpenCV4.X video capture and display. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

I. Video acquisition and display

The VideoCapture class implements video capture, which can be read from a camera or file. First, create a VideoCapture object, which can be the device index (camera index) or the name of the video file. If it is a device index, when only one camera is connected, it can be 0 or-1, or you can select the second camera by passing 1. Then, the video image frame can be obtained frame by frame through the object. Finally, release the VideoCapture object.

Displays video image frames, and the opencv functions involved include:

Create an object: cap = cv2.VideoCapture (0)

Open the video for initialization: cap.open ()

Determine whether initialization is successful: cap.isOpened ()

Read video frames: cap.read () returns whether there are image frames and image frame data.

Get video attributes: cap.get (propld) propId from 0 to 18, and each number represents a video attribute.

Set video attribute values: some of the above attribute values of cap.set (propId, value) can be set through this function, and value represents the updated value.

Video object resource release: cap.release ()

Take the video from the camera and convert it to grayscale video for display. Import cv2

Import numpy as np

# obtain video from camera ID and change it from file to video file path

Cap = cv2.VideoCapture (0)

If not cap.isOpened ():

Print ("Cannot open camera")

Exit ()

While (True):

# read video z in one frame

Ret, frame = cap.read ()

# every frame is processed.

Gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY)

# display processed images

Cv2.imshow ('frame', gray)

# if the key'QQ'is detected, exit

If cv2.waitKey (1) & 0xFF = = ord ('q'):

Break

# When everything done, release the capture

Cap.release () # release the camera

Cv2.destroyAllWindows () # Delete all windows

II. Video preservation

The VideoWriter class implements video preservation. First, create a VideoWriter object and specify the output file name (for example: output.avi). Then specify the FourCC code, the frame rate (FPS), the height and width of the frame, and the isColor flag, and if it is True, the encoder needs a color frame, otherwise it is suitable for grayscale frames.

FourCC

Use four characters to represent compressed frame coding, and you can find a list of available codes in fourcc.org. It depends on the platform.

In Fedora: DIVX,XVID,MJPG,X264,WMV1,WMV2. (XVID is preferable. MJPG produces high-size videos. X264 can provide very small videos) in Windows: DIVX (more to test and add) in OSX: MJPG (.mp4), DIVX (.avi), X264 (.mkv)

The FourCC code is passed through cv2.VideoWriter_fourcc (), such as:

For MJPG,FourCC code, it is passed as cv2.VideoWriter_fourcc ('Muggle MJPG'') or cv2.VideoWriter_fourcc (* 'MJPG').

Get the video from the file, flip each frame vertically and save it. Import cv2

Cap = cv2.VideoCapture ('input.avi')

# create VideoWriter object and set fourcc

Fourcc = cv2.VideoWriter_fourcc (* 'XVID')

Out = cv2.VideoWriter ('output.avi', fourcc, 20.0,640,480)

While cap.isOpened ():

Ret, frame = cap.read ()

If not ret:

Print ("Can't receive frame (stream end?). Exiting...")

Break

Frame = cv2.flip (frame, 0)

# the processed frame is written to the object

Out.write (frame)

Cv2.imshow ('frame', frame)

If cv2.waitKey (1) & 0xFF = = ord ('q'):

Break

# release all objects and resources

Cap.release ()

Out.release ()

Cv2.destroyAllWindows ()

III. Application

Preprocess video: capture image frames from the video file every 5 frames, flip the horizontal mirror and save it to a folder.

Import cv2

Cap = cv2.VideoCapture ('input.avi')

Num = 0

While cap.isOpened ():

Ret, frame = cap.read ()

If ret:

If num% 5 = 0:

Frame = cv2.flip (frame, 1)

Cv2.imwrite ('frame_%d.jpg'% num, frame)

If cv2.waitKey (1) & 0xFF = = ord ('q'):

Break

Num + = 1

# release all objects and resources

Cap.release ()

Out.release ()

Cv2.destroyAllWindows () the above is the editor for you to share how to achieve OpenCV4.X video capture and display, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report