Numpy平均一系列

时间:2015-07-22 15:19:34

标签: python numpy

numpy_frames_original 是视频中的帧。首先,我想找到这些帧的平均值,并从每个帧中减去它,给出 numpy_frames 。对于我试图解决的问题,我认为找到所有这些帧的平均值是个好主意,为此,我编写了下面的代码:

package com.example;

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

@Order(Ordered.HIGHEST_PRECEDENCE)
public class ConfigServicePropertyDeprioritizer
        implements ApplicationListener<ApplicationPreparedEvent>
{
    private static final String CONFIG_SOURCE = "bootstrap";

    private static final String PRIORITY_SOURCE = "systemEnvironment";

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event)
    {
        ConfigurableEnvironment environment = event.getApplicationContext()
                .getEnvironment();
        MutablePropertySources sources = environment.getPropertySources();
        PropertySource<?> bootstrap = findSourceToMove(sources);

        if (bootstrap != null)
        {
            sources.addAfter(PRIORITY_SOURCE, bootstrap);
        }
    }

    private PropertySource<?> findSourceToMove(MutablePropertySources sources)
    {
        boolean foundPrioritySource = false;

        for (PropertySource<?> source : sources)
        {
            if (PRIORITY_SOURCE.equals(source.getName()))
            {
                foundPrioritySource = true;
                continue;
            }

            if (CONFIG_SOURCE.equals(source.getName()))
            {
                // during bootstrapping, the "bootstrap" PropertySource
                // is a simple MapPropertySource, which we don't want to
                // use, as it's eventually removed. The real values will 
                // be in a CompositePropertySource
                if (source instanceof CompositePropertySource)
                {
                    return foundPrioritySource ? null : source;
                }
            }
        }

        return null;
    }
}

现在我已经决定最好不要获得所有帧的平均值,而是每帧减去最接近它的平均100帧。

我不确定如何编写此代码?

例如,对于第0帧,它将平均帧0到99并减去平均值。对于帧3,它还将平均帧0-99并减去,对于帧62,它将平均帧12-112并减去。

由于

1 个答案:

答案 0 :(得分:1)

我认为这可以满足您的需求。

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

注意断言,如果最短序列短于总范围大小(例如100帧),则需要修改代码。