# Is there something like skimage's view\_as\_windows in Julia?

**URL:** https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532
**Category:** General Usage
**Created:** [June 9, 2022, 9:58pm UTC](https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532 "2022-06-09T21:58:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Shirakumo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shirakumo/32/38369_2.png) [@Shirakumo](https://discourse.julialang.org/u/Shirakumo)
#### Post date: [June 9, 2022, 9:58pm UTC](https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532/1 "2022-06-09T21:58:40Z")

</div>

Hello everyone,

in Python there is a library, to be specific skimage.util.shape and there is view\_as windows which allow for something like this:

```julia
A
array([[0, 1, 2, 3],
       [4, 5, 6, 7],
       [8, 9, 10, 11],
       [12, 13, 14, 15]])

window_shape = (2, 2)

B = view_as_windows(A, window_shape)

```

As a result B will contain all windows for the given window\_shape, i.e.:

```julia
B[0, 0]
array([[0, 1],
       [4, 5]])

B[0, 1]
array([[1, 2],
       [5, 6]])

```

It is very useful i.e. for image convolutions. I tried to understand Julia’s implementation of im2col but I can’t figure out how to make it the same way as here. It can be something similar if anyone knows other solutions like that, I will be very grateful.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [June 10, 2022, 3:33pm UTC](https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532/2 "2022-06-10T15:33:22Z")

</div>

I think you probably want [`mapwindow`](https://juliaimages.org/ImageFiltering.jl/stable/function_reference/#ImageFiltering.MapWindow.mapwindow) from ImageFiltering.jl.

---

<div class="post-metadata">

### Author: ![Shirakumo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shirakumo/32/38369_2.png) [@Shirakumo](https://discourse.julialang.org/u/Shirakumo)
#### Post date: [June 10, 2022, 4:42pm UTC](https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532/3 "2022-06-10T16:42:01Z")

</div>

The thing is I must apply a function to that windows. I just want an array of such windows so I can do whatever I want with them. I technically have an alternative which is im2col but I want to use GPU to check if it will be faster than CPU version.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [June 10, 2022, 5:42pm UTC](https://discourse.julialang.org/t/is-there-something-like-skimages-view-as-windows-in-julia/82532/4 "2022-06-10T17:42:31Z")

</div>

The function to apply to each window is the first argument to `mapwindow`. For example:

```julia
julia> A=permutedims(reshape(collect(0:15), 4,4))
4×4 Matrix{Int64}:
  0 1 2 3
  4 5 6 7
  8 9 10 11
 12 13 14 15

julia> mapwindow(maximum, A, (0:1,0:1))
4×4 Matrix{Int64}:
  5 6 7 7
  9 10 11 11
 13 14 15 15
 13 14 15 15

```

If you really need the full array (perhaps your function needs to access two windows at the same time?) you could use a `StridedView`

```julia
using Strided

julia> A=permutedims(reshape(collect(0:15), 4,4))
4×4 Matrix{Int64}:
  0 1 2 3
  4 5 6 7
  8 9 10 11
 12 13 14 15

julia> V=StridedView(A, (3,3,2,2), (1,4,1,4));

julia> V[1,1,:,:]
2×2 StridedView{Int64, 2, Vector{Int64}, typeof(identity)}:
 0 1
 4 5

julia> V[1,2,:,:]
2×2 StridedView{Int64, 2, Vector{Int64}, typeof(identity)}:
 1 2
 5 6

```
