I’ve just tested Sobel using C api and C++ api. But why is it different? All the parameters I used are the same.
Output – using C API
Output – using C++ API
Edited
C API :
/// Generate grad_x
grad_x = cvCreateImage(cvGetSize(grayImg), IPL_DEPTH_16S, 1);
abs_grad_x = cvCreateImage(cvGetSize(grayImg), 8, 1);
/// Gradient X
cvSobel(grayImg, grad_x, 1, 0, 3);
cvConvertScaleAbs(grad_x, abs_grad_x);
cvThreshold(abs_grad_x, abs_grad_x, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);
C++ API :
cv::Mat img_sobel;
cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT);
Mat img_threshold;
threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
There is only 1 reason for different results. Data-Type!
In the C version, you are creating
grad_xwithIPL_DEPTH_16Sdepth. So each pixel hasshortdata type. This increases the precision of the results which you get when calling thecvSobelfunction.cvSobelis able to accommodate a wider range of values (-32768 to 32767) in the resultgrad_x.In the C++ version, you are not initializing the matrix and specifying the destination type
CV_8U. The functioncv::Sobelinternally creates destination matrix of type CV_8U, calculates the results and then clamps them to the range of destination data type, i.e from 0 to 255. So all the negative values become 0.To get the same results in C version, change the
IPL_DEPTH_16StoIPL_DEPTH_8U.