Main
Main

Reputation: 1842

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

I am novice in OpenCV. My program reads image data in 16 bit unsigned int. I need to multiply the image data by some gain of 16 bit unsigned int. So, the resulting data should be kept in 32 bit image file. I tried following, but I get 8 bit all white image. Please help.

    Mat inputData = Mat(Size(width, height), CV_16U, inputdata); 
    inputData.convertTo(input1Data, CV_32F);
    input1Data = input1Data * gain;//gain is ushort

Upvotes: 4

Views: 6845

Answers (1)

marol
marol

Reputation: 4074

As Micka noticed in the comment, first of all we need to scale inputData to have values between 0.0f and 1.0f by passing a 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 the 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);

optimizing first version a bit by not creating temporary data.

Upvotes: 4

Related Questions