python - Numpy averaging a series -


numpy_frames_original frames in video. firstly, wanted find average of these frames , subtract each frame giving numpy_frames. problem trying tackle thought idea find average of of these frames, wrote code below:

arr=np.zeros((height,width), np.float)  in range(0, number_frames):     imarr=np.array(numpy_frames_original[i,:,:].astype(float))     arr=arr+imarr/number_frames  img_avg=np.array(np.round(arr),dtype=np.uint8) numpy_frames = np.array(np.absolute(np.round(np.array(numpy_frames_original.astype(float))-np.array(img_avg.astype(float)))), dtype=np.uint8) 

now have decided better not average of of frames, instead each frame subtract average of 100 frames closest it.

i'm not sure how write code?

for example frame 0 average frames 0 - 99 , subtract average. frame 3 average frames 0 - 99 , subtract, frames 62 average frames 12-112 , subtract.

thanks

i think need.

import numpy  # create fake data frame_count = 10 frame_width = 2 frame_height = 3 frames = numpy.random.randn(frame_count, frame_width, frame_height).astype(numpy.float32) print 'original frames\n', frames  # compute modified frames on specified range mean_range_size = 2 assert frame_count >= mean_range_size * 2  start_mean = frames[:2 * mean_range_size + 1].mean(axis=0) start_frames = frames[:mean_range_size] - start_mean  middle_frames = numpy.empty((frames.shape[0] - 2 * mean_range_size,                              frames.shape[1], frames.shape[2]),                             dtype=frames.dtype)  index in xrange(middle_frames.shape[0]):     middle_frames[index] = frames[mean_range_size + index] - \                            frames[index:index + 2 * mean_range_size + 1].mean(axis=0)  end_mean = frames[-2 * mean_range_size - 1:].mean(axis=0) end_frames = frames[-mean_range_size:] - end_mean  modified_frames = numpy.concatenate([start_frames, middle_frames, end_frames]) print 'modified frames\n', modified_frames 

note assert, code need modified if shortest sequence shorter total range size (e.g. 100 frames).


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -