wolvorinePk
wolvorinePk

Reputation: 1790

How to extract Numerical gradient matrix from the image matrix in Opencv

i am searching a way to get Numerical gradient from the matrix. The same function is implemented in matlab's default documentation.http://www.mathworks.com/help/techdoc/ref/gradient.html but i couldn't find any in opencv.

I want to port this to C++ using opencv.

should i use sobel for horizontal and vertical gradient or any other function or way to do it???

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;

/// Gradient X
Sobel( mat, grad_x, CV_32F, 1, 0, 3);
imshow("xx",grad_x);
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
Sobel( mat, grad_y, CV_32F, 0, 1, 3);
convertScaleAbs( grad_y, abs_grad_y );

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

[EDIT] solution

Mat grad_x,abs_grad_x,grad_y,abs_grad_y;
int type=CV_64F ; 
Gradient.setTo(Scalar::all(0));
/// Gradient Y
Sobel( input, grad_x, type, 1, 0, 3);
convertScaleAbs(grad_x,abs_grad_x);
cv::accumulateSquare(abs_grad_x,Gradient);
/// Gradient Y
Sobel(input, grad_y, type, 0, 1, 3);
convertScaleAbs(grad_y,abs_grad_y);
cv::accumulateSquare(abs_grad_y,Gradient);

    imshow("gradient Mag",Gradient);

Upvotes: 2

Views: 4741

Answers (1)

user349026
user349026

Reputation:

You can find the gradient calculation here Just like you have said to calculate sobel gradients, the example does so.

Upvotes: 1

Related Questions