Since matlab imgradient () returns a gradient value (e.g. sqrt (dx (x, y) ² + dy (x, y) ²) for each pixel with x, y coordinates), you can do something like this:
// 1) Get the horizontal gradient Mat kH = (cv::Mat_<double>(1,3) << -1,0,1); // differential kernel in x Mat Dx; filter2D(image, Dx, -1, kH, cv::Point(-1,-1), 0); // 2) Get the vertical gradient Mat kV = (cv::Mat_<double>(3,1) << -1,0,1); // differential kernel in y Mat Dy; filter2D(image, Dy, -1, kV, cv::Point(-1,-1), 0); // 3) Get sqrt(dx²+dy²) in each point for(int i=0; i<Dx.rows; i++) for(int j=0; j<Dx.cols; j++) Dmag.at<double>(i,j) = sqrt(pow(Dx.at<double>(i,j),2)+pow(Dy.at<double>(i,j),2));
He should get what you want. You can achieve better performance by accessing gradient data instead of using .at (i, j) for each pixel.
Hope this helps!
source share