Convolution on single image with 3x3 filter

Hi, I am trying to make something like sobel filter on a single image in PyTorch.
I did that in Pyton using CV2.

How to do that for a single image in PyTorch?

You need to use torch.nn.functional.conv2d in order to convolve the image with an specific kernel.
docs indicates the shape of the kernel, very straight forward.

Note that pytorch use cross-correlation instead of convolutions.



Yes, JuanFMontesinos, I used just what you told me.

Where f is sobel filter expanded to all 3 channels.

import torch
filter = torch.tensor([[1., 2. , 1.], [0., 0., 0.], [-1., -2. , -1.]])
f = filter.expand(1,3,3,3)
1 Like

Wonderful feedback. Thanks.