使用SDK 1.8的KinectV1测试c ++

时间:2018-09-27 12:47:34

标签: c++ opencv sdk kinect

大家好,我是一名初学者。我从朋友那里借了这段代码。运行程序后。我在深度图像上有问题。在我看来,深度图像应该显示从浅到深(颜色渐变)。但是,此结果显示出从明到暗,从明到暗,就像重复模式一样。 您是否介意解决此代码并提出建议,以帮助我自己理解编程算法?

非常感谢您。

主要

#include "stdafx.h"
#include "KinectV1Capture.h"

int main()
{
KinectV1Capture kinect;

if (kinect.Init() != S_OK) {
    system("pause");
    return 0;
}

bool quit = false;
std::vector<int> quality;
//std::vector<std::vector<cv::Point>> contour_point;
int count = 0;
//::Mat colorimg;
cv::Mat depthimg;
cv::namedWindow("DepthImage", CV_WINDOW_AUTOSIZE);

//colorimg.create(kinect.GetColorImgSize(), kinect.GetColorImgType());
//depthimg.create(kinect.GetDepthImgSize(), kinect.GetDepthImgType());

while (1) {
    kinect.UpdateFrame();

    kinect.GetDepthImg().copyTo(depthimg);
    //kinect.GetDepth3HImg().copyTo(depthimg);
    //cv::threshold(depthimg, binary, 100.0, 255.0, cv::THRESH_BINARY); 

    /*cv::findContours()*/

    cv::imshow("DepthImage", depthimg);
    //cv::imshow("BinaryImage", binary);

    int keyInput = cv::waitKey(10);
    if (keyInput != -1) {
        switch (keyInput) {
        case 'q':
            quit = true;
            break;
        case 's':
            quality.push_back(CV_IMWRITE_PNG_COMPRESSION);
            quality.push_back(0);
            cv::imwrite("Depth_" + std::to_string(count) + ".png", depthimg, quality);
            count++;
            //cv::imwrite("Binary.png", binary, quality);
            break;
        default:
            break;
        }
    }

    if (quit) {
        break;
    }
}

kinect.Close();

return 0;
}

KinectV1Capture.h

#pragma once

#include <Windows.h>
#include <iostream>

#include <NuiApi.h>

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>

#define color_height 480
#define color_width 640

#define depth_height 480
#define depth_width 640

class KinectV1Capture {
private:
cv::Mat colorImg, depthImg, depthImg3H;

HANDLE colorEvent, depthEvent, colorStream, depthStream;

void UpdateColorFrame();
void UpdateDepthFrame();
void DepthImg1ChTo3CH();

int depth_max = 31800;
int depth_min = 6408;

public:
KinectV1Capture();
~KinectV1Capture();

bool Init();
void UpdateFrame();
void Close();

cv::Mat GetColorImg();
cv::Mat GetDepthImg();
cv::Mat GetDepth3HImg();

int GetColorImgType();
int GetDepthImgType();

cv::Size GetColorImgSize();
cv::Size GetDepthImgSize();

void SetMaxDepth(int);
void SetMinDepth(int);
};

KinectV1Capture.cpp

#include "KinectV1Capture.h"


KinectV1Capture::KinectV1Capture() {
colorImg.create(color_height, color_width, CV_8UC3);
depthImg.create(depth_height, depth_width, CV_8UC1);
depthImg3H.create(depth_height, depth_width, CV_8UC3);

colorEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
depthEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

colorStream = NULL;
depthStream = NULL;
}

KinectV1Capture::~KinectV1Capture() {
colorImg.release();
depthImg.release();
depthImg.release();

NuiShutdown();
}

bool KinectV1Capture::Init() {
HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR |    NUI_INITIALIZE_FLAG_USES_DEPTH);

if (hr != S_OK) {
    std::cout << "NuiInit failed" << std::endl;
    return hr;
}

hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, NULL, 4, colorEvent, &colorStream);
if (hr != S_OK) {
    std::cout << "Open Color Stream failed" << std::endl;
    NuiShutdown();
    return hr;
}

hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, NULL, 2, depthEvent, &depthStream);
if (hr != S_OK) {
    std::cout << "Open depth Stream failed" << std::endl;
    NuiShutdown();
    return hr;
    }
}

