【问题标题】:Best way to detect a light blinking from video footage via python通过python从视频片段中检测灯光闪烁的最佳方法
【发布时间】:2016-10-28 01:31:15
【问题描述】:

我有一些视频片段并希望分析光源何时闪烁。光源在同一位置,因此应该易于使用 ROI。

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

我目前的方法是这样的

  1. 加载视频
  2. 从视频中提取图像以选择区域 兴趣(ROI)通过绘制一个矩形(希望有一个简单的 模块)
  3. 遍历整个视频以检测像素变化 在投资回报率中
  4. 记录/绘制变化超过阈值的时间

我很确定有人做过类似的事情,所以任何有用的模块、教程或方便的软件的链接都会很棒。谢谢。

【问题讨论】:

    标签: python video video-capture video-processing roi


    【解决方案1】:

    mahotas 可能是分析图像的不错选择。它将图像加载为 numpy 数组,因此选择 ROI 是微不足道的。它还具有内置的阈值方法,计算图片的平均亮度和类似的东西。最后但同样重要的是,mahotas 的文档非常好。

    我不知道在 Python 中从视频中提取帧的最佳方法(尽管您可以使用 opencv 之类的方法来完成,但这似乎有点过头了)所以我建议使用一些外部程序,例如 ffmpeg 并使用 @ 调用它987654323@ 模块。此外,快速谷歌搜索给了我this 这可能是合适的。

    【讨论】:

      【解决方案2】:
      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()
      

      【讨论】:

        猜你喜欢
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        相关资源
        最近更新 更多