3

I have implemented Sobel operator in vertical direction. But the result which I am getting is very poor. I have attached my code below.

int mask_size= 3;

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

void sobel(Mat input_image)
{

/**Padding m-1 and n-1 zeroes to the result where m and n are mask_size**/

Mat result=Mat::zeros(input_image.rows+(mask_size - 1) * 2,input_image.cols+(mask_size - 1) * 2,CV_8UC1);
Mat result1=Mat::zeros(result.rows,result.cols,CV_8UC1);            
int sum= 0;

/*For loop for copying original values to new padded image **/

for(int i=0;i<input_image.rows;i++)
    for(int j=0;j<input_image.cols;j++)
        result.at<uchar>(i+(mask_size-1),j+(mask_size-1))=input_image.at<uchar>(i,j);

GaussianBlur( result, result, Size(5,5), 0, 0, BORDER_DEFAULT );
/**For loop to implement the convolution **/

for(int i=0;i<result.rows-(mask_size - 1);i++)
    for(int j=0;j<result.cols-(mask_size - 1);j++)
    {
        int counter=0;
        int counterX=0,counterY=0;
        sum= 0;
        for(int k= i ; k < i + mask_size ; k++)
        {
            for(int l= j ; l< j + mask_size ; l++)
            {
                sum+=result.at<uchar>(k,l) * mask[counterX][counterY];
                counterY++;
            }
            counterY=0;
            counterX++;
        }
        result1.at<uchar>(i+mask_size/2,j+mask_size/2)=sum/(mask_size * mask_size);
    }

/** Truncating all the extras rows and columns **/

result=Mat::zeros( result1.rows  - (mask_size - 1) * 2, result1.cols - (mask_size - 1) * 2,CV_8UC1);
for(int i=0;i<result.rows;i++)
    for(int j=0;j<result.cols;j++)                      
        result.at<uchar>(i,j)=result1.at<uchar>(i+(mask_size - 1),j+(mask_size - 1));

imshow("Input",result);
imwrite("output2.tif",result);

}

My input to the algorithm is enter image description here

My output is enter image description here

I have also tried using Gaussian blur before actually convolving an image and the output I got is enter image description here

The output which I am expecting isenter image description here

The guide I am using is: https://www.tutorialspoint.com/dip/sobel_operator.htm

Sigur
  • 355
  • 8
  • 19
Abhishek
  • 650
  • 1
  • 8
  • 31
  • 1
    To be honest, it looks fine to me. Try applying gaussian blurring on the image before you run Sobel on it. That should smooth out some of the bumpiness. – ljetibo Aug 09 '17 at 06:01
  • I did tried using Gausian blur but it didn't work for me. The output is highlighted above – Abhishek Aug 09 '17 at 08:48
  • 1
    next time, please take your time and enter your code as code, not as an image so other people can copy it. – Piglet Aug 10 '17 at 18:55
  • Thank You @Piglet. I have edited my document. But I am not sure how it works – Abhishek Aug 14 '17 at 09:58

1 Answers1

1

Your convolution looks ok although I only had a quick look.

Check your output type. It's unsigned char.

Now think about the values your output pixels may have if you have negative kernel values and if it is a good idea to store them in uchar directly.

If you store -1 in an unsigned char it will be wrapped around and your output is 255. In case you're wondering where all that excess white stuff is coming from. That's actually small negative gradients.

The desired result looks like the absolute of the Sobel output values.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • So can you please suggest what shall i do @Piglet and what do you maen by absolute value for sobel – Abhishek Aug 14 '17 at 10:12
  • @Abhishek you shouldn't do image processing or any programming if your maths skills are so horrible. https://en.wikipedia.org/wiki/Absolute_value – Piglet Aug 14 '17 at 15:41
  • 1
    @Abhishek I did not mean to be rude. This was just an advice. You make your programming life harder than necessary. It's like reading a book when you only know 5 letters. You will not be able to make any sense of the story nor will you ever fully enjoy it. Why do you think I spent 4 years with 90% maths in my image processing study at university? the absolute value of a number is it's "positive version". -1 becomes 1, 1 remains 1, 0 is 0. - 5 is 5... you cannot display negative numbers on a screen, so you have to somehow modify them. for example by displaying their absolute value. – Piglet Aug 16 '17 at 15:56
  • Hello @Piglet. It should be nice if you can atleast have minimum common sense not to demotivate and criticize others. If you don't want to answer then you better choose not to do so. Just by abusing someone by telling them not to do programming language or image processing course in platform like stack overflow is a heinous crime. I know I am rude with you but I just felt you should put yourself in others shoes and visualize for yourself. If you feel bad about it sorry. By the way thanks for you answers which you have posted. Check the link for Be nice. https://stackoverflow.com/help/be-nice – Abhishek Aug 16 '17 at 15:58
  • 1
    @Abhishek and if you read absolte value somewhere and you don't know what it means, the first thing you should do is searching the web for that term. If I would just give you the correct line of code you would use it and never think about it again, hence you wouldn't learn a thing. what do you think doctors would say if you came into the surgery room, starting to cut around in the patient cluelessly and then ask them what they mean with blood pressure? – Piglet Aug 16 '17 at 15:59