Is there something like skimage's view_as_windows in Julia?

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:

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.:

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.

I think you probably want mapwindow from ImageFiltering.jl.

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.

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

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

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
1 Like