# Is it possible to combine two Sobel kernels into one?

                 sobel-y --> [ 1   2   1]
[ 0   0   0]
[-1  -2  -1]

sobel-x -->  [1  0 -1]
[2  0 -2]
[1  0 -1]


Is it possible to combine two Sobel kernels (x-direction, and y-direction) into one and then convolve that with an image?

The idea is that, I already have an Convolution function written. I would be able to reuse it in that way.

Update:

I tried Fat32's suggestion of applying X and Y kernels sequentially.

Input:

Expected Output:

Present Output:

Looks like my filter isn't producing the correct output.

• I think all you need to do is convolve the two kernels. – MBaz Sep 3 '18 at 23:07
• So you have a convolution function, you will call it twice, with those two directional kernels.... (@MBaz convolving them would not be a good idea, for these are edge detectors) – Fat32 Sep 3 '18 at 23:32
• @MBaz, your suggestion isn't working. – user36563 Sep 4 '18 at 0:33
• I do not suggest sequential filtering..? you will filter the original image two times in parallel one with hx and the other with hy producing two different images, then their squares are added. I assume you want a Sobel edge detector... – Fat32 Sep 4 '18 at 0:42
• @Fat32 My idea was that, if $H_x$ and $H_y$ are the OP's two matrices, and $I$ is the image, then one could calculate $(H_x \ast H_y) \ast I$ since convolution is associative. Of course, if you do non-linear operations in between, then this wouldn't work, as you point out in your answer. – MBaz Sep 4 '18 at 12:58

A Sobel edge detector compares an approximation of the image gradient $|\nabla f_c(x,y)|$ to a threshold to decide if a pixel is an edge or not. A proper threshold must be determined and computed so that the comparison produces useful results. The approximation is given by the following:
$$|\nabla f_c(x,y)| \longrightarrow \sqrt{ (f \star h_x)^2 + (f \star h_y)^2 }$$
where $f = f[n_1,n_2]$ is the discrete-time image sequence, and $h_x = h_x[n_1,n_2]$ and $h_y = h_y[n_1,n_2]$ are the two directional Sobel kernels respectively.
As can be seen the Sobel operator is nonlinear in the filters $h_x$ and $h_y$, and a single kernel cannot be obtained, which would replace those two directional kernels.