Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Sobel Operator

Tags:

c++

opencv

vision

I am trying to implement a sobel operator in both horizontal and vertical direction. But somehow I am getting the reverse output. The code I have attached below. For the horizontal mask

char mask [3][3]=  {{-1,-2,-1},{0,0,0},{1,2,1}};

void masking(Mat image){

Mat temImage= image.clone();
for (int i = 1; i < image.rows-1; i++)
{
    for (int j = 1; j < image.cols-1; j++)
    {   
        for(int k=0;k<3;k++)
        {
            int pixel1 = image.at<Vec3b>(i-1,j-1)[k] * -1;
            int pixel2 = image.at<Vec3b>(i,j-1)[k] * -2;
            int pixel3 = image.at<Vec3b>(i+1,j-1)[k] * -1;

            int pixel4 = image.at<Vec3b>(i-1,j)[k] * 0;
            int pixel5 = image.at<Vec3b>(i,j)[k] * 0;
            int pixel6 = image.at<Vec3b>(i+1,j)[k] * 0;

            int pixel7 = image.at<Vec3b>(i-1,j+1)[k] * 1;
            int pixel8 = image.at<Vec3b>(i,j+1)[k] * 2;
            int pixel9 = image.at<Vec3b>(i+1,j+1)[k] * 1;

            int sum = pixel1 + pixel2 + pixel3 + pixel4 + pixel5 + pixel6 + pixel7 + pixel8 + pixel9;
            if(sum < 0)
            {
                sum = 0;
            }

            if(sum > 255)
                sum = 255;

            temImage.at<Vec3b>(i,j)[k] = sum;


        }
    }
}
//printf("conter = %d",counter);
imshow( "Display", temImage );
imwrite("output1.png",temImage);

}

I am getting the output as

enter image description here

where as for the vertical mask

char mask [3][3]=  {{-1,0,1},{-2,0,2},{-1,0,1}}; 

void masking(Mat image){

Mat temImage= image.clone();
for (int i = 1; i < image.rows-1; i++)
{
    for (int j = 1; j < image.cols-1; j++)
    {   
        for(int k=0;k<3;k++)
        {
            int pixel1 = image.at<Vec3b>(i-1,j-1)[k] * -1;
            int pixel2 = image.at<Vec3b>(i,j-1)[k] * 0;
            int pixel3 = image.at<Vec3b>(i+1,j-1)[k] * 1;

            int pixel4 = image.at<Vec3b>(i-1,j)[k] * -2;
            int pixel5 = image.at<Vec3b>(i,j)[k] * 0;
            int pixel6 = image.at<Vec3b>(i+1,j)[k] * 2;

            int pixel7 = image.at<Vec3b>(i-1,j+1)[k] * -1;
            int pixel8 = image.at<Vec3b>(i,j+1)[k] * 0;
            int pixel9 = image.at<Vec3b>(i+1,j+1)[k] * 1;

            int sum = pixel1 + pixel2 + pixel3 + pixel4 + pixel5 + pixel6 + pixel7 + pixel8 + pixel9;
            if(sum < 0)
            {
                sum = 0;
            }

            if(sum > 255)
                sum = 255;

            temImage.at<Vec3b>(i,j)[k] = sum;


        }
    }
}
//printf("conter = %d",counter);
imshow( "Display", temImage );
imwrite("output1.png",temImage);

}

I am getting output as

enter image description here

The main function is attached below

int main( int argc, char** argv ){
Mat input_image = imread("sobel1.jpg",1);
masking(input_image);
waitKey(0);
return 0;

}

According the the guide https://www.tutorialspoint.com/dip/sobel_operator.htm I should get reverse output. Can anyone help me in this

The original image is

enter image description here

like image 485
Abhishek Avatar asked Aug 23 '17 06:08

Abhishek


People also ask

How is Sobel filter applied?

Sobel filtering involves applying two 3 x 3 convolutional kernels (also called filters) to an image. The kernels are usually called G x and G y, and they are shown in the following figure. These two kernels detect the edges in the image in the horizontal and vertical directions.

What is Sobel in image processing?

The Sobel method, or Sobel filter, is a gradient-based method that looks for strong changes in the first derivative of an image. The Sobel edge detector uses a pair of 3 × 3 convolution masks, one estimating the gradient in the x-direction and the other in the y-direction.

What is the advantage of using Sobel operator?

The primary advantages of the Sobel operator lie in its simplicity. The Sobel method provides a approximation to the gradient magnitude. Another advantage of the Sobel operator is it can detect edges and their orientations.


1 Answers

No, the tutorial is not wrong, it talks about masks and not gradients. The weak point of that tutorial is that it doesn't mention we are calculating horizontal gradients using what they call the vertical mask.

like image 84
fireant Avatar answered Nov 08 '22 09:11

fireant