来自opencv和Matlab的Sobel滤波器输出不同

时间:2013-09-04 12:05:49

标签: c++ matlab opencv image-processing

我正在将一些代码从matlab转换为opencv。我尝试在opencv中使用Sobel但是opencv和matlab的输出完全不同可能是原因。如何使opencv的输出与matlab相同?我的MATLAB代码是:

 [sobel_edges,T,V,H] = edge(rgb2gray(im),'sobel',0.03);
  sobel_angles = atan2(V,H); 
  sobel_weights = (V.*V+H.*H).^0.5;

其中0.03是阈值。在opencv中,当我使用预构建的Sobel滤波器时,输出与matlab的输出完全不同,即使是在openc中计算的engle和幅度也不同。 opencv代码是:

Mat gray_img=Mat::zeros(img.size(),CV_8U);
Mat gradientX=Mat::zeros(gray_img.size(),CV_64F);
Mat gradientY=Mat::zeros(gray_img.size(),CV_64F);
Mat sobel_edge=Mat::zeros(gray_img.size(),CV_64F);
cvtColor(img, gray_img, CV_BGR2GRAY);
Sobel(gray_img, gradientX, gradientX.type(), 1, 0, 3);
Sobel(gray_img, gradientY, gradientY.type(), 0, 1, 3);
Sobel(gray_img,sobel_edge,sobel_edge.type(),1,1,3);
sobel_edge.convertTo(sobel_edge,CV_8U);
sobel_edge.convertTo(sobel_edge,CV_64F);
sobel_edge=sobel_edge/255.0; //I divided this my 255 becuz in MATLAB the output is between 0 to 1
imshow("Sobel",sobel_edge);

   Mat magnitude(gray_img.size(), CV_64F, cv::Scalar(0.0));
    Mat angles=Mat::zeros(gradientX.size(),CV_64F);
    bool anglesInDegrees = true;
    cartToPolar(gradientX, gradientY, magnitude, angles, anglesInDegrees);

sobel边缘本身也是不同的,幅度和角度也是不同的,我试图通过查看matlab中的边缘函数手动转换opencv中的sobel,但输出仍然不同,因为它结果是opencv和imfilter的filter2D在matlab中返回不同的输出。如何在matlab和opencv中获得相同的sobel输出?将slab of matlab手动转换为opencv的代码是:

Mat gray_img=Mat::zeros(img.size(),CV_32FC1);
  cvtColor(img, gray_img, CV_RGB2GRAY);
  double minVal,maxVal;
  cv::Mat gray = cv::Mat(gray_img.size(),CV_32FC1);
  gray_img.convertTo(gray_img,CV_32FC1);
  gray=gray_img/255.0;
  cout<<gray<<endl<<"End";
  double data[]={1,2,1,0,0,0,-1,2,-1};
  Mat op=Mat(3,3,CV_64F,data).clone();
  op=op/8;
  Mat x_mask;
  transpose(op,x_mask);
  cout<<x_mask<<endl;
  Mat y_mask=op.clone();
  int scale=4;
  int offset[]={0,0,0,0};
  double sobel_thresh=0.03;
  Mat bx,by,bx_mul,by_mul,b;
  Point anchor(0,0);
  float delta = 0.0;
  cv::filter2D(gray, bx, CV_32FC1, x_mask, anchor, delta, BORDER_REPLICATE);
   bx=abs(bx);
   imshow("f1",bx);

  cv::filter2D(gray, by, CV_32FC1, y_mask, anchor, delta, BORDER_REPLICATE);
   by=abs(by);
  imshow("by",by);

  pow(bx,2,bx_mul);
      imshow("f2",bx_mul);
  pow(by,2,by_mul);
  b= bx_mul+by_mul;
   imshow("f3",b);

  double cut_off;
  cut_off=pow(sobel_thresh,2);
  Mat sobel_edge(gray.size(),CV_32FC1);
    for(int i=0;i<b.rows;i++)
    {
        for(int j=0;j<b.cols;j++)
        {
            if((b.at<float>(i,j))>cut_off)
            {
                sobel_edge.at<float>(i,j)=1;
            }
            else
            {
               sobel_edge.at<float>(i,j)=0;
            }
        }
    }
    imshow("Sobel_edge",sobel_edge);

1 个答案:

答案 0 :(得分:1)

这段代码给出了与MATLAB代码相同的结果:

int main(int argc, char* argv[])
{
    namedWindow("result");
    Mat img=imread("D:\\ImagesForTest\\1.tiff",0);
    img.convertTo(img,CV_32FC1,1.0/255.0);
    Mat h,v,g;
    cv::Sobel(img,h,-1,1,0,3,1.0/8.0);
    cv::Sobel(img,v,-1,0,1,3,1.0/8.0);
    cv::magnitude(h,v,g);

    // Check extremums
    double m,M;
    cv::minMaxLoc(g,&m,&M);
    cout << m << ":" << M << endl;
    cv::minMaxLoc(h,&m,&M);
    cout << m << ":" << M << endl;
    cv::minMaxLoc(v,&m,&M);
    cout << m << ":" << M << endl;
    imshow("result",g);
    cv::waitKey(0);
}

OpenCV从不缩放卷积结果,所以要小心。