Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemente this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemente this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

Update gradient(A4,4). These 4s are used to specify the kernel size to calculate the derivative for each number.

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemente this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

Update Update

gradient(A4,4). These 4s are used to specify the kernel size to calculate the derivative for each number.

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemente implemented this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

Update

gradient(A4,4). These 4s are used to specify the kernel size to calculate the derivative for each number.

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemented this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

Update

gradient(Agradient(A,4,4). These 4s are used to specify the kernel size to calculate the derivative for each number.

matlab gradient() to C++

Hello,

I am trying to port the method gradient (Matlab) to C++ with OpenCV:

I tested this in matlab:

Input:

 A =
 1     3
 4     2

[dx dy] = gradient(A, 4, 4)

Output:

dx =
0.5000    0.5000
-0.5000   -0.5000
dy =
0.7500   -0.2500
0.7500   -0.2500

I followed this example :

And I implemented this code:

float A[2][2] = {{1.0,3.0},{4.0,2.0}};
Mat src_grad = Mat(2,2,CV_32F,A);

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Mat grad;

/// Gradient X
Sobel( src_grad, grad_x, CV_32F, 1, 0);
convertScaleAbs( grad_x, abs_grad_x );

/// Gradient Y
Sobel( src_grad, grad_y, CV_32F, 0, 1);
convertScaleAbs( grad_y, abs_grad_y );

/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

But grad doesn't have the same result. The problem is in the gradient method and its second and third parameter ( gradient(A,4,4)) which specify the 'spacing'. Is it correct this code? How I can specify this spacing in this example?

Update

gradient(A,4,4). These 4s are used to specify the kernel size to calculate the derivative for each number.

It was returning matrices with values to 0. I tested a matrix 3x3 (because Sober works with a ksize=3) and now, I returns values !=0...