# Rolling (time) average of a timeseries of images

**URL:** <https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352>\
**Category:** Signal and Image Processing\
**Tags:** images\
**Created:** [February 17, 2024, 6:03pm UTC](https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352 "2024-02-17T18:03:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonathanH](https://avatars.discourse-cdn.com/v4/letter/j/8dc957/32.png) [@JonathanH](https://discourse.julialang.org/u/JonathanH)\
**Post date:** [February 17, 2024, 6:03pm UTC](https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352/1 "2024-02-17T18:03:27Z")

</div>

Hi all,

I am new to Julia, and this may be a very straightforward question. I have a timeseries of images, and I am trying to calculate a pixel-wise rolling time average. The obvious choice for doing this would be the mapwindow ffunction from the Images.jl package. For instance, if I want a rolling time average (where time is the third dimension) with window size 5, the line would be time\_mean = mapwindow(mean, timeseries, (1,1,5), “reflect”), but I got an error trying this, I think because mapwindow tries to pad each dimension, even if the input window size for that dimension is 1 (which would correspond to padding 0 indices). At least that was the way I understood the error message. Is there a way to make this work with mapwindow and if not, is there an alternative way to do this?

---

<div class="post-metadata">

**Author:** ![mgkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgkuhn/32/6276_2.png) [@mgkuhn](https://discourse.julialang.org/u/mgkuhn)\
**Post date:** [February 18, 2024, 11:37pm UTC](https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352/2 "2024-02-18T23:37:24Z")

</div>

To move a rolling-sum window along efficiently, just add the new first frame and subtract the old last frame. No need to use any library function, as that is just a basic loop.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 19, 2024, 6:26am UTC](https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352/3 "2024-02-19T06:26:33Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [February 19, 2024, 9:59am UTC](https://discourse.julialang.org/t/rolling-time-average-of-a-timeseries-of-images/110352/4 "2024-02-19T09:59:36Z")

</div>

Hi there,

This is a peculiar error indeed. Would you file an issue in ImageFiltering.jl (where the mapwindow function lives)?

Interestingly, `; border = "replicate"` (the default) does work fine, as does `border = Inner()`, but not `"reflect"` or `NoPad()`. I’m not familiar enough to be sure that it’s not my fault, but since the underlying padding algorithm seems to work just fine with reflect, you can circumvent the problem by preapplying padding and using `Inner()` to discard the padded edges.

```julia
testim = Gray.(rand(20,20,15))
mywindow = (1,1,5)
pad = floor.(Int, mywindow ./ 2) # (0,0,2) or just hardcode the necessary padding
paddedimages = padarray(testim, Pad(:reflect, pad)) # here :reflect does work
filteredimages = mapwindow(mean, paddedimages, mywindow; border = Inner())

```
