I am working on a computer vision program and trying to create a background image by taking the median value of each pixel over the duration of the video. To do this I convert each video frame from RGB to gray scale and also convert the data to int32. Once I do this I want to read the gray-value of each pixel and add 1 to a bin of a histogram (each pixel has its own histogram assigned to it).
This should be a simple task however it is taking wayyyyyy to long. When I look at the time for each step I see the conversions are order of magnitude .01 sec, but the reading of the data across all pixels is on the order of magnitude of 1 sec.
Since the gray scale conversion is essentially doing more flops than just reading the data at each pixel and taking less time to do it, I know there must be a better way to do this. I have tried to multi-thread the process and saw no speed up between 2 threads and 4 threads, so I don't think this will help.
backgroundImg=[[[0 for col in range(64)]
for col in range(1920)]
for row in range(1080)]
while(True)
ret,frame=cam.read()
if ret:
frame=Image.fromarray(frame).convert('LA')
frame=np.int32(frame)
for line in range(0,1080):
for col in range(0,1920):
backgroundImage[line][col][frame[line][col][0]]+=1
This is an example of my code. After the background image is calculated I will need to subtract this from every frame and run algorithm to find all the connected pixels.