Ask Your Question
0

Sobel operator gives too large values

asked 2018-02-03 11:30:30 -0600

psi gravatar image

updated 2018-02-03 11:54:19 -0600

berak gravatar image

I've applied Sobel operator to my image and obtained very large values (e.g 1347440720, -1970897035, ...). Is it normal? Should I scale it using convertScaleAbs( grad_x, abs_grad_x )? My snippet of code is here. What is more interesting minmaxLoc gives uninterpretable values too. Help me please with this case.

Mat prev_frame = imread("1.jpg", 0);
Mat frame = imread("2.jpg", 0);
Mat grad_x, grad_y;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
cout << grad_x << endl;
Sobel(prev_frame, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
Sobel(prev_frame, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

Mat diff = frame - prev_frame;
double min, max;
minMaxLoc(grad_y, &min, &max);
cout << min << endl;
cout << max << endl;

for (int i = 0; i < grad_x.cols; i++)
{
    for (int j = 0; j < grad_x.rows; j++)
    {
        cout << grad_x.at<int>(i, j) << endl;
    }
}

OUTPUT:

51968
65523
0
0
0
.
.
.
1532713819
1532713819
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-02-03 14:04:33 -0600

LBerger gravatar image

updated 2018-02-03 14:47:56 -0600

CV_16S means signed short this line is wrong, also you got i,j wrong.

(opencv is row-major)

    cout << grad_x.at<int>(i, j) << endl;

It should be

    cout << grad_x.at<short>(j,i) << endl;

I use this program to test ( frame unused) grad_x empty don't need to print....

{
    Mat prev_frame=imread("g:/lib/opencv/samples/data/fruits.jpg",IMREAD_GRAYSCALE);
    resize(prev_frame, prev_frame, Size(8, 8));
    cout << prev_frame << " \n";
    Mat grad_x, grad_y;
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;
    Sobel(prev_frame, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
    Sobel(prev_frame, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
    cout << grad_x << endl;

    double min, max;
    minMaxLoc(grad_x, &min, &max);
    cout << min << endl;
    cout << max << endl;

    /* ALREADY in cout <<grad_x
    for (int j = 0; j < grad_x.rows; j++)
    {
           for (int i = 0; i < grad_x.cols; i++)
           {
            cout << grad_x.at<short>(j, i)<<"\t" ;
        }
        cout << endl;
    }*/
}
edit flag offensive delete link more

Comments

minMaxLoc result still look weird

berak gravatar imageberak ( 2018-02-03 14:28:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-03 11:30:30 -0600

Seen: 276 times

Last updated: Feb 03 '18