[英]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.