How to convert a 16-bit image to a 32-bit image in OpenCV?

I am new to OpenCV. My program reads image data in a 16-bit unsigned int. I need to multiply image data by some 16 bit unsigned gain int. Thus, the received data should be stored in a 32-bit image file. I tried to follow, but I get 8 bits of the whole white image. Please, help.

    Mat inputData = Mat(Size(width, height), CV_16U, inputdata); 
    inputData.convertTo(input1Data, CV_32F);
    input1Data = input1Data * gain;//gain is ushort
+4
source share
1 answer

As Mickey noted in a comment, first of all we need to scale inputData to have values ​​between 0.0f and 1.0f, passing the scaling factor:

inputData.convertTo(input1Data, CV_32F, 1.0/65535.0f); // since in inputData 
                                                       // we have values between 0 and 
                                                       // 65535 so all resulted values 
                                                       // will be between 0.0f and 1.0f

And now, the same with multiplication:

input1Data = input1Data * gain * (1.0f / 65535.0f); // gain, of course, will be
                                                    // automatically cast to float
                                                    // therefore the resulted factor 
                                                    // will have value from 0 to 1, 
                                                    // so input1Data too!

And I think this should compile too:

input1Data *= gain * (1.0f / 65535.0f);

, .

+4

All Articles