Set weights for custom filters/kernels

How do I know which weights to choose for filters when I want to use custom filters? I haven’t found anything on the internet.
I’m using 9x9 grayscale images, with pixel values between 0 and 1. Let’s say for example the image looks somehow like this:

[1, 1, 1, 0, ...]
[1, 0, 0, 0, ...]
[1, 0, 0, 0, ...]
[0, 0, 0, 0, ...]
.
.
.

When I want to detect this kind of corner, would I use a filter like this?

[[1., 1., 1.],
[1., 0., 0.],
[1., 0., 0.]]

Yes, you can apply a cross-correlation with a normalized filter returning a peak signal (if normalized it would be 1.) in the output image. Since nn.Conv2d layers are actually using a cross-correlation approach you could initialize the weight with the same shape as your target:

image = torch.zeros(1, 1, 12, 12)
image[:, :, 3:8, 3] = 1.
image[:, :, 3, 3:8] = 1.
print(image)
# tensor([[[[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 0.],
#           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
#           [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]]])

plt.imshow(image[0, 0])

conv = nn.Conv2d(1, 1, 5, padding=2, bias=False)
with torch.no_grad():
    weight = torch.zeros(1, 1, 5, 5)
    weight[:, :, 3:8, 3] = 1.
    weight[:, :, 3, 3:8] = 1.
    weight = weight / weight.sum()
    conv.weight.copy_(weight)

out = conv(image)
print(out)
tensor([[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.3333, 0.3333, 0.3333, 0.3333, 0.3333, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.3333, 1.0000, 0.6667, 0.6667, 0.6667, 0.3333, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.3333, 0.6667, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.3333, 0.3333, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
           0.0000, 0.0000, 0.0000, 0.0000]]]], grad_fn=<ConvolutionBackward0>)

plt.imshow(out[0, 0].detach().numpy())