目录
两张图片像素差:
深度图和rgb图对齐
视频比较差异:
结构化(1行)贴到深度图上(5行):
两张图片像素差:
diff=np.clip(np.abs( img_mask.astype(np.int16))-img.astype(np.int16), 0, 255).astype(np.uint8)
深度图和rgb图对齐
# -*- coding: utf-8 -*-
import glob
import osimport cv2
from moviepy.editor import VideoFileClip
import numpy as np
from natsort import natsorted
from moviepy.editor import ImageSequenceClipdef 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_sizereturn True, file_sizeif __name__ == '__main__':dir_a = r'C:\Users\Administrator\Downloads\v_turn_left\v_turn_left'dir_a = r'C:\Users\Administrator\Downloads\v_turn_left_box\v_turn_left'dir_a = r'C:\Users\Administrator\Downloads\v_turn_right\v_turn_right'dir_a = r'C:\Users\Administrator\Downloads\HLX33B12XP07413111706257257828\HLX33B12XP07413111706257257828\trajectory_data\v_turn_left'dir_a = r'C:\Users\Administrator\Downloads\v_turn_left_rgb\v_turn_left'dirs = glob.glob(dir_a+ '/*')out_dir_base = f'{os.path.dirname(dir_a)}/pinjie/'os.makedirs(out_dir_base, exist_ok=True)for dir_m in dirs:if not os.path.isdir(dir_m):continueimg_files = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(dir_m) for j in i[-1] if j.endswith(('_mask.jpg', 'xpng', 'jpeg'))]img_files=natsorted(img_files)out_dir=out_dir_base+ os.path.basename(dir_m)imgs=[]for img_i, file in enumerate(img_files):print(img_i, file)img_ = cv2.imread(file)lane_path=file.replace('_mask.jpg','.png')lane_img=cv2.imread(lane_path)lane_img = cv2.cvtColor(lane_img, cv2.COLOR_BGR2RGB)pic_w = 960pic_h = 576# 加载背景图片和前景图片background = img_.copy()foreground =lane_imgh, 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# cv2.imshow('background', background)# cv2.imshow('foreground', foreground)new_img = np.vstack((foreground,img_, background))new_img=cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB)imgs.append(new_img)if 0:if max(new_img.shape[:2]) > 1200:x_scale = 1200 / max(new_img.shape[:2])new_img = cv2.resize(new_img, None, fx=x_scale, fy=x_scale, interpolation=cv2.INTER_AREA)cv2.imwrite(out_dir+os.path.basename(file), new_img)cv2.imshow('rss', new_img)cv2.waitKey(1)image_sequence_clip = ImageSequenceClip(imgs, fps=6)# 输出为视频文件image_sequence_clip.write_videofile(out_dir+".mp4", codec="libx264")
视频比较差异:
import glob
import osfrom moviepy.editor import VideoFileClip
import numpy as npdef 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_sizereturn True, file_sizedef process_frame(frame):# 获取帧的宽度和高度height, width, _ = frame.shapewidth_part=960h_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_framedir_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))
结构化(1行)贴到深度图上(5行):
import glob
import osimport cv2
from moviepy.editor import VideoFileClip
import numpy as npdef 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_sizereturn True, file_sizedef process_frame(frame):pic_w=960pic_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] = resultpic_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:] = backgroundnew_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.shapewidth_part=960h_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_frameif __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))