Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the conversion between Video and Image by OpenCV

Shulou Source: shulou.com Published: 2022-06-01 06:33:36 10月05日 Update

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!

Tags: Videos images articles between code content pictures data files starting point similar not much things most of which are buttons folders more knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Information Shulou Tech Info Redmi