# 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) 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... 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 seen, the Sobel operator is nonlinear in the filters $$h_x$$ and $$h_y$$, and a single kernel, which would replace those two directional kernels, cannot be obtained in this case.

• Question edited.
– user36563
Sep 4 '18 at 0:28

I stumbled across that wikipedia "ouput" image as well and no matter what I did with my kernels or values, my output never matched the example output on wiki.

I took a close look at what kind of edges are shown and recognized, that the sobel filter is applied two times!

So you take hy and hx and run your usual sobel on your input image. Then you take a (horizontally) flipped version of hy and a (vertically) flipped version of hx and apply sobel again to the input image! Now you add the results of each corresponding cell in the two resulting images/matrices together.... Do some normalizing/cutting/whatever.... and voila, the Wikipedia output image.

I tried to google the topic....I found no indication why and if sobel should be applied twice. So I don't get the reason why such misleading "output example" is used on wikipedia.