简体   繁体   English

如何从帧的 ndarray 访问单个像素阵列,或者是否有更好的方法来存储帧并访问它们?

[英]How can I access the individual pixel arrays from an ndarray of frames or is there a better way to store the frames and access them?

I am trying to take 25 frames from a video and store them in an array so that I can process those frames later(extract the background).我试图从视频中获取 25 帧并将它们存储在一个数组中,以便我可以稍后处理这些帧(提取背景)。 I am using OpenCV to load and store the frames from the video(don't know any other way).我正在使用 OpenCV 加载和存储视频中的帧(不知道任何其他方式)。 The problem is I can't seem to figure how to iterate through the individual arrays stored in the ndarray.问题是我似乎无法弄清楚如何遍历存储在 ndarray 中的各个数组。 Is it possible to do that?有可能这样做吗? If not what is a better way to store the frames to be able to access them later?如果不是,有什么更好的方法来存储帧以便以后访问它们? Note: I know there is already an OpenCV function that substracts the background but for the application I am building for university I am not allowed to use it.注意:我知道已经有一个 OpenCV 函数可以减去背景,但是对于我为大学构建的应用程序,我不允许使用它。 Also this is the first time I am programming in python so any advice, where to look for info is most welcome.这也是我第一次用 python 编程,所以任何建议,在哪里寻找信息都是最受欢迎的。 Here is what I tried to do:这是我尝试做的:

frames=[]
vidcap = cv2.VideoCapture(video_source)
for fid in range(0,25):
    vidcap.set(cv2.CAP_PROP_POS_FRAMES, fid)
    ret, frame = vidcap.read()
    frames.append(frame)

The problem is here:问题在这里:

width,height = frames[0].size
for row in range(0,height):
    for col in range(0,width):
        for frameNo in range(0,frames.size):
            median_matr.append(frames[0][row,col])

The error i am getting is this:我得到的错误是这样的:

width,height = frames[0].size TypeError: 'int' object is not iterable width,height = frames[0].size TypeError: 'int' 对象不可迭代

The problem here is that frames[0].size is an integer.这里的问题是frames[0].size 是一个整数。 You're trying to assign both width and height in the line您正在尝试在行中同时分配宽度和高度

width,height = frames[0].size

but since Python only has one integer to use for assignment it's not able to process the line of code.但由于 Python 只有一个整数用于赋值,因此无法处理这行代码。

I mean, my overall suggestion is really, "Go get an integrated visual environment so you can put a break point here and debug into what's going on."我的意思是,我的总体建议实际上是,“去获得一个集成的视觉环境,这样你就可以在这里设置一个断点并调试正在发生的事情。” The underlying problem is you're trying to assign something that isn't assign-able.潜在的问题是您试图分配一些不可分配的东西。 I suggest running this chunk of code in PyCharm, Visual Studio, or some other visual debugger that allows you to dig into the variables will show you exactly what's going on instead of requiring you to know all the variable properties ahead of time.我建议在 PyCharm、Visual Studio 或其他一些允许您深入了解变量的可视化调试器中运行这段代码,它将准确地显示正在发生的事情,而不是要求您提前了解所有变量属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用其中之一的索引合并数据帧? - How can I merge data frames by using the indexing of one of them? 如果浏览器不支持帧+如何直接获取帧的内容,则无法直接访问帧 - How to get contents of frames automatically if browser does not support frames + can't access frame directly 如何访问下面嵌套的 python 列表的各个值? 循环遍历我的列表以获取它们的最佳方法是什么? - How can I access the individual values my nested python list below? Whats the Best way to loop over my list to get them? 访问张量中各个元素的更好方法 - Better way to access individual elements in a tensor 有没有一种从多个帧中提取单个像素的有效方法? - Is there an efficient way of extracting individual pixels from a number of frames? 从图像中提取单个帧 - Extracting individual frames from an image 拉取MS访问表并将它们放在python中的数据框中 - Pulling MS access tables and putting them in data frames in python 如何在 15 帧的时间窗口的密集光流中找到特征点(特定像素)的速度? - How can I find speed of a feature point (certain pixel) in dense optical flow for temporal window of 15 frames? 如何使用 NAN 填充来自同一列表的先前数据帧的相同值的数据帧 - How can I fill data frames with NAN with same values of previous data frames from the same list 如何加载动画 GIF 并获取 Pygame 中的所有单个帧? - How can I load an animated GIF and get all of the individual frames in Pygame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM