Ask Your Question

Revision history [back]

That's not an answer. But I must say that's I don't understand results given by my program. One thing I still don't understand this line gradient_x.at<uchar>(r - radius, c - radius) I have changed to gradient_x.at<uchar>(r, c )

Now your program with my change :

Mat img_gray = (Mat_<uchar>(7, 7) <<
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0,
    0,0,1,2,1,0,0,
    0,0,28,3,12,0,0,
    0,0,1,32,11,0,0,
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0);


Mat gradient_x(img_gray.rows, img_gray.cols,CV_16S,Scalar(0));

int sobel_x[3][3] = { { -1,0,1 },{ -2,0,2 },{ -1,0,1 } };
int scharr_x[3][3] = { { -3,0,3 },{ -10,0,10 },{ -3,0,3 } };

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * scharr_x[i+1][j+1];
            }
        }
        gradient_x.at<short>(r , c ) = saturate_cast<short>(s) ;

    }
}
Mat Gx;
cout << img_gray << endl;
Sobel(img_gray, Gx, CV_32F, 1, 0, CV_SCHARR, 1, 0, BORDER_ISOLATED);
cout << "Scharr OPENCV=\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * sobel_x[i + 1][j + 1];
            }
        }
        gradient_x.at<short>(r, c) = saturate_cast<short>(s);

    }
}

Sobel(img_gray, Gx, CV_32F, 1, 0, 1,1,0,BORDER_CONSTANT);
cout << "Sobel OPENCV =\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;

results are :
[  0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   1,   2,   1,   0,   0;
   0,   0,  28,   3,  12,   0,   0;
   0,   0,   1,  32,  11,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0]
Scharr =
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
Sobel =
[0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 28, 3, -16, -3, -12, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 30, 7, -16, -7, -14, 0;
 0, 58, 40, -22, -40, -36, 0;
 0, 30, 67, 4, -67, -34, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0]

So.... A new question why scharr filter is good and not sobel filter?

That's not an answer. But I must say that's I don't understand results given by my program. One thing I still don't understand this line gradient_x.at<uchar>(r - radius, c - radius) I have changed to gradient_x.at<uchar>(r, c )

Now your program with my change :

Mat img_gray = (Mat_<uchar>(7, 7) <<
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0,
    0,0,1,2,1,0,0,
    0,0,28,3,12,0,0,
    0,0,1,32,11,0,0,
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0);


Mat gradient_x(img_gray.rows, img_gray.cols,CV_16S,Scalar(0));

int sobel_x[3][3] = { { -1,0,1 },{ -2,0,2 },{ -1,0,1 } };
int scharr_x[3][3] = { { -3,0,3 },{ -10,0,10 },{ -3,0,3 } };

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * scharr_x[i+1][j+1];
            }
        }
        gradient_x.at<short>(r , c ) = saturate_cast<short>(s) ;

    }
}
Mat Gx;
cout << img_gray << endl;
Sobel(img_gray, Gx, CV_32F, 1, 0, CV_SCHARR, 1, 0, BORDER_ISOLATED);
cout << "Scharr OPENCV=\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * sobel_x[i + 1][j + 1];
            }
        }
        gradient_x.at<short>(r, c) = saturate_cast<short>(s);

    }
}

/// BUG Sobel(img_gray, Gx, CV_32F, 1, 0, 1,1,0,BORDER_CONSTANT);
Sobel(img_gray, Gx, CV_32F, 1, 0, 3,1,0,BORDER_CONSTANT); // GOOD with ksize 3 (I should read the doc!)
cout << "Sobel OPENCV =\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;

results are :
[  0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   1,   2,   1,   0,   0;
   0,   0,  28,   3,  12,   0,   0;
   0,   0,   1,  32,  11,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0]
Scharr =
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
Sobel =
[0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 28, 3, -16, -3, -12, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 30, 7, -16, -7, -14, 0;
 0, 58, 40, -22, -40, -36, 0;
 0, 30, 67, 4, -67, -34, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0]

So.... A new question why scharr filter is good and not sobel filter?

That's not an answer. But I must say that's I don't understand results given by my program. One thing I still don't understand this line gradient_x.at<uchar>(r - radius, c - radius) I have changed to gradient_x.at<uchar>(r, c )

Now your program with my change :

Mat img_gray = (Mat_<uchar>(7, 7) <<
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0,
    0,0,1,2,1,0,0,
    0,0,28,3,12,0,0,
    0,0,1,32,11,0,0,
    0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0);


Mat gradient_x(img_gray.rows, img_gray.cols,CV_16S,Scalar(0));

int sobel_x[3][3] = { { -1,0,1 },{ -2,0,2 },{ -1,0,1 } };
int scharr_x[3][3] = { { -3,0,3 },{ -10,0,10 },{ -3,0,3 } };

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * scharr_x[i+1][j+1];
            }
        }
        gradient_x.at<short>(r , c ) = saturate_cast<short>(s) ;

    }
}
Mat Gx;
cout << img_gray << endl;
Sobel(img_gray, Gx, CV_32F, 1, 0, CV_SCHARR, 1, 0, BORDER_ISOLATED);
cout << "Scharr OPENCV=\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;

// Iterate on image 
for (int r = 1; r < img_gray.rows - 1; ++r)
{
    for (int c = 1; c < img_gray.cols - 1; ++c)
    {
        int s = 0;

        // Iterate on kernel
        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                s += img_gray.at<uchar>(r + i, c + j) * sobel_x[i + 1][j + 1];
            }
        }
        gradient_x.at<short>(r, c) = saturate_cast<short>(s);

    }
}

/// BUG Sobel(img_gray, Gx, CV_32F, 1, 0, 1,1,0,BORDER_CONSTANT);
Sobel(img_gray, Gx, CV_32F, 1, 0, 3,1,0,BORDER_CONSTANT); // GOOD with ksize 3 (I should read the doc!)
cout << "Sobel OPENCV =\n" << Gx << endl;
cout << "gradientx manual=\n" << gradient_x << endl;
 

results are : :

[  0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   1,   2,   1,   0,   0;
   0,   0,  28,   3,  12,   0,   0;
   0,   0,   1,  32,  11,   0,   0;
   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0]
Scharr =
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 3, 6, 0, -6, -3, 0;
 0, 94, 29, -48, -29, -46, 0;
 0, 286, 132, -130, -132, -156, 0;
 0, 94, 329, 52, -329, -146, 0;
 0, 3, 96, 30, -96, -33, 0;
 0, 0, 0, 0, 0, 0, 0]
Sobel =
[0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 30, 7, -16, -7, -14, 0;
 0, 58, 40, -22, -40, -36, 0;
 0, 30, 67, 4, -67, -34, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0]
gradientx=
[0, 0, 0, 0, 0, 0, 0;
 0, 1, 2, 0, -2, -1, 0;
 0, 30, 7, -16, -7, -14, 0;
 0, 58, 40, -22, -40, -36, 0;
 0, 30, 67, 4, -67, -34, 0;
 0, 1, 32, 10, -32, -11, 0;
 0, 0, 0, 0, 0, 0, 0]

results are : [ 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0; 0, 0, 1, 2, 1, 0, 0; 0, 0, 28, 3, 12, 0, 0; 0, 0, 1, 32, 11, 0, 0; 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0] Scharr = [0, 0, 0, 0, 0, 0, 0; 0, 3, 6, 0, -6, -3, 0; 0, 94, 29, -48, -29, -46, 0; 0, 286, 132, -130, -132, -156, 0; 0, 94, 329, 52, -329, -146, 0; 0, 3, 96, 30, -96, -33, 0; 0, 0, 0, 0, 0, 0, 0] gradientx= [0, 0, 0, 0, 0, 0, 0; 0, 3, 6, 0, -6, -3, 0; 0, 94, 29, -48, -29, -46, 0; 0, 286, 132, -130, -132, -156, 0; 0, 94, 329, 52, -329, -146, 0; 0, 3, 96, 30, -96, -33, 0; 0, 0, 0, 0, 0, 0, 0] Sobel = [0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0; 0, 1, 2, 0, -2, -1, 0; 0, 28, 3, -16, -3, -12, 0; 0, 1, 32, 10, -32, -11, 0; 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0] gradientx= [0, 0, 0, 0, 0, 0, 0; 0, 1, 2, 0, -2, -1, 0; 0, 30, 7, -16, -7, -14, 0; 0, 58, 40, -22, -40, -36, 0; 0, 30, 67, 4, -67, -34, 0; 0, 1, 32, 10, -32, -11, 0; 0, 0, 0, 0, 0, 0, 0]

So.... A new question why scharr filter is good and not sobel filter?