Converting CV_32FC1 to CV_16UC1

对着背影说爱祢 提交于 2021-01-29 08:32:36

问题


I am trying to convert a float image that I get from a simulated depth camera to CV_16UC1. The camera publishes the depth in CV_32FC1 format. I tried many ways but the result was not reasonable.

cv::Mat depth_cv(512, 512, CV_32FC1, depth);
cv::Mat depth_converted;
depth_cv.convertTo(depth_converted,CV_16UC1);

The result is a black image. If I use a scale factor, the image will be white.

I also tried to do it this way:

float depthValueF [512*512];
for (int i=0;i<resolution[1];i++){ // go through the rows (y)
    for (int j=0;j<resolution[0];j++){ // go through the columns (x)
        depthValueOfPixel=depth[i*resolution[0]+j]; // this is location j/i, i.e. x/y
        depthValueF[i*resolution[0]+j] = (depthValueOfPixel) * (65535.0f);
    }
}

It was not successful either.


回答1:


Try using cv::normalize instead, which will not only convert the image into the proper data type, but it will properly do the scaling for you under the hood.

Therefore:

cv::Mat depth_cv(512, 512, CV_32FC1, depth);
cv::Mat depth_converted;
cv::normalize(depth_cv, depth_converted, 0, 65535, NORM_MINMAX, CV_16UC1);


来源:https://stackoverflow.com/questions/61470255/converting-cv-32fc1-to-cv-16uc1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!