如何组合视频的每个帧(Python和OpenCV)的像素?

时间:2018-02-09 08:32:40

标签: python opencv image-processing

下面的代码允许我读取视频每帧的数组。

如何查找单个像素的平均值并根据结果制定新图片?

非常感谢!

import cv2
import numpy as np

# loading the video
vidcap = cv2.VideoCapture('testing.mp4')
success, img = vidcap.read()

# establish a while loop for reading all the video frames
frames = 0

while success:

    success, image = vidcap.read()
    copimg = img[65:230, 0:350]
    blur_img = cv2.GaussianBlur(copimg, (5, 5), 3)
    gray_img = cv2.cvtColor(blur_img, cv2.COLOR_BGR2GRAY)        

    frames += 1
    print gray_img

1 个答案:

答案 0 :(得分:0)

正如我在评论中告诉你的那样取决于视频的长度,但是你可以采用天真的方式将所有内容添加到累加器并进行分割...我说天真的方式,因为这种方法可能带有不准确性。 ..理想的做法是做一个在线的平均值(以性能为代价)......(here is在线伪代码)...

对于天真的情况,创建一个累加器并添加所有帧(或你想要的部分)。您的代码有错误,您不能在新框架上工作,只能在第一个框架上工作。这是修复此问题的代码。

import cv2
import numpy as np

# loading the video
vidcap = cv2.VideoCapture('testing.mp4')
success, img = vidcap.read()

# establish a while loop for reading all the video frames
frames = 0
# accumulator in double precision
avg = np.zeros((165,350), dtype=np.float64)
while success:

    success, img = vidcap.read()
    copimg = img[65:230, 0:350]
    blur_img = cv2.GaussianBlur(copimg, (5, 5), 3)
    gray_img = cv2.cvtColor(blur_img, cv2.COLOR_BGR2GRAY)
    avg = np.add(avg, gray_img)

    frames += 1

avg = np.divide(avg, frames)
print (avg)

我希望这可以帮助你,否则发表评论