Ask Your Question

Revision history [back]

Black image problem while multiplying intensity with a float

Hi, I have a 1D array containing floating values and I want to multiply the corresponding values to the intensity of a chosen color to get a sense of the values of an array.

 for (int j = 0; j < 39; j++)
{
    for (int i = 0; i < 39; i++)
    {
        imtest.at<cv::Vec4b>(i,j)[0] = 255 * ro[j*39 + i];
        imtest.at<cv::Vec4b>(i,j)[1] = 0;
        imtest.at<cv::Vec4b>(i,j)[2] = 0;
        imtest.at<cv::Vec4b>(i,j)[3] = 150*ro[j*39 + i];
    }

}

This is how I do it. The values in the middle of that array have higher values than one(1.01-1.1) hoping that these values will give me the shape of the "bubble". However, I get a black bubble with a blue border instead of a blue one.

So trying to find the issue, I expanded the code as below.

int n = 1;
double* k = new double[n];
k[0] = 1.08455576996775482;
double test = ro[39*15 + 15];
//k[0] = test;
for (int j = 0; j < 39; j++)
{
    for (int i = 0; i < 39; i++)
    {
        imtest2.at<cv::Vec4b>(i,j)[0] = 255 * k[0];//ro[15*39 + 15];//ro[j*39 + i];
        imtest2.at<cv::Vec4b>(i,j)[1] = 0;//255 * 1.2;
        imtest2.at<cv::Vec4b>(i,j)[2] = 0;//255 * 1.2;
        imtest2.at<cv::Vec4b>(i,j)[3] = 150 * ro[j*39 + i];
    }

}
std::cout.precision(18);
std::cout << ro[15*39 + 15] << std::endl;
std::cout << k[0] << std::endl;
std::cout << 1.08455576996775482 << std::endl;

cv::imwrite("color.png",imtest2);
cv::imwrite("slice.png",imtest);

The last section with 3 print statements have exactly the same value of 1.08455576996775482 on the console but if I assign the value of ro[15*39 + 15] directly into the k[0] or via any double variable I get black bubble as expected.

However, if I directly assign the value to the k[0] or test I get a blue bubble as below.

In other words, if I delete the commenting of the k[0] = test line I get black bubble and if I comment that line I get a blue bubble with the same code. This bug, or whatever it is, has become very confusing to me and I have no explanation to it with my limited c++ knowledge. Thanks in advance to anyone who helps to interpret this issue.