Implementation of four kernel sobel edge detection

asked 2017-04-30 07:09:21 -0600

Kenny Karnama gravatar image

updated 2017-04-30 07:56:04 -0600

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.

edit retag flag offensive close merge delete

Comments

1

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

LBerger gravatar imageLBerger ( 2017-04-30 07:21:02 -0600 )edit

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

Kenny Karnama gravatar imageKenny Karnama ( 2017-04-30 07:35:15 -0600 )edit

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

filter2D(grayImg,res1,grayImg.type(),kern1);
filter2D(grayImg,res2,grayImg.type(),kern2);
filter2D(grayImg,res3,grayImg.type(),kern3);
filter2D(grayImg,res4,grayImg.type(),kern4);

for first derivative in a specific direction

LBerger gravatar imageLBerger ( 2017-04-30 07:38:52 -0600 )edit

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

Kenny Karnama gravatar imageKenny Karnama ( 2017-04-30 07:44:42 -0600 )edit

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

LBerger gravatar imageLBerger ( 2017-04-30 07:48:10 -0600 )edit

have a look at the docs

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

berak gravatar imageberak ( 2017-04-30 07:49:10 -0600 )edit

Thanks in advance :D

Kenny Karnama gravatar imageKenny Karnama ( 2017-04-30 07:49:15 -0600 )edit

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

Kenny Karnama gravatar imageKenny Karnama ( 2017-04-30 07:50:31 -0600 )edit