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) = 255 * ro[j*39 + i];
imtest.at<cv::Vec4b>(i,j) = 0;
imtest.at<cv::Vec4b>(i,j) = 0;
imtest.at<cv::Vec4b>(i,j) = 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 = 1.08455576996775482;
double test = ro[39*15 + 15];
//k = test;
for (int j = 0; j < 39; j++)
{
for (int i = 0; i < 39; i++)
{
imtest2.at<cv::Vec4b>(i,j) = 255 * k;//ro[15*39 + 15];//ro[j*39 + i];
imtest2.at<cv::Vec4b>(i,j) = 0;//255 * 1.2;
imtest2.at<cv::Vec4b>(i,j) = 0;//255 * 1.2;
imtest2.at<cv::Vec4b>(i,j) = 150 * ro[j*39 + i];
}

}
std::cout.precision(18);
std::cout << ro[15*39 + 15] << std::endl;
std::cout << k << 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 or via any double variable I get black bubble as expected. However, if I directly assign the value to the k or test I get a blue bubble as below. In other words, if I delete the commenting of the k = 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.

edit retag close merge delete

it's unclear, what you try to achieve here, but you probably should not write for loops like that at all, it's a clear anti-pattern using opencv

I have an I'd array with 39x39 values. Sort of "vectorized" array containing the values of 39x39 mesh. And each value on that mesh has varying values from 0 - 1.1 or so. Thus, if I multiply these values to a certain intensity. I should get the picture visually depicting the array with colored places having values close to 1. And I get I these type of pictures but with black color instead of a blue one which the code I've written should have given. The problem here is if I explicitly take one value from that black colored section, let's say (15,15) and I explicitly assign it to a variable as below.

k = 1.08455576996775482;

I get a blue picture. But if I do the following

double test = ro[39*15 + 15];
k = test;

I get a black picture again.

The last 4 print statements are giving exactly the same value on the console.