import cv2
import numpy as np
def is_blank(img, threshold=10):
# 计算像素值的平均值
mean = np.mean(img)
# 如果平均值接近0(黑色)或255(白色),认为图像是黑白屏
return mean < threshold or mean > (255 - threshold)
def detect_black_white_screen(video_path):
# 读取视频文件
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("无法打开视频文件")
return
black_screens, white_screens = 0, 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 转换为灰度图像
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if is_blank(gray_frame, threshold=10):
mean = np.mean(gray_frame)
if mean < 10:
print("检测到黑屏")
black_screens += 1
else:
print("检测到白屏")
white_screens += 1
cap.release()
print(f"黑屏数量: {black_screens}")
print(f"白屏数量: {white_screens}")
if __name__ == '__main__':
video_path = 'path/to/your/video/file.mp4'
detect_black_white_screen(video_path)
检测黑白屏
最新推荐文章于 2024-07-27 12:20:46 发布