# Understanding the application of Sobel kernel followed by ReLU to a zero-padded image

Let's say I have a $$2 \times 2$$ pixel of grayscale picture, where there is one edge such that the left pixel contains a value, 30, and the right pixels contain a value 0 (in red below). And for edge detection I have zero-padded the input image and then used the Sobel vertical filter to find out the vertical edges and apply ReLU to the output. The output is a $$2 \times 2$$ matrix with all pixel values $$0$$. So that should mean there is no edge in the picture whereas in actual case it has one. Where am I going wrong?

I assume that you would like to use convolution with padding with the same size of output matrix as this picture. You messed up calculations with convolution with "full padding". If we imagine these two matrices as windows that slide on each other, you can see that the filter is symmetrically inverted. I used a little bit different filter to show you in a better way how it works (I changed the last row to [-3, 0 3]).

Assuming these matrices:

You should add to your picture matrix two rows and columns of zeros:

Then you can start matrix multiplication, but notice that the filter is symmetrically inverted. The result shown as matrix $$4 \times 4$$ is the convolution with "full padding". The $$2 \times 2$$ matrix in the middle is a result of convolution with the "same padding", that you requested.

Next step:

Some iterations later:

And later:

And finally:

For convolution with the same size, the result will be the small $$2 \times 2$$ matrix in the middle.

After using the ReLU function, the result will be exactly the same.

So with using of your filter, the result would look like [[0,90], [0,90]].

• Totally appreciate your detailed explanation. But it would really help if you can educate me further on the below 2 questions. 1. Why did you choose the different filter? I mean is it completely random or there is a process to arrive to the correct filter. 2. Since the pixel value (0 -> 30 -> 0) changes in in same proportion in the input image so in the convolved image if i use your filter then why are we getting different values (90, 150)? I guess the ixels in the convolved image gives the rate of change of pixel value in the original picture right? Shouldn't theybe same? – Abhranil Mukhopadhyay Jan 5 '19 at 13:04
• Ad 1. Your filter is OK. I decided to change values in last row to make it more visible, that matrix is "flipped" in both axis. That can be confusing. Ad 2. That's the metter of values in my filter and those "3s" in last row. Pure math. With using of your filter, the result is (90,90). – ketzul Jan 6 '19 at 14:16