视频比较差异:
import glob
import os
from moviepy.editor import VideoFileClip
import numpy as np
def check_file_size(file_path, size_level=0.5 * 1000):
file_size = os.path.getsize(file_path)
if file_size < size_level:
return False, file_size
return True, file_size
def process_frame(frame):
# 获取帧的宽度和高度
height, width, _ = frame.shape
width_part=960
h_part=576
# 前两列: 左半部分 (前 width // 2 列)
part1_2 = frame[h_part:h_part*2, :width_part, :]
part1_3 = frame[h_part*2:h_part*3, :width_part, :]
part2_2 = frame[h_part:h_part*2, width_part:width_part*2, :]
part2_3 = frame[h_part*2:h_part*3, width_part:width_part*2, :]
part3_2 = frame[h_part:h_part*2, width_part*2:width_part*3, :]
part3_3 = frame[h_part*2:h_part*3,width_part*2:width_part*3, :]
pic_right=np.zeros((2880,960,3),dtype=np.uint8)
pic_right[h_part:h_part*2]= np.clip(np.abs(part1_2.astype(np.int16) - part1_3.astype(np.int16)), 0, 255).astype(np.uint8)
pic_right[h_part*2:h_part*3]= np.clip(np.abs(part2_2.astype(np.int16) - part2_3.astype(np.int16)), 0, 255).astype(np.uint8)
pic_right[h_part*3:4*h_part]= np.clip(np.abs(part3_2.astype(np.int16) - part3_3.astype(np.int16)), 0, 255).astype(np.uint8)
# 将原帧与差值列拼接
new_frame = np.concatenate((frame, pic_right), axis=1)
return new_frame
dir_ar=r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_6f_150_depth_nomask,mask_e20_e30\aa'
fils=glob.glob(os.path.join(dir_ar,'*.mp4'))
out_dir=r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_6f_150_depth_nomask,mask_e20_e30/diff/'
os.makedirs(out_dir,exist_ok=True)
for file in fils:
size_ok,_= check_file_size(file)
if size_ok:
video = VideoFileClip(file)
# 对每一帧应用帧处理函数
new_video = video.fl_image(process_frame)
# 保存处理后的视频
new_video.write_videofile(out_dir+os.path.basename(file))
拼接2:
import glob
import os
import cv2
from moviepy.editor import VideoFileClip
import numpy as np
def check_file_size(file_path, size_level=0.5 * 1000):
file_size = os.path.getsize(file_path)
if file_size < size_level:
return False, file_size
return True, file_size
def process_frame(frame):
pic_w=960
pic_h=576
# 加载背景图片和前景图片
background =frame[pic_h*4:] .copy()
foreground =frame[:pic_h] .copy()
h, w = foreground.shape[:2]
# 创建前景图片的掩码,掩码区域为黑色部分
# 这里我们假设前景图的黑色部分是完全黑色(RGB值全为0)
black_mask = cv2.inRange(foreground, np.array([0, 0, 0]), np.array([10, 10, 10]))
# 创建反掩码
foreground_mask = cv2.bitwise_not(black_mask)
# 使用掩码将前景图片中的黑色部分去除
foreground_no_black = cv2.bitwise_and(foreground, foreground, mask=foreground_mask)
# 获取前景图片的区域
roi = background[0:h, 0:w]
# 创建背景的反掩码
background_mask = cv2.bitwise_not(foreground_mask)
# 使用反掩码将背景图片的对应区域去除
background_no_foreground = cv2.bitwise_and(roi, roi, mask=background_mask)
# 将前景图片粘贴到背景图片
result = cv2.add(background_no_foreground, foreground_no_black)
background[0:h, 0:w] = result
pic_right = np.zeros((pic_h*5, pic_w, 3), dtype=np.uint8)
pic_right[pic_h*2:pic_h*3] = frame[pic_h*3:pic_h*4]
pic_right[pic_h*4:] = background
new_frame = np.concatenate((frame, pic_right), axis=1)
top_part = new_frame[:pic_h*3]
bottom_part = new_frame[pic_h*4:]
# 将上半部分和下半部分拼接
new_img = np.vstack((top_part, bottom_part))
# cv2.imshow('frame', new_img)
# cv2.waitKey(0)
return new_img
# 获取帧的宽度和高度
height, width, _ = frame.shape
width_part=960
h_part=576
# 前两列: 左半部分 (前 width // 2 列)
part1_2 = frame[h_part:h_part*2, :width_part, :]
part1_3 = frame[h_part*2:h_part*3, :width_part, :]
part2_2 = frame[h_part:h_part*2, width_part:width_part*2, :]
part2_3 = frame[h_part*2:h_part*3, width_part:width_part*2, :]
part3_2 = frame[h_part:h_part*2, width_part*2:width_part*3, :]
part3_3 = frame[h_part*2:h_part*3,width_part*2:width_part*3, :]
pic_right=np.zeros((2880,960,3),dtype=np.uint8)
pic_right[h_part:h_part*2]= np.clip(np.abs(part1_2.astype(np.int16) - part1_3.astype(np.int16)), 0, 255).astype(np.uint8)
pic_right[h_part*2:h_part*3]= np.clip(np.abs(part2_2.astype(np.int16) - part2_3.astype(np.int16)), 0, 255).astype(np.uint8)
pic_right[h_part*3:4*h_part]= np.clip(np.abs(part3_2.astype(np.int16) - part3_3.astype(np.int16)), 0, 255).astype(np.uint8)
# 将原帧与差值列拼接
new_frame = np.concatenate((frame, pic_right), axis=1)
return new_frame
if __name__ == '__main__':
dir_a=r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_9f_150_depth_real_e20_test\liauto_fv_960_arrow_36_9f_150_depth_real_e20_test'
mp4s=glob.glob(os.path.join(dir_a,'*.mp4'))
out_dir = r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_9f_150_depth_real_e20_test/pinjie/'
os.makedirs(out_dir, exist_ok=True)
for file in mp4s:
size_ok, _ = check_file_size(file)
if size_ok:
video = VideoFileClip(file)
# 对每一帧应用帧处理函数
new_video = video.fl_image(process_frame)
# 保存处理后的视频
new_video.write_videofile(out_dir + os.path.basename(file))