0
votes

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
            break

    else:
        break

cv2.destroyAllWindows()
c.release()
out.release()
2
Can you show what you have coded so far, and explain, by referencing your code what areas are giving you difficulty? - idjaw
I add the code currently I have. I've just used append() but the results is not like what I want. Output of "result_array.shape" is something like (3185L,)! - Fatemeh_myd

2 Answers

0
votes

Use numpy.dstack to stack arrays in the 3rd axis:

In [1]: import numpy

In [2]: d = numpy.random.random((5,5))

In [3]: d.shape
Out[3]: (5, 5)

In [4]: for i in range(10): d=numpy.dstack((d,numpy.random.random((5,5))))

In [5]: d.shape
Out[5]: (5, 5, 11)
0
votes

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 :

result_array.append(curFrame)