# Implementation of four kernel sobel edge detection

Hi guys, recently i have read a journal, which implemented sobel mask based on 4 kernels : horizontal, vertical and right - left diagonal

As I know, in opencv, cvSobel only supports for horizontal and vertical changes. I need to include right and left diagoals.

I have tried to implement this method in opencv, and i use custom filter. Here is my code :

```
//Sobel edge detection dengan custom kernel
Mat kern1 = (Mat_<float>(3,3)<<1.,2.,1.,0.,0.,0.,-1.,-2.,-1.); // horizontal kernel
Mat kern2 = (Mat_<float>(3,3)<<-1.,0.,1.,-2.,0.,2.,-1.,0.,1.); // vertical kernel
Mat kern3 = (Mat_<float>(3,3)<<0.,1.,2.,-1.,0.,1.,-2.,-1.,0.); // left diagonal
Mat kern4 = (Mat_<float>(3,3)<<-2.,-1.,0.,-1.,0.,1.,0.,1.,2.); // right diagonal
Mat res;
filter2D(grayImg,res,grayImg.type(),kern2);
filter2D(res,res,res.type(),kern1);
filter2D(res,res,res.type(),kern3);
filter2D(res,res,res.type(),kern4);
namedWindow("sobel",0);
imshow("sobel",res);
waitKey(0);
```

I have got the result, but i am still doubt, is it valid to do sobel edge detection by using filter2D four times ? Or am I missing something ? Thanks.

Must we guess your problem ? I can see one problem : a waitKey() is missing at the end of your program.

I have edit my post. Basically, my problem is, is it the correct way to do sobel edge detection with four kernels ? Thanks.

If you want to estimate fourth derivatives why not? If you haven't got a specific question i propose to close your question...

for first derivative in a specific direction

I am sorry, if i am still confused. Does it mean, my code estimates for fourth derivative ?

yes it is. In res you have k1 * k2 * k3 *k4 * grayImg where * is convolution

have a look at the docs

then, i guess, that kern1 == kern2 is a copy/paste tyop ;)

Thanks in advance :D

Oh right then. i am sorry. I will edit it.