# Cleaner way for applying conv2 to RGB image

**URL:** https://discourse.julialang.org/t/cleaner-way-for-applying-conv2-to-rgb-image/6438
**Category:** General Usage
**Tags:** question
**Created:** [October 14, 2017, 2:32pm UTC](https://discourse.julialang.org/t/cleaner-way-for-applying-conv2-to-rgb-image/6438 "2017-10-14T14:32:47Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![FlorinGogianu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floringogianu/32/2186_2.png) [@FlorinGogianu](https://discourse.julialang.org/u/FlorinGogianu)
#### Post date: [October 14, 2017, 2:32pm UTC](https://discourse.julialang.org/t/cleaner-way-for-applying-conv2-to-rgb-image/6438/1 "2017-10-14T14:32:47Z")

</div>

I am looking to apply `conv2` on a RGB image. However the two solutions I arrived at don’t look very good. Questions:

1. Is there ~~any~~ a better way to apply `conv2` along a given dimension of `img` and return the result?
2. 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 new `3xHxW Array`.

I know I can use the api from `Images` for filtering, but this is just an exercise in Julia syntax.

```julia
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!
