I am calculating the gradient of grayscale images using OpenCV, the resulting values for some pixels are negative (but i am unable to print them on screen).
my Code:
Mat gradX, kernelX, gray;
//i load an rgb image and convert it to grayscale here
gray.create(rows,cols,CV_32FC1);
kernelX = (Mat_<float>(1,3) << -1, 0, 1);
gradX.create(rows,cols,CV_32FC1);
filter2D(gray,gradX,-1, kernelX,cvPoint(-1, -1),0);
I have tried these 2 methods to print the values of 5×5 top left corner of gradX
Method 1 (it prints all values very small (e.g. 1.23e-40 . etc)):
for (int j=0; j<5;j++)
{
for (int i=0; i<5;i++)
{
cout<< (float)gradX.at<float>(j, i) << " ";
}
cout<< endl;
}
Method 2 (it prints positive values fine, but all negative values are displayed as “0”):
for (int j=0; j<5;j++)
{
uchar* data=gradX.ptr<uchar>(j);
for (int i=0; i<5;i++)
{
cout<< (float)data[i] << " ";
}
cout<< endl;
}
Why don’t you
?