比较差异 图片 视频

视频比较差异:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
FFmpeg是一个强大的跨平台命令行工具,用于处理多媒体文件,包括音频、视频流的转换、编码、解码等。当使用FFmpeg将图片序列转换成视频时,如果速度较慢,可能是以下几个原因导致的: 1. **图片分辨率高或数量大**:每张图片的质量和尺寸都会影响到转换的时间。如果图片分辨率非常高,或者是大量的图片叠加在一起,处理时间会显著增加。 2. **编码设置**:FFmpeg提供了多种编码格式供选择,不同的编码器性能差异较大。比如H.264编码通常比 MJPEG 或 WMV 更快,但质量可能会有所牺牲。调整合适的编码参数可以提高效率。 3. **硬件限制**:如果你的计算机CPU或内存不足,处理大量图片视频数据会显得吃力。使用更快的处理器或增加内存可以加快转换速度。 4. **命令行选项优化**:如果没有正确设置如并行处理或多线程(`-threads` 参数),FFmpeg可能会按单线程运行,这会限制了它的处理能力。 5. **I/O瓶颈**:如果输入图片存储位置离系统读取远,或者磁盘IO速度慢,也可能会影响转换速度。 为了改善这种情况,你可以尝试优化命令,例如采用多线程,调整编码格式和参数,以及检查系统的资源利用情况。这里提供一个基本的FFmpeg命令示例,你可以作为起点进行优化: ```bash ffmpeg -framerate <帧率> -i input_%d.jpg -c:v libx264 -preset faster output.mp4 ``` 其中`<帧率>`是你需要的输出视频帧率,`input_%d.jpg`代表你的图片序列文件名规则(%d表示数字序号)。记得替换具体的参数值,并查看FFmpeg文档了解更多详细选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值