比较差异 图片 视频

目录

两张图片像素差:

深度图和rgb图对齐

视频比较差异:

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


两张图片像素差:

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

深度图和rgb图对齐

python 复制代码
# -*- coding: utf-8 -*-
import glob
import os

import cv2
from moviepy.editor import VideoFileClip
import numpy as np
from natsort import natsorted
from moviepy.editor import ImageSequenceClip

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



if __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):
            continue
        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'))]

        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 = 960
            pic_h = 576
            # 加载背景图片和前景图片
            background = img_.copy()
            foreground =lane_img

            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

            # 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")

视频比较差异:

python 复制代码
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))

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

python 复制代码
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))
相关推荐
中式代码美式咖啡4 小时前
记录开发一个英语听力训练网站
java·spring boot·bootstrap·音视频·语音识别
神一样的老师8 小时前
使用卷积神经网络进行人类活动识别的特征学习:惯性测量单元和音频数据的案例研究
学习·cnn·音视频
一只小小程序猿9 小时前
Python计算机视觉编程 第四章 照相机模型与增强现实
python·计算机视觉
爱敲代码的小崔11 小时前
目标检测基本知识
人工智能·目标检测·计算机视觉
阿利同学11 小时前
停车位检测-停车场车位识别
人工智能·深度学习·机器学习·计算机视觉·视觉检测·车位识别·车位检测
985小水博一枚呀11 小时前
【梯度下降|链式法则】卷积神经网络中的参数是如何传输和更新的?
人工智能·python·深度学习·神经网络·机器学习·计算机视觉·cnn
李佩锦peijin13 小时前
YOLOv8目标检测模型——遥感小目标检测经验分享
人工智能·python·yolo·目标检测·计算机视觉
一只小小程序猿15 小时前
Python计算机视觉编程 第三章 图像到图像的映射
python·opencv·计算机视觉
Kuekua-seu15 小时前
文生视频算法
算法·音视频
PlumCarefree15 小时前
基于鸿蒙API10的RTSP播放器(五:拖动底部视频滑轨实现跳转)
华为·ffmpeg·音视频