# cv::Mat float values and unsigned int values showing differentials

Hi. I want to learn if any matrix element is calculating with different method when using float cast to get pixel value. I mean, image.at<float>(row, col) is quite different then image.at<unsigned int>(row, col). I want to calculate vertical and horizontal projection with accessing pixel values and draw it like a histogram. But when I read pixel with typename float result value is 0 < result << 1. Am I missing something?

Edit 1:

Mat histogram;
double min = 0;
double max = 0;
int htp = static_cast<int>(0.9 * image.cols);
minMaxLoc(image, &min, &max, 0, 0);
histogram = Mat((int)max * 2, image.cols * 2, CV_8U, Scalar(255));

for (int i = 0; i < image.cols; ++i)
{
float binVal = image.at<float>(0, i);
// std::cout << "Float = " << image.at<float>(0, i) << ", Uchar = " << image.at<unsigned int>(0, i) << std::endl;
int intensity = static_cast<int>(binVal * hpt / max);
line(histogram, Point(i, histogram.rows - 1), Point(i, (histogram.rows - intensity)), Scalar::all(0), 3);
// std::cout << "Point 1: x = " << i << ", y = " << histogram.rows - 1 << std::endl;
// std::cout << "Point 2: x = " << i << ", y = " << histogram.rows - intensity << std::endl;
// std::cout << "BinVal = " << binVal << ", hpt = " << hpt << ", y = " << y << ", x = " << x << std::endl;
// std::cout << "Intensity = " << intensity;
// std::cout << std::endl;
}


Commented lines are for see results. I want to show vertical and horizontal projection but when I run above code all intensity value. image has 1 x original_image.cols dimension for vertical projection and 1 x original_image.rows for horizontal projection and original image is a gray scale image. First, I calculate the 1D pixel sums then I normalized these values with 256 now I want to see these distribution like histogram but calculated intensity value is 0 so I'm just getting one horizontal line. But when I changed (below) the values double and float to int I can see the distribution. Am I right way to find projections?

Mat histogram;
int x = 0;
int y = 0;
// int htp = static_cast<int>(0.9 * image.cols);
// find max
for (int i = 0; i < image.cols; ++i)
{
if (y < image.at<int>(0, i)))
y = image.at<int>(0, i);
}
x = image.cols * 2;
histogram = Mat(y * 2, x, CV_8U, Scalar(255));

for (int i = 0; i < image.cols; ++i)
{
// float binVal = static_cast<float>(image.at<int>(0, i));
// std::cout << "Float = " << image.at<float>(0, i) << ", Uchar = " << image.at<unsigned int>(0, i) << std::endl;
// int intensity = static_cast<int>(binVal * hpt / y);
line(histogram, Point(i, histogram.rows - 1), Point(i, (histogram.rows - image.at<unsigned int>(0, i))), Scalar::all(0), 3);
// std::cout << "Point 1: x = " << i << ", y = " << histogram.rows - 1 << std::endl;
// std::cout << "Point 2: x = " << i << ", y = " << histogram.rows - image.at<unsigned int>(0, i) << std::endl;
// std::cout << "BinVal = " << binVal << ", hpt = " << hpt << ", y = " << y << ", x = " << x << std::endl;
// std::cout << "Intensity = " << intensity;
// std::cout << std::endl;
}


Vertical Projection with image.at<float>

Horizontal Projection withimage.at<float>

Vertical Projection ...

edit retag close merge delete

Can you post some code example?

( 2015-07-03 15:02:38 -0500 )edit

Sort by » oldest newest most voted

First I haven't image declaration. I have supposed this :

RNG r;
Mat image(64,64,CV_32S);
r.fill(image,RNG::UNIFORM,-128000,128000);

std::cout << "Float = " << image.at<float>(0,28) << ", Uchar = " << image.at<unsigned int>(0, 28) << std::endl;


This line is not good but it depends how you compile your code. In debug there is an exception

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si ze.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * cha nnels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file F:\lib\opencv\modules\ core\include\opencv2/core/mat.inl.hpp, line 894

and in release something like this happen

Float = -1.#QNAN, Uchar = 4294854902

You cannot access to image data with a wrong using at type because there is some check in method at. I am not sure but I think there is no unsigned int for type only signed int exist.

PS I hope I well understand your question

more

Thanks for the answer. I didn't get any exception in debug mode with my code (for this code) and I didn't try yours. I added four result image.

( 2015-07-05 18:59:10 -0500 )edit

Official site

GitHub

Wiki

Documentation