比力差异 图片 视频

[复制链接]
发表于 2026-1-26 11:02:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
两张图片像素差:
深度图和rgb图对齐
视频比力差异:
结构化(1行)贴到深度图上(5行):


两张图片像素差:

  1. diff=np.clip(np.abs( img_mask.astype(np.int16))-img.astype(np.int16), 0, 255).astype(np.uint8)
复制代码

深度图和rgb图对齐

  1. # -*- coding: utf-8 -*-
  2. import glob
  3. import os
  4. import cv2
  5. from moviepy.editor import VideoFileClip
  6. import numpy as np
  7. from natsort import natsorted
  8. from moviepy.editor import ImageSequenceClip
  9. def check_file_size(file_path, size_level=0.5 * 1000):
  10.     file_size = os.path.getsize(file_path)
  11.     if file_size < size_level:
  12.         return False, file_size
  13.     return True, file_size
  14. if __name__ == '__main__':
  15.     dir_a = r'C:\Users\Administrator\Downloads\v_turn_left\v_turn_left'
  16.     dir_a = r'C:\Users\Administrator\Downloads\v_turn_left_box\v_turn_left'
  17.     dir_a = r'C:\Users\Administrator\Downloads\v_turn_right\v_turn_right'
  18.     dir_a = r'C:\Users\Administrator\Downloads\HLX33B12XP07413111706257257828\HLX33B12XP07413111706257257828\trajectory_data\v_turn_left'
  19.     dir_a = r'C:\Users\Administrator\Downloads\v_turn_left_rgb\v_turn_left'
  20.     dirs = glob.glob(dir_a+ '/*')
  21.     out_dir_base = f'{os.path.dirname(dir_a)}/pinjie/'
  22.     os.makedirs(out_dir_base, exist_ok=True)
  23.     for dir_m in dirs:
  24.         if not os.path.isdir(dir_m):
  25.             continue
  26.         img_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'))]
  27.         img_files=natsorted(img_files)
  28.         out_dir=out_dir_base+ os.path.basename(dir_m)
  29.         imgs=[]
  30.         for img_i, file in enumerate(img_files):
  31.             print(img_i, file)
  32.             img_ = cv2.imread(file)
  33.             lane_path=file.replace('_mask.jpg','.png')
  34.             lane_img=cv2.imread(lane_path)
  35.             lane_img = cv2.cvtColor(lane_img, cv2.COLOR_BGR2RGB)
  36.             pic_w = 960
  37.             pic_h = 576
  38.             # 加载背景图片和前景图片
  39.             background = img_.copy()
  40.             foreground =lane_img
  41.             h, w = foreground.shape[:2]
  42.             # 创建前景图片的掩码,掩码区域为黑色部分
  43.             # 这里我们假设前景图的黑色部分是完全黑色(RGB值全为0)
  44.             black_mask = cv2.inRange(foreground, np.array([0, 0, 0]), np.array([10, 10, 10]))
  45.             # 创建反掩码
  46.             foreground_mask = cv2.bitwise_not(black_mask)
  47.             # 使用掩码将前景图片中的黑色部分去除
  48.             foreground_no_black = cv2.bitwise_and(foreground, foreground, mask=foreground_mask)
  49.             # 获取前景图片的区域
  50.             roi = background[0:h, 0:w]
  51.             # 创建背景的反掩码
  52.             background_mask = cv2.bitwise_not(foreground_mask)
  53.             # 使用反掩码将背景图片的对应区域去除
  54.             background_no_foreground = cv2.bitwise_and(roi, roi, mask=background_mask)
  55.             # 将前景图片粘贴到背景图片
  56.             result = cv2.add(background_no_foreground, foreground_no_black)
  57.             background[0:h, 0:w] = result
  58.             # cv2.imshow('background', background)
  59.             # cv2.imshow('foreground', foreground)
  60.             new_img = np.vstack((foreground,img_, background))
  61.             new_img=cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB)
  62.             imgs.append(new_img)
  63.             if 0:
  64.                 if max(new_img.shape[:2]) > 1200:
  65.                     x_scale = 1200 / max(new_img.shape[:2])
  66.                     new_img = cv2.resize(new_img, None, fx=x_scale, fy=x_scale, interpolation=cv2.INTER_AREA)
  67.                 cv2.imwrite(out_dir+os.path.basename(file), new_img)
  68.                 cv2.imshow('rss', new_img)
  69.                 cv2.waitKey(1)
  70.         image_sequence_clip = ImageSequenceClip(imgs, fps=6)
  71.         # 输出为视频文件
  72.         image_sequence_clip.write_videofile(out_dir+".mp4", codec="libx264")
复制代码

