Matlab gradient equivalent in opencv

前端 未结 5 983
感情败类
感情败类 2020-12-10 21:54

I am trying to migrate some code from Matlab to Opencv and need an exact replica of the gradient function. I have tried the cv::Sobel function but for some reason the values

5条回答
  •  青春惊慌失措
    2020-12-10 22:41

    Pei's answer is partly correct. Matlab uses these calculations for the borders:

    G(:,1) = A(:,2) - A(:,1); G(:,N) = A(:,N) - A(:,N-1);

    so used the following opencv code to complete the gradient:

    static cv::Mat kernelx = (cv::Mat_(1, 3) << -0.5, 0, 0.5);
    static cv::Mat kernely = (cv::Mat_(3, 1) << -0.5, 0, 0.5);
    cv::Mat fx, fy;
    
    cv::filter2D(Image, fx, -1, kernelx, cv::Point(-1, -1), 0, cv::BORDER_REPLICATE);
    cv::filter2D(Image, fy, -1, kernely, cv::Point(-1, -1), 0, cv::BORDER_REPLICATE);
    
    fx.col(fx.cols - 1) *= 2;
    fx.col(0) *= 2;
    fy.row(fy.rows - 1) *= 2;
    fy.row(0) *= 2;
    

提交回复
热议问题