I have a matrix of size [c, n, m]
where c
is a number of channels; n
and m
are width and height. In the particular example I have a matrix that has 1000 channels. I want to make a convolution with a kernel of the size a x a
for each channel separately. In my example the kernel size is 3 x 3
. Is there any function in scipy
or numpy
that does that kind of operation without iterating through the channels with a loop?
I found scipy.ndimage.convolve
function but I think that I can not apply that function on this problem without using a loop.
I think you just need to make you kernel three-dimensional. Something like this ought to work:
kernel = kernel[:, :, None]
If scipy.ndimage.convolve
doesn't work for 3D arrays, you could try scipy.signal.convolve
.
Treat your matrix as an image and use opencv. Change the shape of your array to be [height, width, num_channels]. Then run filter2D (convolution function for images) in opencv.
image = cv2.imread("some_image.jpg")
image.shape # (height, width, 3) # 3 is 3 channels for Red, Green, Blue
kernel = np.ones((3,3)) / 9.
image_blurred = cv2.filter2D(image, cv2.CV_64F, kernel) # will apply the kernel for each channel. You can have more than 3 channels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With