本文主要是介绍【开盖即食】多种算法实现画面动静判断(附源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
大家好,我是cv君,今天想跟大家分享一下,如何实现画面动静判断、判断画面或者物体是否在运动或者是比较静止,简单使用计算机视觉传统方法实现,AI的后续带给大家。我们提供三种方案:
1、背景消除法;
2、光流追踪法;
3、相似度、清晰度变化法;
代码开盖即食,拿来可用,请品尝~
然后我们可以把视频中运动的部分保留,静止的部分扣除;
1、背景消除法;
import cv2
import numpy as np# 配置视频文件路径和输出文件路径
video_path = r"demo3.mp4"
output_video_path = r"demo3.avi"# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():print("无法打开视频文件")exit()# 获取视频基本信息
fps = cap.get(cv2.CAP_PROP_FPS) # 帧率
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 创建背景减除器
bg_subtractor = cv2.createBackgroundSubtractorMOG2(varThreshold=30)
# fgbg = cv2.createBackgroundSubtractorMOG2(varThreshold=30, detectShadows=True) # 设置输出视频编解码器
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))# 处理每一秒的帧
frame_count = 0
seconds_counter = 0
frame_buffer = []while True:ret, frame = cap.read()if not ret:breakframe_count += 1second = int(frame_count // fps) # 当前秒钟# 应用背景减除器fg_mask = bg_subtractor.apply(frame)# 计算前景区域的像素数量non_zero_count = cv2.countNonZero(fg_mask)# 保存当前帧到缓冲区frame_buffer.append(frame)# 每秒钟结束时判断运动情况if frame_count % 10 == 0:# print(non_zero_count)if non_zero_count > 15000: # 根据实际情况调整阈值print(f"第 {second} 秒有运动")for f in frame_buffer:out.write(f) # 将帧写入输出视频else:print(f"第 {second} 秒静止")frame_buffer.clear() # 清空缓冲区准备处理下一秒的帧# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
2、光流追踪法;
import cv2
import numpy as np# 配置视频文件路径和输出文件路径
video_path = r"zjkzlzxjg-1511.ts"
output_video_path = r"demo3.avi"# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():print("无法打开视频文件")exit()# 获取视频基本信息
fps = cap.get(cv2.CAP_PROP_FPS) # 帧率
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 设置输出视频编解码器
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))# 读取第一帧
ret, prev_frame = cap.read()
if not ret:print("无法读取视频帧")exit()prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)# 提取关键点
prev_pts = cv2.goodFeaturesToTrack(prev_gray, maxCorners=1000, qualityLevel=0.3, minDistance=7, blockSize=7)if prev_pts is None:print("无法提取关键点")cap.release()out.release()cv2.destroyAllWindows()exit()
if prev_pts is not None:prev_pts = np.float32(prev_pts).reshape(-1, 1, 2)
# prev_pts = np.int0(prev_pts)frame_buffer = []
frame_count = 0while True:ret, frame = cap.read()if not ret:breakframe_count += 1second = int(frame_count // fps) # 当前秒钟gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 计算光流next_pts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, gray, prev_pts, None)if next_pts is not None and status is not None:good_prev_pts = prev_pts[status == 1]good_next_pts = next_pts[status == 1]# 计算光流的总变化量displacement = np.linalg.norm(good_next_pts - good_prev_pts, axis=1)non_zero_count = np.sum(displacement > 0.0) # 根据实际情况调整阈值# 保存当前帧到缓冲区frame_buffer.append(frame)# 每秒钟结束时判断运动情况if frame_count % 15 == 0:if non_zero_count > 0: # 根据实际情况调整阈值print(f"第 {second} 秒有运动")for f in frame_buffer:out.write(f) # 将帧写入输出视频else:print(f"第 {second} 秒静止")frame_buffer.clear() # 清空缓冲区准备处理下一秒的帧prev_gray = grayprev_pts = good_next_pts.reshape(-1, 1, 2)else:print("光流计算失败")# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
3、相似度、清晰度变化法;
import cv2
import numpy as np# 配置视频文件路径和输出文件路径
video_path = r"C:\Users\sunhongzhe\Pictures\expandai_move\a.mp4"
output_video_path = r"C:\Users\sunhongzhe\Pictures\expandai_move\a.avi"# 打开视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():print("无法打开视频文件")exit()# 获取视频基本信息
fps = cap.get(cv2.CAP_PROP_FPS) # 帧率
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 设置输出视频编解码器
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))# 读取第一帧
ret, prev_frame = cap.read()
if not ret:print("无法读取视频帧")exit()prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
prev_edges = cv2.Canny(prev_gray, 50, 150)frame_buffer = []
frame_count = 0# 运动检测阈值
motion_threshold = 3000 # 根据实际情况调整while True:ret, frame = cap.read()if not ret:breakframe_count += 1second = int(frame_count // fps) # 当前秒钟gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)edges = cv2.Canny(gray, 50, 150)# 计算边缘图像的差异diff = cv2.absdiff(prev_edges, edges)non_zero_count = np.sum(diff > 0)# 保存当前帧到缓冲区frame_buffer.append(frame)# 每秒钟结束时判断运动情况if frame_count % 10 == 0: # 每秒处理一次if non_zero_count > motion_threshold: # 根据差异判断是否运动print(non_zero_count)print(f"第 {second} 秒有运动")for f in frame_buffer:out.write(f) # 将帧写入输出视频else:print(f"第 {second} 秒静止")frame_buffer.clear() # 清空缓冲区准备处理下一秒的帧prev_edges = edges# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()
开盖即食,大家随便放个运动、静止结合的视频进来,调整一下阈值即可实现动静分离;保留运动的视频,剔除静止的画面
第三个方法,剩下的大家可以用phash等相似度方法同理替换,请查阅我的另一篇文章:
【含泪提速!】一文全解相似度算法、跟踪算法在各个AI场景的应用(附代码)_image.antialias-CSDN博客
三个py的就按测阈值,大家都可以改哦,还有多久判断一次,都可以;
这是实现视频中动静画面区分的;
想要实现一个视频中,哪些画面在东,哪些画面在静止,就可以将画面分成多个区域,分别运算这些个算法,都可以得到哪些地方在动,哪些地方在静止了。
演示效果:原本视频没法上传,原视频15秒,静止部分有5秒,最后处理完后,成功剔除了静止部分的帧,保留下了运动的10秒
最后
最近cv君重新常更,欢迎三连~欢迎大家进入cv君的AI 与计算机视觉世界:DeepAI 视界 里面有几千位AI的朋友,有任何问题都可以交流哦,联系微信zxx15277368495z
这篇关于【开盖即食】多种算法实现画面动静判断(附源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!