简体   繁体   English

脾气暴躁的平均系列

[英]Numpy averaging a series

numpy_frames_original are frames in a video. numpy_frames_original是视频中的帧。 Firstly, I wanted to find the average of these frames and subtract it from each frame giving numpy_frames . 首先,我想找到这些帧的平均值,并从每个给出numpy_frames的帧中减去它。 For the problem I am trying to tackle I thought it would be a good idea to find the average of all of these frames, to do this I wrote the code below: 对于我要解决的问题,我认为找到所有这些帧的平均值是个好主意,为此,我编写了以下代码:

arr=np.zeros((height,width), np.float)

for i 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 I have decided It would be better not to get an average of all of the frames, but instead for each frame subtract an average of 100 frames closest to it. 现在,我决定最好不要获得所有帧的平均值,而是为每个帧减去最接近它的100个帧的平均值。

I'm not sure how to write this code? 我不确定如何编写此代码?

For example for frame 0 it would average frames 0 - 99 and subtract the average. 例如,对于帧0,它将对帧0-99求平均并减去平均值。 For frame 3 it would also average frames 0 - 99 and subtract, for frames 62 it would average frames 12-112 and subtract. 对于帧3,还将对帧0-99平均并减去,对于帧62,将对帧12-112平均并减去。

Thanks 谢谢

I think this does what you need. 我认为这可以满足您的需求。

import numpy

# Create some 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 the modified frames over a 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)

for 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 the assert, the code will need to be modified if your shortest sequence is shorter than the total range size (eg 100 frames). 请注意断言,如果您最短的序列小于总范围大小(例如100帧),则需要修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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