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

2024-05-14 14:16:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我应该怎么做才能找到每个像素的平均值,并根据结果形成一个新的图片?在

非常感谢!在

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

Tags: the代码importimgreadframes视频video
1条回答
网友
1楼 · 发布于 2024-05-14 14:16:28

正如我在评论中告诉你的,这取决于视频的长度,但是你可以用天真的方式,把所有的都加到一个累加器上,然后除以。。。我说天真的方式,因为这种方法可能会带来不准确。。。最理想的方法是在网上实现它(也许以牺牲性能为代价)。。。(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)

我希望这对你有帮助,否则请留言

相关问题 更多 >

    热门问题