void KinectV1Capture::UpdateColorFrame() {
const NUI_IMAGE_FRAME *colorFrame = NULL;

NuiImageStreamGetNextFrame(colorStream, 0, &colorFrame);
INuiFrameTexture *pTexture = colorFrame->pFrameTexture;

NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);

if (LockedRect.Pitch != 0) {
    for (int i = 0; i < colorImg.rows; i++) {
        uchar *ptr = colorImg.ptr<uchar>(i);

        uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
        for (int j = 0; j < colorImg.cols; j++) {
            ptr[3 * j] = pBuffer[4 * j];
            ptr[3 * j + 1] = pBuffer[4 * j + 1];
            ptr[3 * j + 2] = pBuffer[4 * j + 2];
        }
    }
}
else {
    std::cout << "Get Color Image failed" << std::endl;
}

//Filps Img
cv::Mat nColorImg;
colorImg.copyTo(nColorImg);
cv::flip(nColorImg, colorImg, 1);
nColorImg.release();

pTexture->UnlockRect(0);
NuiImageStreamReleaseFrame(colorStream, colorFrame);
}

void KinectV1Capture::UpdateDepthFrame() {
const NUI_IMAGE_FRAME *depthFrame = NULL;

NuiImageStreamGetNextFrame(depthStream, 0, &depthFrame);
INuiFrameTexture *pTexture = depthFrame->pFrameTexture;

NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);

if (LockedRect.Pitch != 0) {

    for (int i = 0; i < depthImg.rows; i++) {
        uchar *ptr = depthImg.data;
        uchar *pBuffer = (uchar*)(LockedRect.pBits) + i * LockedRect.Pitch;
        USHORT *pBufferRun = (USHORT*)pBuffer;

        for (int j = 0; j < depthImg.cols; j++) {
            if (pBufferRun[j] < depth_min && pBufferRun[j] != 0)
                ptr[(i*depthImg.cols) + j] = 0;//6408
            else if (pBufferRun[j] > depth_max) {
                ptr[(i*depthImg.cols) + j] = 0;//31800
            }
            else {
                ptr[(i*depthImg.cols) + j] = 256 * pBufferRun[j] / 4095;
            }
        };
    }
}
else {
    std::cout << "Get Depth Image failed" << std::endl;
}

cv::flip(depthImg, depthImg, 1);

DepthImg1ChTo3CH();

pTexture->UnlockRect(0);
NuiImageStreamReleaseFrame(depthStream, depthFrame);
}

void KinectV1Capture::DepthImg1ChTo3CH() {
///cv::Mat depth8bit;
cv::Mat depht3HMask = cv::Mat(depthImg.rows, depthImg.cols, CV_8UC3, cv::Scalar(0, 255, 255));
//depthImg.convertTo(depth8bit, CV_8U, 255.0 / 10000);
cv::cvtColor(depthImg, depthImg3H, CV_GRAY2BGR);
cv::bitwise_and(depthImg3H, depht3HMask, depthImg3H);
}

void KinectV1Capture::UpdateFrame() {
if (WaitForSingleObject(colorEvent, 0) == 0) {
    UpdateColorFrame();
}

if (WaitForSingleObject(depthEvent, 0) == 0) {
    UpdateDepthFrame();
    }
}

cv::Mat KinectV1Capture::GetColorImg() {
return colorImg;
}

cv::Mat KinectV1Capture::GetDepthImg() {
return depthImg;
}

cv::Mat KinectV1Capture::GetDepth3HImg() {
return depthImg3H;
}

cv::Size KinectV1Capture::GetColorImgSize() {
return colorImg.size();
}

cv::Size KinectV1Capture::GetDepthImgSize() {
return depthImg.size();
}

int KinectV1Capture::GetColorImgType() {
return colorImg.type();
}

int KinectV1Capture::GetDepthImgType() {
return depthImg.type();
}

void KinectV1Capture::SetMinDepth(int min) {
depth_min = 25392 * min / 1000 + 6408;

}

void KinectV1Capture::SetMaxDepth(int max) {
depth_max = 25392 * max / 1000 + 6408;
}

void KinectV1Capture::Close() {
colorImg.release();
depthImg.release();
depthImg3H.release();

NuiShutdown();
}

0 个答案:

没有答案