检测连续图像的非/最小变化像素的最快方法 [英] Fastest way to detect the non/least-changing pixels of successive images

查看:62
本文介绍了检测连续图像的非/最小变化像素的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到静态视频流的像素.通过这种方式,我可以检测视频流中的徽标和其他不动的项目.我的脚本背后的想法如下:

I want to find the pixels of a video stream that are static. This way I can detect logos and other non-moving items on my video stream. My idea behind the script is as follows:

  • 在一个名为 previous
  • 的列表中收集一些大小相等和灰度大小相同的帧
  • 如果收集到一定数量的帧,调用函数np.std
  • 此函数循环遍历新图像的所有 x-y 坐标.
  • 根据所有帧对应坐标的灰度值计算所有坐标的灰度值的标准差

我的脚本:

import math
import cv2
import numpy as np


video = cv2.VideoCapture(0)
previous = []
n_of_frames = 200

while True:
   ret, frame = video.read()
   if ret:
      cropped_img = frame[0:150, 0:500]
      gray = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2GRAY)
      if len(previous) == n_of_frames:
         stdev_gray = np.std(previous, axis=2)
         previous = previous[1:]
         previous.append(gray)
      else:
         previous.append(gray)

      cv2.imshow('frame', frame)

      key = cv2.waitKey(1)
      if key == ord('q'):
         break

video.release()
cv2.destroyAllWindows()

这个过程非常缓慢,我很好奇是否有更快的方法来做到这一点.我对 Cython 等持开放态度.非常感谢!

This process is pretty slow and I am curious if there are faster ways to do this. I am open to Cython etc. Many many thanks in advance!

推荐答案

一种方法是使用 cv2.bitwise_and() 逐帧比较每个帧.这个想法是前一帧中的像素必须存在于当前帧中才能成为不变的像素.通过遍历帧列表,场景中的所有特征都必须出现在前一帧和当前帧中才能被视为非移动项目.因此,如果我们依次迭代每一帧,最后一次迭代将具有所有先前帧的共享特征.

An approach is to compare each frame-by-frame using cv2.bitwise_and(). The idea is that pixels in the previous frame must be present in the current frame to be a non-changing pixel. By iterating through the list of frames, all features in the scene must be present in the previous and current frame to be considered a non-moving item. So if we sequentially iterate through each frame, the last iteration will have shared features from all previous frames.

使用这组每秒捕获一次的帧

Using this set of frames captured once per second

我们将每一帧转换为灰度,然后使用前一帧和当前帧cv2.bitwise_and().每次连续迭代的不变像素以灰色突出显示,而变化的像素以黑色突出显示.最后一次迭代应该展示所有帧之间共享的像素.

We convert each frame to grayscale then cv2.bitwise_and() with the previous and current frame. The non-changing pixels of each successive iteration are highlighted in gray while changing pixels are black. The very last iteration should showcase pixels shared between all frames.

如果你也对每一帧进行阈值处理,你会得到更明显的结果

If instead you also thresholded each frame, you get a more pronounced result

import cv2
import glob

images = [cv2.imread(image, 0) for image in glob.glob("*.png")]

result = cv2.bitwise_and(images[0], images[1])
for image in images[2:]:
    result = cv2.bitwise_and(result, image)

cv2.imshow('result', result)
cv2.waitKey(0)

这篇关于检测连续图像的非/最小变化像素的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