参考OpenCV制作视频,自己加了一些图像拼接的代码段。
from os.path import isfile, join from imutils import paths import numpy as np import random import imutils import cv2 import os import matplotlib.pyplot as plt # define the aspect aware prepeocessor class class AspectAwarePreprocessor: def __init__(self, width, height, inter=cv2.INTER_AREA): # store the target image width, height, and interpolation # method used when resizing self.width = width self.height = height self.inter = inter def preprocess(self, image): # grab the dimensions of the image and then initialize # the deltas to use when cropping (h, w) = image.shape[:2] dW = 0 dH = 0 # if the width is smaller than the height, then resize # along the width (i.e., the smaller dimension) and then # update the deltas to crop the height to the desired # dimension if w < h: image = imutils.resize(image, width=self.width, inter=self.inter) dH = int((image.shape[0] - self.height) / 2.0) # otherwise, the height is smaller than the width so # resize along the height and then update the deltas # crop along the width else: image = imutils.resize(image, height=self.height, inter=self.inter) dW = int((image.shape[1] - self.width) / 2.0) # now that our images have been resized, we need to # re-grab the width and height, followed by performing # the crop (h, w) = image.shape[:2] image = image[dH:h - dH, dW:w - dW] # finally, resize the image to the provided spatial # dimensions to ensure our output image is always a fixed # size return cv2.resize(image, (self.width, self.height), interpolation=self.inter) def convert_frames_to_video(pathIn, pathOut, fps): # initialize the frame, width, and height array frame_array = [] width_array = [] height_array = [] left_files = list(paths.list_images(pathIn+'left/')) # 待拼接图像1 left_files.sort() right_files = list(paths.list_images(pathIn + 'right/')) # 待拼接图像2 right_files.sort() predicted_files = list(paths.list_images(pathIn + 'predicted/')) # 待拼接图像3 predicted_files.sort() assert len(left_files)==len(right_files) assert len(left_files) == len(predicted_files) print('length of files is ', len(left_files)) # initialize the image preprocessor and the target size aap = AspectAwarePreprocessor(1500, 2500) size = (1500, 2500) for i in range(len(left_files)): # filename=pathIn + files[i] # reading each files left_img = cv2.cvtColor(cv2.imread(left_files[i]), cv2.COLOR_BGR2RGB) right_img = cv2.cvtColor(cv2.imread(right_files[i]), cv2.COLOR_BGR2RGB) predicted_img = cv2.cvtColor(cv2.imread(predicted_files[i]), cv2.COLOR_BGR2RGB) plt.figure(figsize=(25, 15)) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) # plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) plt.subplots_adjust(hspace=0, wspace=0) plt.margins(0, 0) plt.subplot(311) plt.axis('off') plt.imshow(left_img) plt.title('NJUST on Cityscapes left image') plt.subplot(312) plt.axis('off') plt.imshow(right_img) plt.title('NJUST on Cityscapes right image') plt.subplot(313) plt.axis('off') plt.imshow(predicted_img) plt.title('NJUST on Cityscapes predicted image') plt.tight_layout() # 去掉空白区域 # plt.savefig('./NJUST_Experiments.png', bbox_inches='tight') plt.savefig('./NJUST_Experiments.png') plt.clf() plt.cla() plt.close("all") img = cv2.imread('./NJUST_Experiments.png') # 暂存临时图片 height, width, channels = img.shape # resize the image img = aap.preprocess(img) # save image to a certain directory f = left_files[i] file_name, file_ext = os.path.splitext(f) file_num = '{}'.format(i) file_num = file_num.zfill(5) new_name = 'demo' + file_num + file_ext path = '/data/zd/video_images/' cv2.imwrite(os.path.join(path, new_name), img) if i % 10 == 0: print("[INFO] Processing {}th image".format(i)) # inserting the frames into an image array frame_array.append(img) height_array.append(height) width_array.append(width) print('pathOut is ', pathOut) out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'mp4v'), fps, size) print('Process finished, begin writing ...') for i in range(len(frame_array)): # writing to a image array out.write(frame_array[i]) out.release() print('Done!') def main(): pathIn = '/data/zd/binocular-seg/val_results/' # 输入图像的路径 pathOut = '/data/zd/binocular-seg/binocular_seg_video.mp4' # 输出视频的路径 fps = 2.0 convert_frames_to_video(pathIn, pathOut, fps) if __name__ == "__main__": main() from os.path import i