如何识别损坏的视频文件?

时间:2021-02-25 13:49:51

标签: python opencv

我有一个简单的脚本来从视频中获取图像:

import cv2
z = cv2.VideoCapture("test.ts")
c,b = z.read()
cv2.imwrite("test.jpg",b)

有时视频已损坏,我会看到此图像:

enter image description here

如何判断图片是否损坏?

1 个答案:

答案 0 :(得分:2)

对于这种非常特殊的图像损坏,您可以分析沿 y 轴的标准偏差。在给定的图像中,标准偏差将比来自视频流的现实生活中的图像低。

代码很简单:

import cv2
import numpy as np


def is_damaged(image):

    # Calculate standard deviation along y axis; average over all color channels
    stddev = np.mean(np.std(image, axis=0), axis=1)

    # DEBUG
    print('DEBUG:', np.mean(stddev))

    if np.mean(stddev) < 30:
        return True

    return False

is_damaged(your_image)

我测试了以下三张图片,最后给出的图片:

Image 1

Image 2

Image 3

而且,这些是输出:

DEBUG: 62.19297268275302
False
DEBUG: 67.96137802561054
False
DEBUG: 46.35735168890202
False
DEBUG: 21.412358349165217
True

如您所见,与损坏的图像相比,即使是这些“人造”图像也具有相当高的标准偏差。根据您的数据设置阈值。

再次:这(仅)适用于这种非常特殊的图像损坏。

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
NumPy:         1.20.1
OpenCV:        4.5.1
----------------------------------------
相关问题