Opencv+Kinect2.0抠图

不知道为什么,这个用c++写的运行起来比c#写的还要慢,用了OpenMP也只是勉强能看。
有空再改改。

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include <Kinect.h>  
#include<Kinect.Face.h>
#include <Kinect.VisualGestureBuilder.h>
#pragma comment ( lib, "kinect20.lib" )  
#pragma comment ( lib, "Kinect20.face.lib" )  
#pragma comment ( lib, "Kinect20.VisualGestureBuilder.lib" )  

using namespace cv;
using namespace std;

template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
    if (pInterfaceToRelease != NULL)
    {
        pInterfaceToRelease->Release();
        pInterfaceToRelease = NULL;
    }
}

int main()
{
    HRESULT hResult;
    setUseOptimized(true);

    IKinectSensor *kinect;
    GetDefaultKinectSensor(&kinect);
    kinect->Open();

    IBodyIndexFrameSource *bodyindexframesource;
    kinect->get_BodyIndexFrameSource(&bodyindexframesource);
    IBodyIndexFrameReader *bodyindexframereader;
    bodyindexframesource->OpenReader(&bodyindexframereader);
    IColorFrameSource*colorframesource;
    kinect->get_ColorFrameSource(&colorframesource);
    IColorFrameReader*colorframereader;
    colorframesource->OpenReader(&colorframereader);
    ICoordinateMapper*coordinate;
    kinect->get_CoordinateMapper(&coordinate);
    IDepthFrameSource*depthframesource;
    kinect->get_DepthFrameSource(&depthframesource);
    IDepthFrameReader*depthframereader;
    depthframesource->OpenReader(&depthframereader);

    IFrameDescription*nfd;
    depthframesource->get_FrameDescription(&nfd);
    int nwidth = 0;
    int nheight = 0;
    nfd->get_Width(&nwidth);
    nfd->get_Height(&nheight);

    IFrameDescription *cfd;
    colorframesource->get_FrameDescription(&cfd);
    int cwidth = 0;
    int cheight = 0;
    cfd->get_Width(&cwidth);
    cfd->get_Height(&cheight);

    vector<DepthSpacePoint>depthspacepoints(cwidth*cheight);
    Mat img(cheight, cwidth, CV_8UC4);

    while (1)
    {
        UINT nBodyIndexBufferSize = 0;
        BYTE *pBodyIndexBuffer = NULL;
        IBodyIndexFrame*bodyindexframe;
        hResult = bodyindexframereader->AcquireLatestFrame(&bodyindexframe);
        if (SUCCEEDED(hResult))
        {
            hResult = bodyindexframe->AccessUnderlyingBuffer(&nBodyIndexBufferSize, &pBodyIndexBuffer);
        }


        if (SUCCEEDED(hResult))
        {
            UINT nDepthBufferSize = 0;
            UINT16 *pDepthBuffer = NULL;
            IDepthFrame*depthframe;
            hResult = depthframereader->AcquireLatestFrame(&depthframe);
            if (SUCCEEDED(hResult))
            {
                hResult = depthframe->AccessUnderlyingBuffer(&nDepthBufferSize, &pDepthBuffer);
            }


            if (SUCCEEDED(hResult))
            {
                UINT nColorBufferSize = 0;
                RGBQUAD *pColorBuffer = NULL;
                IColorFrame*colorframe;
                hResult = colorframereader->AcquireLatestFrame(&colorframe);
                if (SUCCEEDED(hResult))
                {
                    hResult = colorframe->CopyConvertedFrameDataToArray(cheight*cwidth * 4, reinterpret_cast<BYTE*>(img.data), ColorImageFormat::ColorImageFormat_Bgra);
                    if (SUCCEEDED(hResult))
                    {
                        hResult = colorframe->AccessRawUnderlyingBuffer(&nColorBufferSize, reinterpret_cast<BYTE**>(&pColorBuffer));
                    }
                }

                if (SUCCEEDED(hResult))
                {

                    coordinate->MapColorFrameToDepthSpace(nwidth * nheight, (UINT16*)pDepthBuffer, cwidth * cheight, &depthspacepoints[0]);
#pragma omp parallel for
                    for (int i = 0; i < cheight; i++)
                        for (int j = 0; j < cwidth; j++)
                        {
                            int k = i*cwidth + j;
                            DepthSpacePoint p = depthspacepoints[k];

                            int depthX = (int)(p.X + 0.5f);
                            int depthY = (int)(p.Y + 0.5f);

                            if ((depthX >= 0 && depthX < nwidth) && (depthY >= 0 && depthY < nheight))
                            {
                                BYTE player = pBodyIndexBuffer[depthX + (depthY * nwidth)];
                                if (player == 0xff)
                                {
                                    img.at <Vec4b>(i,j) = Vec4b(0, 0, 0, 255);
                                }
                            }
                            else
                                img.at <Vec4b>(i, j) = Vec4b(0, 0, 0, 255);
                        }
                }
                SafeRelease(colorframe);
            }

            SafeRelease(depthframe);
        }

        SafeRelease(bodyindexframe);

        imshow("aaa", img);
        if (waitKey(34) > 0)
        {
            break;
        }
    }
    SafeRelease(bodyindexframesource);
    SafeRelease(bodyindexframereader);
    SafeRelease(nfd);
    if (kinect) 
    {
        kinect->Close();
    }
    SafeRelease(kinect);

    return 0;
}

运行图:
这里写图片描述

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值