# Jitter
import cv2
import numpy as np
# 视频路径和输出路径
input_video_path = r'D:\desk\20240713_test\Ori_Videos\ori_videos\IR\ir_shake_20240713.mp4'
output_video_path = r'D:\desk\20240713_test\Ori_Videos\enhance\IR\jitter\jitter_20.mp4'
# 打开视频文件
cap = cv2.VideoCapture(input_video_path)
# 获取视频的帧率和帧大小
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# 定义输出视频的编码和创建VideoWriter对象
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 定义编码器和文件格式
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
# 抖动参数
jitter_x = 100 # 水平方向抖动范围
jitter_y = 100 # 垂直方向抖动范围
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 生成随机抖动的偏移量
offset_x = np.random.randint(-jitter_x, jitter_x)
offset_y = np.random.randint(-jitter_y, jitter_y)
# 应用平移变换
rows, cols = frame.shape[:2]
M = np.float32([[1, 0, offset_x], [0, 1, offset_y]])
jittered_frame = cv2.warpAffine(frame, M, (cols, rows))
# 写入处理后的帧到输出视频
out.write(jittered_frame)
# # 显示原始和抖动后的帧
# cv2.imshow('Original', frame)
# cv2.imshow('Jittered', jittered_frame)
# 按 'q' 退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
主要思想就是让图像在上下左右随机偏移
标签:视频,jitter,抖动,frame,cap,cv2,OpenCV,video From: https://blog.csdn.net/weixin_49824703/article/details/140425790