Fatemeh_myd Fatemeh_myd - 1 year ago 61
Python Question

How to add in a multi dimentional array in Python?

I have just started doing my project with python.

I want to extract the background in a video by considering the values of each pixels which has higher occurrence in the entire video. for this, I want to add the pixel values for each frame to the previous ones.

To make it more simplified, for example, I have a video with total frame of 400. the frame size is 100X300. For each loop, I want to append (concatenate) by the values of a current frame. So, after each loop (for reading the frame in video), the size of the array should be increase in the third dimentions by 1 and in total after reading all frames, I should have an array with size (100X300X400)-> an array with 100 rows, 300 columns and 400 depth!

Then, I will calculate the histogram for each pixel.

Your help is really appreciated.

I am using OpenCV 3.1 and Python 2.7.12 using Pycharm.

import cv2
import numpy as np

c = cv2.VideoCapture('NonStop_stab.mp4')
width = c.get(cv2.CAP_PROP_FRAME_WIDTH)
height = c.get(cv2.CAP_PROP_FRAME_HEIGHT)
numOfFrame= c.get(cv2.CAP_PROP_FRAME_COUNT)

fps= c.get(cv2.CAP_PROP_FPS)
size = (int(width), int(height))

_, f = c.read()

prevFrame= f

result_array= prevFrame

while c.isOpened():

#Here is the part I am asking
result_array = np.append(result_array, [prevFrame])
ret, f = c.read()
curFrame= f

if ret == True:

...['Do Something']

k = cv2.waitKey(20)
if k == 27:
print result_array.shape



Answer Source

Thanks for any one who tries to help me. I found the answer. Just before the While() loop we define an empty array:

result_array= []

then we append the curFrame into result_array by :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download