如何在OpenCV中将16位图像转换为32位图像?

时间:2014-10-27 16:11:09

标签: c++ opencv

我是OpenCV的新手。我的程序以16位无符号整数读取图像数据。我需要将图像数据乘以16位无符号整数的增益。因此,结果数据应保存在32位图像文件中。 我试过跟随,但我得到8位全白图像。请帮忙。

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

1 个答案:

答案 0 :(得分:4)

正如Micka在评论中注意到的,首先我们需要通过传递比例因子来缩放inputData,使其值介于0.0f和1.0f之间:

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

现在,与乘法相同:

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!

我认为这也应该编译:

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

通过不创建临时数据来优化第一版本。

相关问题