简体   繁体   中英

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). I am using OpenCV to load and store the frames from the video(don't know any other way). The problem is I can't seem to figure how to iterate through the individual arrays stored in the 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. Also this is the first time I am programming in python so any advice, where to look for info is most welcome. 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

The problem here is that frames[0].size is an integer. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM