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 the conversion between Video and Image by OpenCV

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

Share

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

Editor to share with you how to achieve video and image conversion between OpenCV, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

I. converting video to image

Because the image similarity of the adjacent frames of continuous video is very high, which is not a good thing for the data set, so the images can be taken from other frames. In the following example, I just take it every 10 frames.

The Python code is as follows:

Import cv2def video2images (Video_Dir): "" function: video to pictures author: AIJun date:2021/3/17 "" cap = cv2.VideoCapture (Video_Dir) c = 1 # frame count start index = 1 # picture naming start, such as 1.jpgif not cap.isOpened (): print ("Cannot open camera") exit () while True:# captures ret frame by frame, frame = cap.read () # if the frame is read correctly Ret is Trueif not ret:print ("Can't receive frame (stream end?)." Exiting. ") breakif c% 10 = = 0:cv2.imwrite ('pictures/' + str (index) +' .jpg', frame) index + = 1c + = 1cv2.waitKey (1) # button to stop if cv2.waitKey (1) = = ord ('q'): breakcap.release () Video_Dir =" D:\ dataset\ data3_16\ WIN_20210316_17_46_54_Pro.mp4 "video2images (Video_Dir) 2. Image synthesis video

The following shows that continuous pictures are combined into a video with a frame rate of 24. The Python code is as follows:

Import globimport osimport cv2def images2video (image_dir, save_name): fps = 24fourcc = cv2.VideoWriter_fourcc video_w = cv2.VideoWriter (save_name, fourcc, fps, (1920, 1080)) # scan all jpg files in the folder images = glob.glob (os.path.join (image_dir, "* .jpg") for i in range (1) Len (images): # Select the name "{} .jpg" .format (1) = 1.jpgimage_name = os.path.join (image_dir, "{} .jpg" .format (I)) frame = cv2.imread (image_name) video_w.write (frame) video_w.release () image_dir = "D:\ Project\ VideoToImage\ pictures" save_name = "test.avi" images2video (image_dir) Save_name) these are all the contents of the article "how does OpenCV realize the conversion between video and image" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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