Kinect学习(4)——无损深度图存储

之前已经说过,Kinect捕获的原始深度信息(mm为单位),保存成8位的BMP会使精度降低。因此寻求其他保存格式,以完整保留深度信息。

经过查找各种图像格式标准,PNG灰度图可最大支持单通道16位,且PNG是一种无损压缩格式(LZ77压缩);TIFF可支持三通道16位,也是一种无损压缩格式(LZW压缩),因此尝试了使用这两种格式来保存深度图。

原始深度信息是保存在UINT16数组里面的,其实只要把数组内的数据填充到Mat矩阵内,然后显示和保存Mat就可以了。

PNG的Mat数据填充:

/// PNG: 把 16位深度数据 从 UINT16型的数组 转换到 CV_16UC1的Mat,即16位无符号整型单通道Mat中
Mat CDepthBasics::ConvertMat_1(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
    Mat img(nHeight, nWidth, CV_16UC1);
    UINT16* p_mat = (UINT16*)img.data;

    const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);

    while (pBuffer < pBufferEnd)
    {
        USHORT depth = *pBuffer;
        *p_mat = (depth >= nMinDepth) && (depth <= nMaxDepth) ? depth<<3 : 0;
        p_mat++;
        ++pBuffer;
    }
    return img;
}

TIFF的Mat数据填充:

/// TIFF: 把 16位深度数据 从 UINT16型的数组 转换到 CV_16UC3的Mat,即16位无符号整型三通道Mat中
Mat CDepthBasics::ConvertMat_3(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
    Mat img(nHeight, nWidth, CV_16UC3); // rows = nHeight cols = nWidth

    for (int i = 0; i < nHeight; i++)
    {
        UINT16* data = img.ptr(i);
        for (int j = 0; j < nWidth * 3; j++)
        {
            USHORT depth = (*pBuffer >= nMinDepth) && (*pBuffer <= nMaxDepth) ? *pBuffer << 3 : 0;
            data[j++] = depth;
            data[j++] = depth;
            data[j] = depth;
            pBuffer++;
        }
    }

    return img;
}

另外,为了方便存储,用时间作为存储文件名:

/// 按时间产生文件名
string CDepthBasics::GenerateFileName()
{
    string szRet = "";
    char timeBuffer[30];
    time_t nowtime = time(NULL);
    tm timeTemp;
    localtime_s(&timeTemp, &nowtime);
    
    // PNG文件名
/*  
    sprintf_s(timeBuffer, "%04d-%02d-%02d_%02d-%02d-%02d.png", timeTemp.tm_year + 1900, timeTemp.tm_mon + 1,
        timeTemp.tm_mday, timeTemp.tm_hour, timeTemp.tm_min, timeTemp.tm_sec);
*/
    // TIFF文件名
    sprintf_s(timeBuffer, "%04d-%02d-%02d_%02d-%02d-%02d.tif", timeTemp.tm_year + 1900, timeTemp.tm_mon + 1,
        timeTemp.tm_mday, timeTemp.tm_hour, timeTemp.tm_min, timeTemp.tm_sec);
    szRet = timeBuffer;
    return szRet;
}

用imshow和imwrite函数显示和存储Mat就可以了,只是要注意PNG的存储要设定imwrite的压缩参数CV_IMWRITE_PNG_COMPRESSION,范围0-9,数字越大代表压缩尺寸越小压缩时间越长,默认为3,显然我们应该设置成9。

    vectorcompression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);
    string filename = GenerateFileName();
    imwrite(filename, img, compression_params);

从存储大小来看,对于同样512x424的灰度图,BMP为848KB(毕竟是一点都不压缩的),PNG为178KB,TIFF为379KB。TIFF三通道存储了同样的信息,吃亏了啊。。。除此之外,从代码上看也是PNG更简单高效一些,再加上TIFF的详细格式据说很严格很规范。。。

不过读取的时候要注意,imread要定义为

Mat img=imread("xxx.png",CV_LOAD_IMAGE_ANYCOLOR|CV_LOAD_IMAGE_ANYDEPTH);

这样读出来才是原来的16bit,默认是CV_LOAD_IMAGE_COLOR,表示把读取的图片转换为彩色,就会转化成默认的8UC3的。

以上。:)

3/06更新:

这两天把DepthBasics-D2D和ColorBasics-D2D合并成一个程序了,但没有用他的显示和存储程序,而是改成了Opencv的imshow显示。因此原来的保存按钮也消失了,由于opencv没有按钮的功能,就直接用key=WaitKey(1)获取键盘按键的方法来控制。回车键保存,Esc退出。彩色图Mat是CV_8UC3,保存为三通道24位PNG,深度图Mat是CV_16UC1,保存为单通道16位PNG。

再次以上。:)

你可能感兴趣的:(Kinect学习(4)——无损深度图存储)