c++ - Sobel filter output from opencv and Matlab different -


i converting code matlab opencv. tried use sobel in opencv output of opencv , matlab different reason. how can make ouput of opencv same matlab? matlab code :

 [sobel_edges,t,v,h] = edge(rgb2gray(im),'sobel',0.03);   sobel_angles = atan2(v,h);    sobel_weights = (v.*v+h.*h).^0.5; 

where 0.03 threshold. in opencv when use prebuilt sobel filter output different of matlab engle , magnitude calculated in openc vis different. opencv code is:

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 255 becuz in matlab output between 0 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); 

the sobel edge different , magnitude , angle too, tried convert manually sobel in opencv looking @ edge function in matlab still output different because turns out filter2d of opencv , imfilter in matlab returns different output. how can obtain same output of sobel in matlab , opencv???the code of manually converting sobel of matlab opencv is:

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); 

this code gives me same result matlab code:

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 never scales convolution result, so, careful.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -