市场上各种深度摄像头测试之Kinect V1(第一代)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzw_ice/article/details/77164684

系统要求、系统配置、开发环境

Windows XP 及以后,X86(32/64bit)
2 GB RAM
USB 2.0 高速端口

sdk下载安装及运行官方demo


深度图像获取

windows+vs2013+opencv 保存16位深度图像,格式为png
1、opencv安装+vs2013配置
直接官网下载:http://opencv.org/
安装和配置:http://blog.csdn.net/wzw_ice/article/details/77159741
2、vs项目配置Kinect V1
(1)右键项目,进入项目属性页,选择Debug配置
(2)VC++目录:
包含目录中 添加官方驱动的include文件夹,如:
C:\Program Files\Microsoft SDKs\Kinect\v1.8\inc
库目录中 添加依赖库,如:
C:\Program Files\Microsoft SDKs\Kinect\v1.8\lib\x86
(3)链接器->输入->附加依赖项,添加
kinect10.lib
3 个人项目代码(从官方demo提取出来并通过opencv获取深度图像保存为16bit的png)
opencvKinectDep.cpp


/*
原始深度图像获取
kinect sdk 1.0
*/
#include "opencvKinectDepOrigin.h"
#define DEPTH_WIDTH     320
#define DEPTH_HIGHT     240
#define CHANNEL         3
BYTE buf[DEPTH_WIDTH*DEPTH_HIGHT*CHANNEL];

HANDLE hDepth1;
HANDLE hDepth2;

DWORD WINAPI DepthFunc(LPVOID pParam)
{

    //cout << "depth start!" << endl;
    while (TRUE)
    {
        if (WaitForSingleObject(hDepth1, INFINITE) == WAIT_OBJECT_0)
        {
            drawDepth(hDepth2);

        }
        //  Sleep(10);
        //cout << "depth" << endl;
    }
}
//深度图转伪彩色图
void depth2color(cv::Mat & color, const cv::Mat & depth, const double max, const double min)
{
    cv::Mat grayImage;
    double alpha = 255.0 / (max - min);
    depth.convertTo(grayImage, CV_8UC1, alpha, -alpha * min);// expand your range to 0..255. Similar to histEq();
    cv::applyColorMap(grayImage, color, cv::COLORMAP_JET);// this is great. It converts your grayscale image into a tone-mapped one, much more pleasing for the eye function is found in contrib module, so include contrib.hpp  and link accordingly
}

int drawDepth(HANDLE h)
{

    const NUI_IMAGE_FRAME * pImageFrame = NULL;
    HRESULT hr = NuiImageStreamGetNextFrame(h, 0, &pImageFrame);
    if (FAILED(hr))
    {
        cout << "Get Depth Image Frame Failed" << endl;
        return -1;
    }
    INuiFrameTexture * pTexture = pImageFrame->pFrameTexture;
    NUI_LOCKED_RECT LockedRect;
    pTexture->LockRect(0, &LockedRect, NULL, 0);
    if (LockedRect.Pitch != 0)
    {
        USHORT * pBuff = (USHORT*)LockedRect.pBits;

        //cout << LockedRect.pBits << endl;
        //cout << pBuff << endl;
        Mat depthTmp(DEPTH_HIGHT, DEPTH_WIDTH, CV_16U, pBuff);
        imshow("DepthOrigin", depthTmp);
        imwrite("./image/16depth/1.png", depthTmp);

        //深度->伪彩色
        Mat sc_color;
        double imax = 0, imin = 70000;
        int imrow = depthTmp.rows;
        int imcol = depthTmp.cols * depthTmp.channels();
        for (int i = 0; i < imrow; i++)
        {
            for (int j = 0; j < imcol; j++)
            {
                ushort data = depthTmp.at<ushort>(i, j);
                if (imin >= data && data != 0)
                {
                    imin = data;
                }
                if (imax <= data)
                {
                    imax = data;
                }

            }
        }
        depth2color(sc_color, depthTmp,imax,imin);
        imshow("伪彩色图",sc_color);
        //imwrite("./1.png",sc_color);



        for (int i = 0; i<DEPTH_WIDTH*DEPTH_HIGHT; i++)
        {
            BYTE index = pBuff[i] & 0x07;
            USHORT realDepth = (pBuff[i] & 0xFFF8) >> 3;

            BYTE scale = 255 - (BYTE)(256 * realDepth / 0x0fff);
            buf[CHANNEL*i] = buf[CHANNEL*i + 1] = buf[CHANNEL*i + 2] = 0;
            switch (index)
            {
            case 0:
                buf[CHANNEL*i] = scale / 2;
                buf[CHANNEL*i + 1] = scale / 2;
                buf[CHANNEL*i + 2] = scale / 2;
                break;
            case 1:
                buf[CHANNEL*i] = scale;
                break;
            case 2:
                buf[CHANNEL*i + 1] = scale;
                break;
            case 3:
                buf[CHANNEL*i + 2] = scale;
                break;
            case 4:
                buf[CHANNEL*i] = scale;
                buf[CHANNEL*i + 1] = scale;
                break;
            case 5:
                buf[CHANNEL*i] = scale;
                buf[CHANNEL*i + 2] = scale;
                break;
            case 6:
                buf[CHANNEL*i + 1] = scale;
                buf[CHANNEL*i + 2] = scale;
                break;
            case 7:
                buf[CHANNEL*i] = 255 - scale / 2;
                buf[CHANNEL*i + 1] = 255 - scale / 2;
                buf[CHANNEL*i + 2] = 255 - scale / 2;
                break;
            }
        }
        Mat b(DEPTH_HIGHT, DEPTH_WIDTH, CV_8UC3, buf);
        imshow("depth", b);
        waitKey(1);
    }
    NuiImageStreamReleaseFrame(h, pImageFrame);
    return 0;
}
int getOpencvKinectDepOrigin()
{

    //初始化NUI
    HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH);
    if (hr != S_OK)
    {
        cout << "NuiInitialize failed" << endl;
        return hr;
    }


    hDepth1 = CreateEvent(NULL, TRUE, FALSE, NULL);
    hDepth2 = NULL;
    hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_320x240, 0, 2, hDepth1, &hDepth2);
    if (FAILED(hr))
    {
        cout << "Could not open depth stream video" << endl;
        return hr;
    }

    HANDLE hThread;
    hThread = CreateThread(NULL, 0, DepthFunc, hDepth2, 0, NULL);
    CloseHandle(hThread);
    //Sleep(50000);
    if (cvWaitKey(20) == 27)
    {
        NuiShutdown();
    }
    return 0;
}

深度图转换成伪彩色图

函数:http://blog.csdn.net/wzw_ice/article/details/77163703
相关代码也可在上面的cpp文件代码中找到

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页