比较差异 图片 视频

目录

两张图片像素差:

深度图和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))
相关推荐
叶子Talk6 小时前
GPT-Image-2正式发布:文字渲染99%,Image Arena三项第一,AI图像生成彻底变天了
人工智能·gpt·计算机视觉·ai·openai·图像生成·gpt-image-2
思绪无限7 小时前
YOLOv5至YOLOv12升级:血细胞检测系统的设计与实现(完整代码+界面+数据集项目)
人工智能·python·深度学习·目标检测·计算机视觉·yolov12·血细胞检测
朝风工作室9 小时前
实时全景拼接|支持任意路数输入,8*8K RTX3050 实测 10ms 内
图像处理·算法·计算机视觉
思绪无限9 小时前
YOLOv5至YOLOv12升级:金属锈蚀检测系统的设计与实现(完整代码+界面+数据集项目)
人工智能·python·深度学习·目标检测·计算机视觉·yolov12
VOOHU_201810 小时前
VOOHU沃虎:音频变压器的主要作用是什么?什么情况下必须使用?
网络·物联网·音视频·电子元器件
ZPC821011 小时前
ROS2 速度远快于 UDP的完整方案(同机节点)
人工智能·算法·计算机视觉·机器人
APIshop11 小时前
小红书笔记视频详情接口深度解析:smallredbook.item_get_video_pro
数据库·笔记·音视频
H Journey13 小时前
SIMD 指令集(如 AVX2, NEON)进行OPenCV加速
opencv·计算机视觉·simd指令集加速
AI服务老曹13 小时前
【架构深评】深度解析异构计算下的 AI 视频管理平台:从 GB28181 接入到 X86/ARM 容器化部署的全链路实战
人工智能·架构·音视频
AI人工智能+14 小时前
表格识别技术:通过深度学习与计算机视觉融合,实现复杂文档中表格的版面还原及数据的结构化转换。
深度学习·计算机视觉·ocr·表格识别