kinect深度图转换为 HSI图和灰度图 代码

// 编程中最没用的东西是源代码,最有用的东西是算法和数据结构

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "MMAPFile.h"
#include "CLog.h"
#include "CLogManager.h"

#include "io.h"
#include "direct.h"
#include "math.h"

using namespace std;
using namespace cv;

#define KINECT_DEPTH_WIDTH          512
#define KINECT_DEPTH_HEIGHT         424
#define KINECT_COLOR_WIDTH          1920
#define KINECT_COLOR_HEIGHT         1080
#define KINECT_COLOR_720P_WIDTH     1280
#define KINECT_COLOR_720P_HEIGHT    720
#define KINECT_COLOR_1080P_WIDTH    1920
#define KINECT_COLOR_1080P_HEIGHT   1080
#define KINECT_BUFFER_SZIE          30

LogManager* g_logmanager;
CLog* g_log;

typedef struct
{
    uchar R;
    uchar G;
    uchar B;
}RgbType;

typedef struct
{
    float H;
    float I;
    float S;
}HsiType;

const double PI = 3.1415926535897932;

void log_init()
{
    if (_access("log", 0) == -1)
    {
        _mkdir("log");
    }

    SYSTEMTIME systemTime;
    GetLocalTime(&systemTime);
    char LogFileString[100] = { 0 };
    sprintf_s(LogFileString, "log\\DepthAlg_%02d_%02d_%02d_%02d_%02d.txt",
        systemTime.wMonth,
        systemTime.wDay,
        systemTime.wHour,
        systemTime.wMinute,
        systemTime.wSecond);

    g_logmanager->Clear();
    g_log = g_logmanager->OpenLog(LogFileString);
    g_log->SetLogLevel(CLog::LL_INFORMATION);
    g_log->ClearLogFile();
}

void convertHSItoRGB(HsiType* hsi, RgbType* rgb)
{
    double Doublepi = 2 * 3.1415926535897932;

    float B = 0.0f;
    float R = 0.0f;
    float G = 0.0f;

    if ((hsi->H >= 0.0f) && (hsi->H < Doublepi / 3))
    {
        B = hsi->I*(1.0f - hsi->S);
        R = hsi->I*(1.0f + hsi->S*cos(hsi->H) / cos(Doublepi / 6 - hsi->H));
        G = 3 * hsi->I - (R + B);
    }
    else if ((hsi->H >= Doublepi / 3) && (hsi->H < 2*Doublepi / 3))
    {
        hsi->H = hsi->H - Doublepi / 3;
        R = hsi->I*(1.0f - hsi->S);
        G = hsi->I*(1.0f + hsi->S*cos(hsi->H) / cos(Doublepi / 6 - hsi->H));
        B = 3 * hsi->I - (R + B);
    }
    else if ((hsi->H >= 2 * Doublepi / 3) && (hsi->H < Doublepi))
    {
        hsi->H = hsi->H - 2 * Doublepi / 3;
        G = hsi->I*(1.0f - hsi->S);
        B = hsi->I*(1.0f + hsi->S*cos(hsi->H) / cos(Doublepi / 6 - hsi->H));
        R = 3 * hsi->I - (R + B);
    }

    rgb->B = (uchar)(255.0f * B);
    rgb->R = (uchar)(255.0f * R);
    rgb->G = (uchar)(255.0f * G);

    return;
}

int main()
{
    log_init();

    char str[100] = "meshfile_04_11_16_35_55_086.bin";
    string filename(str);

    CMMAPFile* pCMMAPFile = new CMMAPFile();
    unsigned long size_low = 0;
    unsigned long size_high = 0;

    size_low = pCMMAPFile->MMAP_GetFileSize(filename, &size_high);

    ushort* pDepth = (ushort*)pCMMAPFile->MMAP_CreateFile(filename, CMMAPFILE_OPEN_EXISTING, 0, size_low, size_high);

    Mat DepthImage(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC3);
    Mat GaryImage(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC3);

    uchar* ptr = (uchar*)DepthImage.data;
    uchar* garyptr = (uchar*)GaryImage.data;

    RgbType rgb;
    HsiType hsiColor;
    ushort depth;
    for (int i = 0; i < KINECT_DEPTH_HEIGHT; i++)
    {
        for (int j = 0; j < KINECT_DEPTH_WIDTH; j++)
        {
            //g_log->WriteLog(CLog::LL_INFORMATION, "pDepth[%d %d] %d", i, j, pDepth[i*KINECT_DEPTH_WIDTH + j]);
            depth = pDepth[i*KINECT_DEPTH_WIDTH + j];

            if (depth > 10000)
            {
                depth = 10000;
            }

            if (depth < 1)
            {
                depth = 1;
            }

            hsiColor.H = 2 * PI * (float)(depth-1) / 9999;
            hsiColor.I = 1.0f;
            hsiColor.S = 1.0f;

            convertHSItoRGB(&hsiColor, &rgb);

            ptr[0] = rgb.B;
            ptr[1] = rgb.G;
            ptr[2] = rgb.R;
            ptr += 3;

            garyptr[0] = 255 * (depth - 1) / 9999;
            garyptr[1] = 255 * (depth - 1) / 9999;
            garyptr[2] = 255 * (depth - 1) / 9999;
            garyptr += 3;
        }
    }

    pCMMAPFile->MMAP_Release();

    imshow("pic", DepthImage);
    imshow("pic2", GaryImage);
    waitKey(0);

    return 0;
}

kinect深度图转换为 HSI图和灰度图 代码_第1张图片

你可能感兴趣的:(kinect)