视频比力差异:

  1. import glob
  2. import os
  3. from moviepy.editor import VideoFileClip
  4. import numpy as np
  5. def check_file_size(file_path, size_level=0.5 * 1000):
  6.     file_size = os.path.getsize(file_path)
  7.     if file_size < size_level:
  8.         return False, file_size
  9.     return True, file_size
  10. def process_frame(frame):
  11.     # 获取帧的宽度和高度
  12.     height, width, _ = frame.shape
  13.     width_part=960
  14.     h_part=576
  15.     # 前两列: 左半部分 (前 width // 2 列)
  16.     part1_2 = frame[h_part:h_part*2, :width_part, :]
  17.     part1_3 = frame[h_part*2:h_part*3, :width_part, :]
  18.     part2_2 = frame[h_part:h_part*2, width_part:width_part*2, :]
  19.     part2_3 = frame[h_part*2:h_part*3, width_part:width_part*2, :]
  20.     part3_2 = frame[h_part:h_part*2, width_part*2:width_part*3, :]
  21.     part3_3 = frame[h_part*2:h_part*3,width_part*2:width_part*3, :]
  22.     pic_right=np.zeros((2880,960,3),dtype=np.uint8)
  23.     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)
  24.     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)
  25.     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)
  26.     # 将原帧与差值列拼接
  27.     new_frame = np.concatenate((frame, pic_right), axis=1)
  28.     return new_frame
  29. dir_ar=r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_6f_150_depth_nomask,mask_e20_e30\aa'
  30. fils=glob.glob(os.path.join(dir_ar,'*.mp4'))
  31. out_dir=r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_6f_150_depth_nomask,mask_e20_e30/diff/'
  32. os.makedirs(out_dir,exist_ok=True)
  33. for file in fils:
  34.     size_ok,_= check_file_size(file)
  35.     if size_ok:
  36.         video = VideoFileClip(file)
  37.         # 对每一帧应用帧处理函数
  38.         new_video = video.fl_image(process_frame)
  39.         # 保存处理后的视频
  40.         new_video.write_videofile(out_dir+os.path.basename(file))
复制代码

结构化(1行)贴到深度图上(5行):

  1. import glob
  2. import os
  3. import cv2
  4. from moviepy.editor import VideoFileClip
  5. import numpy as np
  6. def check_file_size(file_path, size_level=0.5 * 1000):
  7.     file_size = os.path.getsize(file_path)
  8.     if file_size < size_level:
  9.         return False, file_size
  10.     return True, file_size
  11. def process_frame(frame):
  12.     pic_w=960
  13.     pic_h=576
  14.     # 加载背景图片和前景图片
  15.     background =frame[pic_h*4:] .copy()
  16.     foreground =frame[:pic_h] .copy()
  17.     h, w = foreground.shape[:2]
  18.     # 创建前景图片的掩码,掩码区域为黑色部分
  19.     # 这里我们假设前景图的黑色部分是完全黑色(RGB值全为0)
  20.     black_mask = cv2.inRange(foreground, np.array([0, 0, 0]), np.array([10, 10, 10]))
  21.     # 创建反掩码
  22.     foreground_mask = cv2.bitwise_not(black_mask)
  23.     # 使用掩码将前景图片中的黑色部分去除
  24.     foreground_no_black = cv2.bitwise_and(foreground, foreground, mask=foreground_mask)
  25.     # 获取前景图片的区域
  26.     roi = background[0:h, 0:w]
  27.     # 创建背景的反掩码
  28.     background_mask = cv2.bitwise_not(foreground_mask)
  29.     # 使用反掩码将背景图片的对应区域去除
  30.     background_no_foreground = cv2.bitwise_and(roi, roi, mask=background_mask)
  31.     # 将前景图片粘贴到背景图片
  32.     result = cv2.add(background_no_foreground, foreground_no_black)
  33.     background[0:h, 0:w] = result
  34.     pic_right = np.zeros((pic_h*5, pic_w, 3), dtype=np.uint8)
  35.     pic_right[pic_h*2:pic_h*3] = frame[pic_h*3:pic_h*4]
  36.     pic_right[pic_h*4:] = background
  37.     new_frame = np.concatenate((frame, pic_right), axis=1)
  38.     top_part = new_frame[:pic_h*3]
  39.     bottom_part = new_frame[pic_h*4:]
  40.     # 将上半部分和下半部分拼接
  41.     new_img = np.vstack((top_part, bottom_part))
  42.     # cv2.imshow('frame', new_img)
  43.     # cv2.waitKey(0)
  44.     return new_img
  45.     # 获取帧的宽度和高度
  46.     height, width, _ = frame.shape
  47.     width_part=960
  48.     h_part=576
  49.     # 前两列: 左半部分 (前 width // 2 列)
  50.     part1_2 = frame[h_part:h_part*2, :width_part, :]
  51.     part1_3 = frame[h_part*2:h_part*3, :width_part, :]
  52.     part2_2 = frame[h_part:h_part*2, width_part:width_part*2, :]
  53.     part2_3 = frame[h_part*2:h_part*3, width_part:width_part*2, :]
  54.     part3_2 = frame[h_part:h_part*2, width_part*2:width_part*3, :]
  55.     part3_3 = frame[h_part*2:h_part*3,width_part*2:width_part*3, :]
  56.     pic_right=np.zeros((2880,960,3),dtype=np.uint8)
  57.     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)
  58.     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)
  59.     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)
  60.     # 将原帧与差值列拼接
  61.     new_frame = np.concatenate((frame, pic_right), axis=1)
  62.     return new_frame
  63. if __name__ == '__main__':
  64.     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'
  65.     mp4s=glob.glob(os.path.join(dir_a,'*.mp4'))
  66.     out_dir = r'C:\Users\Administrator\Downloads\liauto_fv_960_arrow_36_9f_150_depth_real_e20_test/pinjie/'
  67.     os.makedirs(out_dir, exist_ok=True)
  68.     for file in mp4s:
  69.         size_ok, _ = check_file_size(file)
  70.         if size_ok:
  71.             video = VideoFileClip(file)
  72.             # 对每一帧应用帧处理函数
  73.             new_video = video.fl_image(process_frame)
  74.             # 保存处理后的视频
  75.             new_video.write_videofile(out_dir + os.path.basename(file))
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表