How to do tensor binary erosion operation in Pytorch

Hi Qimin!

If I understand your example tensors correctly, you want the following:

If a pixel and its 4-connected (NSEW) pixels are all 1, the resulting pixel
value should be 1 and 0 otherwise.

Build a 3x3 convolution kernel with zeros in the corners and ones for the
center and 4-connected entries. Initialize the weight of a Conv2d (no bias)
with this kernel, pad the border of your input tensor, convolve it, and test
the resulting pixels against 5: == 51 and < 5 → 0.

Best.

K. Frank

1 Like