I am looking to apply conv2
on a RGB image. However the two solutions I arrived at don’t look very good. Questions:
- Is there
anya better way to applyconv2
along a given dimension ofimg
and return the result? - Is there a cleaner way of writing the second solution below? I need to concatenate the three nested multidimensional arrays in
channels
along a new dimension in a new3xHxW Array
.
I know I can use the api from Images
for filtering, but this is just an exercise in Julia syntax.
img = rand(3, 10, 16)
g = fill!(rand(3, 3), 0.001)
g[2, 2] = 1
# convolve each channel
channels = [conv2(img[i, :, :], g) for i in 1:3]
# solution no 1
new_size = (3, size(channels[1])...)
result = zeros(new_size)
for i in 1:3
result[i, :, :] = channels[i]
end
# solution no 2
new_size = (size(channels[1])..., 3)
result = permutedims(reshape(hcat(channels...), new_size), [3, 1, 2])
Thank you!