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;
}
Edit 2: Added result images.
Vertical Projection with image.at<float>
Horizontal Projection withimage.at<float>
Vertical Projection ...
Can you post some code example?