通过python检测视频片段中闪烁的光线的最佳方法



我有一些视频片段,希望分析光源闪烁时的情况。光源在同一位置,所以应该容易与ROI一起工作。

我习惯使用python,但在视频分析方面不是很强大-不太关心格式和技术方面的东西。只是希望找到一种快速而又肮脏的方法来检测它。

我现在的方法是这样的

  1. 加载视频
  2. 从视频中提取图像,以选择的区域利息(ROI)通过画一个矩形(希望有一个容易
  3. 遍历整个视频以检测像素变化ROI
  4. 记录/绘制变化超过阈值的时间

我很确定有人做过类似的事情,所以链接到任何有用的模块,教程或方便的软件将是伟大的。谢谢。

import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the video
cap = cv2.VideoCapture('../data/video/video_test.mp4')
# Extract an image from the video in order to elect region of interest (ROI) by drawing a rectangle
ret, frame = cap.read()
# Draw a rectangle on the image
cv2.rectangle(frame, (100, 100), (300, 300), (255, 0, 0), 2)
# Select the ROI
roi = frame[100:300, 100:300]
# Convert the image to grayscale
roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# Create a mask
roi_mask = np.zeros(roi_gray.shape[:2], np.uint8)
# Draw a circle on the mask
cv2.circle(roi_mask, (150, 150), 100, 255, -1)
# Apply the mask to the grayscale image
masked_roi_gray = cv2.bitwise_and(roi_gray, roi_gray, mask=roi_mask)
# Create a threshold to exclude minute movements
threshold = 70
# Apply threshold to the masked grayscale image
_, thresh = cv2.threshold(masked_roi_gray, threshold, 255, cv2.THRESH_BINARY)
# Find changes in the masked grayscale image
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw a circle around the detected changes
for cnt in contours:
    (x, y, w, h) = cv2.boundingRect(cnt)
    cv2.circle(roi, (x + int(w/2), y + int(h/2)), 5, (0, 0, 255), -1)
# Show the image
cv2.imshow('image', frame)
# Wait for a key press
cv2.waitKey(0)
# Destroy all windows
cv2.destroyAllWindows()

mahotas可能是分析图像的好选择。它将图像加载为numpy数组,因此选择ROI将是微不足道的。它也有内置的方法来阈值,计算图像的平均亮度等等。最后但并非最不重要的是,mahotas的文档非常好。

我不知道从Python视频中提取帧的最佳方法(虽然你可以用像opencv这样的东西来完成,但它似乎是一个过度的),所以我建议使用一些外部程序,如ffmpeg,并用subprocess模块调用它。此外,快速谷歌给了我这个可能是合适的。

相关内容

最新更新