Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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.,0.,1.,-2.,0.,2.,-1.,0.,1.); // vertical kernel
Mat kern2 = (Mat_<float>(3,3)<<-1.,0.,1.,-2.,0.,2.,-1.,0.,1.); // horizontal 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);

Is it valid to do that ? Or am I missing something ? Thanks.

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

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.,0.,1.,-2.,0.,2.,-1.,0.,1.); // vertical kernel
Mat kern2 = (Mat_<float>(3,3)<<-1.,0.,1.,-2.,0.,2.,-1.,0.,1.); // horizontal 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);

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

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.,0.,1.,-2.,0.,2.,-1.,0.,1.); // vertical kernel
Mat kern2 = (Mat_<float>(3,3)<<-1.,0.,1.,-2.,0.,2.,-1.,0.,1.); // horizontal 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.

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.,0.,1.,-2.,0.,2.,-1.,0.,1.); (Mat_<float>(3,3)<<1.,2.,1.,0.,0.,0.,-1.,-2.,-1.); // vertical horizontal kernel Mat kern2 = (Mat_<float>(3,3)<<-1.,0.,1.,-2.,0.,2.,-1.,0.,1.); // horizontal 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 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.

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

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.