Skip to content Skip to sidebar Skip to footer

How To Implement Imgradient() Function Of Matlab In Opencv Android Java

I want to use the imgradient() function of matlab in my android application using opencv. how can i do so and which function of opencv is equivalent to to Matlab imgradient() funct

Solution 1:

Have you tried using something like sobel or canny operators?

Solution 2:

As matlab imgradient() returns the gradient "magnitude" (i.e. sqrt(dx(x,y)² + dy(x,y)²) for each pixel with coordinates x,y), you may want to do something like:

// 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 pointfor(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));

It should get you what you you want. You can achieve a better performance by accessing gradient data instead of using .at(i,j) for each pixel.

Hope it helps!

Post a Comment for "How To Implement Imgradient() Function Of Matlab In Opencv Android Java"