0
votes

Here is my horizontal gradient results.The left one is opencv result and the other one is matlab result

I am trying to do horizontal and vertical gradient which H =[1,-1] and V=[1;-1]

    Mat H_gradient,G_Filter1,kernel,V_gradient;

    Mat kernelH(1, 2, CV_32F);  
    kernelH.at<float>(0,0) = 1.0f;
    kernelH.at<float>(0,1) = -1.0f;


    Mat kernelV(2, 1, CV_32F);
    kernelV.at<float>(0,0) = 1.0f;
    kernelV.at<float>(1,0) = -1.0f;

    cvtColor( image, image, CV_RGB2GRAY );

    filter2D( image, H_gradient, -1 ,kernelH , Point( -1, -1 ), 0, BORDER_DEFAULT ); 
    filter2D( image, V_gradient, -1 ,kernelV , Point( -1, -1 ), 0, BORDER_DEFAULT );

But still not match with my matlab code results. I dont know why?

My matlab code for gradients

image=double(image);
% horizontal and vertical gradient
H=[1 -1];
V=[1;-1];

H_Gradient=conv2(image,H,'same'); 
V_Gradient=conv2(image,V,'same');
2
Can you show us the difference in an example image?ypnos
I add the image left one is opencv result @ypnosMerveMeriç

2 Answers

0
votes

try do

cvtColor( image, image, **CV_BGR2GRAY** );

instead of

cvtColor( image, image, **CV_RGB2GRAY** );

If you are using the default imread parameters, OpenCv use BGR color format instead of RGB as default!

0
votes

Do the same that you did in Matlab, first convert your image to double.

image.convertTo(image, CV_32F);

Now I got the same result in OpenCv and Matlab.