//#include "duiqi.hpp"

#include "kinect.h"

#include <iostream>

#include "opencv2/opencv.hpp"

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <fstream>

using namespace cv;

using namespace std;

Mat depthFilter(UINT16 *depthData);

template<class Interface>

inline void SafeRelease(Interface *& pInterfaceToRelease)

{

if (pInterfaceToRelease != NULL)

{

pInterfaceToRelease->Release();

pInterfaceToRelease = NULL;

}

}

UINT16 uDepthMin = 0, uDepthMax = 0;

int main()

{

IKinectSensor* m_pKinectSensor;

HRESULT hr;

ICoordinateMapper*      m_pCoordinateMapper = NULL;

CameraIntrinsics* m_pCameraIntrinsics = new CameraIntrinsics();

hr = GetDefaultKinectSensor(&m_pKinectSensor);

if (FAILED(hr))

{

return hr;

}

IMultiSourceFrameReader* m_pMultiFrameReader = NULL;

if (m_pKinectSensor)

{

hr = m_pKinectSensor->Open();

if (SUCCEEDED(hr))

{

hr = m_pKinectSensor->OpenMultiSourceFrameReader(

FrameSourceTypes::FrameSourceTypes_Color |

FrameSourceTypes::FrameSourceTypes_Infrared |

FrameSourceTypes::FrameSourceTypes_Depth,

&m_pMultiFrameReader);

}

}

if (SUCCEEDED(hr))

{

hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);

}

if (!m_pKinectSensor || FAILED(hr))

{

return E_FAIL;

}

IMultiSourceFrame* m_pMultiFrame = nullptr;

IDepthFrameReference* m_pDepthFrameReference = NULL;

IColorFrameReference* m_pColorFrameReference = NULL;

IInfraredFrameReference* m_pInfraredFrameReference = NULL;

IInfraredFrame* m_pInfraredFrame = NULL;

IDepthFrame* m_pDepthFrame = NULL;

IColorFrame* m_pColorFrame = NULL;

Mat rgb(1080, 1920, CV_8UC4);

Mat rgb_resize(540, 960, CV_8UC4);

DepthSpacePoint*        m_pDepthCoordinates = NULL;

ColorSpacePoint*        m_pColorCoordinates = NULL;

CameraSpacePoint*       m_pCameraCoordinates = NULL;

m_pColorCoordinates = new ColorSpacePoint[512 * 424];

m_pCameraCoordinates = new CameraSpacePoint[512 * 424];

UINT16 *depthData = new UINT16[424 * 512];

Mat depth(424, 512, CV_16UC1);

Mat depth8U(424, 512, CV_8U);

vector<DepthSpacePoint> depthSpacePoints(1920 * 1080);

Mat CoordinateMapperMat(1080, 1520, CV_8U);

Mat CoordinateMapperMat_resize(540, 960, CV_8U);

int savecount = 0;

while (true)

{

hr = m_pMultiFrameReader->AcquireLatestFrame(&m_pMultiFrame);

if (FAILED(hr) || !m_pMultiFrame)

{

continue;

}

if (SUCCEEDED(hr))

hr = m_pMultiFrame->get_ColorFrameReference(&m_pColorFrameReference);

if (SUCCEEDED(hr))

hr = m_pColorFrameReference->AcquireFrame(&m_pColorFrame);

if (SUCCEEDED(hr))

hr = m_pMultiFrame->get_DepthFrameReference(&m_pDepthFrameReference);

if (SUCCEEDED(hr))

hr = m_pDepthFrameReference->AcquireFrame(&m_pDepthFrame);

/*m_pDepthFrame->get_DepthMinReliableDistance(&uDepthMin);

m_pDepthFrame->get_DepthMaxReliableDistance(&uDepthMax);

cout << "Reliable Distance: " << uDepthMin << " - " << uDepthMax << endl;*/

savecount++;

cout << savecount << endl;

ostringstream savecountstr;

savecountstr << savecount;

UINT nColorBufferSize = 1920 * 1080 * 4;

if (SUCCEEDED(hr))

{

hr = m_pColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast<BYTE*>(rgb.data), ColorImageFormat::ColorImageFormat_Bgra);

Rect rect(200, 0, 1520, 1080);

Mat rgb_roi = rgb(rect);

resize(rgb_roi, rgb_resize, Size(), 0.4, 0.4);

imshow("color_resize", rgb_resize);

imwrite("D:/file/hust/ARcodes/ARKinect∂‘∆Î181107/ARKinect/save6/color/" + savecountstr.str() + ".png", rgb_resize);

}

UINT nDepthBufferSize = 424 * 512;

if (SUCCEEDED(hr))

{

hr = m_pDepthFrame->CopyFrameDataToArray(nDepthBufferSize, reinterpret_cast<UINT16*>(depthData));

m_pDepthFrame->CopyFrameDataToArray(nDepthBufferSize, reinterpret_cast<UINT16*>(depth.data));

//16位转成了8位显示

depth.convertTo(depth8U, CV_8U, 255.0f / 4500.0f);

imshow("depth", depth8U);

}

//16位数据

Mat filterDepth = depthFilter(depthData);

Mat filterDepth8U;

filterDepth.convertTo(filterDepth8U, CV_8U, 255.0f / 4500.0f);

if (SUCCEEDED(hr))

{

hr = m_pCoordinateMapper->MapColorFrameToDepthSpace(424 * 512, reinterpret_cast<UINT16*>(filterDepth.data), 1920 * 1080, &depthSpacePoints[0]);

}

if (SUCCEEDED(hr))

{

CoordinateMapperMat = Scalar(0, 0, 0, 0); // ∂®“ÂŒ™Mat(colorHeight, colorWidth, CV_8UC4)

for (int y = 0; y < 1080; y++)

{

for (int x = 200; x < 1720; x++)

//for (int x = 0; x < 1920; x++)

{

unsigned int index = y * 1920 + x;

DepthSpacePoint p = depthSpacePoints[index];

if (p.X != -std::numeric_limits<float>::infinity() && p.Y != -std::numeric_limits<float>::infinity())

{

int depthX = static_cast<int>(p.X + 0.2f);

int depthY = static_cast<int>(p.Y + 0.2f);

if ((depthX >= 0) && (depthX < 512) && (depthY >= 0) && (depthY < 424))

{

CoordinateMapperMat.at<uchar>(y, x - 200) = filterDepth8U.at<uchar>(depthY, depthX);

}

}

}

}

resize(CoordinateMapperMat, CoordinateMapperMat_resize, Size(), 0.4, 0.4);

imshow("CoordinateMapper", CoordinateMapperMat_resize);

imwrite("D:/file/hust/ARcodes/ARKinect∂‘∆Î181107/ARKinect/save6/result/" + savecountstr.str() + ".png", CoordinateMapperMat_resize);

}

int c = waitKey(1);

if ((char)c == VK_ESCAPE)

break;

SafeRelease(m_pColorFrame);

SafeRelease(m_pDepthFrame);

SafeRelease(m_pColorFrameReference);

SafeRelease(m_pDepthFrameReference);

SafeRelease(m_pMultiFrame);

}

cv::destroyAllWindows();

SafeRelease(m_pCoordinateMapper);

m_pKinectSensor->Close();

std::system("pause");

return 0;

}

Mat depthFilter(UINT16 *depthData) {

Mat i_before(424, 512, CV_8UC4);

Mat i_after(424, 512, CV_8UC4);

Mat i_result(424, 512, CV_16UC1);

cv::Mat i_result8U;

unsigned short maxDepth = 0;

unsigned short iZeroCountBefore = 0;

unsigned short iZeroCountAfter = 0;

unsigned short* depthArray = (unsigned short*)depthData;

for (int i = 0; i < 512 * 424; i++)

{

int row = i / 512;

int col = i % 512;

unsigned short depthValue = depthArray[row * 512 + col];

if (depthValue == 0)

{

i_before.data[i * 4] = 255;

i_before.data[i * 4 + 1] = 0;

i_before.data[i * 4 + 2] = 0;

i_before.data[i * 4 + 3] = depthValue / 256;

iZeroCountBefore++;

}

else

{

i_before.data[i * 4] = depthValue / 4500.0f * 256;

i_before.data[i * 4 + 1] = depthValue / 4500.0f * 256;

i_before.data[i * 4 + 2] = depthValue / 4500.0f * 256;

i_before.data[i * 4 + 3] = depthValue / 4500.0f * 256;

}

maxDepth = depthValue > maxDepth ? depthValue : maxDepth;

}

//cout << "max depth value: " << maxDepth << endl;

unsigned short* smoothDepthArray = (unsigned short*)i_result.data;

int widthBound = 512 - 1;

int heightBound = 424 - 1;

int innerBandThreshold = 1;

int outerBandThreshold = 3;

for (int depthArrayRowIndex = 0; depthArrayRowIndex<424; depthArrayRowIndex++)

{

for (int depthArrayColumnIndex = 0; depthArrayColumnIndex < 512; depthArrayColumnIndex++)

{

int depthIndex = depthArrayColumnIndex + (depthArrayRowIndex * 512);

if (depthArray[depthIndex] == 0)

{

int x = depthIndex % 512;

int y = (depthIndex - x) / 512;

unsigned short filterCollection[24][2] = { 0 };

int innerBandCount = 0;

int outerBandCount = 0;

for (int yi = -2; yi < 3; yi++)

{

for (int xi = -2; xi < 3; xi++)

{

if (xi != 0 || yi != 0)

{

int xSearch = x + xi;

int ySearch = y + yi;

if (xSearch >= 0 && xSearch <= widthBound &&

ySearch >= 0 && ySearch <= heightBound)

{

int index = xSearch + (ySearch * 512);

if (depthArray[index] != 0)

{

for (int i = 0; i < 24; i++)

{

if (filterCollection[i][0] == depthArray[index])

{

filterCollection[i][1]++;

break;

}

else if (filterCollection[i][0] == 0)

{

filterCollection[i][0] = depthArray[index];

filterCollection[i][1]++;

break;

}

}

if (yi != 2 && yi != -2 && xi != 2 && xi != -2)

innerBandCount++;

else

outerBandCount++;

}

}

}

}

}

if (innerBandCount >= innerBandThreshold || outerBandCount >= outerBandThreshold)

{

short frequency = 0;

short depth = 0;

for (int i = 0; i < 24; i++)

{

if (filterCollection[i][0] == 0)

break;

if (filterCollection[i][1] > frequency)

{

depth = filterCollection[i][0];

frequency = filterCollection[i][1];

}

}

smoothDepthArray[depthIndex] = depth;

}

else

{

smoothDepthArray[depthIndex] = 0;

}

}

else

{

smoothDepthArray[depthIndex] = depthArray[depthIndex];

}

}

}

for (int i = 0; i < 512 * 424; i++)

{

int row = i / 512;

int col = i % 512;

unsigned short depthValue = smoothDepthArray[row * 512 + col];

if (depthValue == 0)

{

i_after.data[i * 4] = 255;

i_after.data[i * 4 + 1] = 0;

i_after.data[i * 4 + 2] = 0;

i_after.data[i * 4 + 3] = depthValue / 256;

iZeroCountAfter++;

}

else

{

i_after.data[i * 4] = depthValue / 4500.0f * 256;

i_after.data[i * 4 + 1] = depthValue / 4500.0f * 256;

i_after.data[i * 4 + 2] = depthValue / 4500.0f * 256;

i_after.data[i * 4 + 3] = depthValue / 4500.0f * 256;

}

}

i_result.convertTo(i_result8U, CV_8U, 255.0f / 4500.0f);   // uDepthMax

return i_result;

}

kinect 深度图与彩色图对齐程序的更多相关文章

  1. OpenNI1.5获取华硕XtionProLive深度图和彩色图并用OpenCV显示

    华硕XtionPro类似Kinect,都是体感摄像机,可捕捉深度图和彩色图. 具体參数见:http://www.asus.com.cn/Multimedia/Xtion_PRO_LIVE/specif ...

  2. OpenNI2获取华硕XtionProLive深度图和彩色图并用OpenCV显示

    使用OpenNI2打开XtionProLive时有个问题,彩色图分辨率不管怎样设置始终是320*240,深度图倒是能够设成640*480,而OpenNI1.x是能够获取640*480的彩色图的. 彩色 ...

  3. Android-将RGB彩色图转换为灰度图

    package com.example.yanlei.wifi; import android.graphics.Bitmap; import android.graphics.BitmapFacto ...

  4. 基于.Net core3.0 开发的斗图小程序后端+斗图小程序

    为啥要写这么一个小程序? 作为互联网的原住民. 90后程序员的我,从高中开始发QQ小表情. 到之后的熊猫头,蘑菇头. 可以说表情包陪伴我从学校到社会,从青少年到中年.. 而且因为斗图厉害,还找到一个女 ...

  5. QT 实现彩色图亮度均衡,RGB和HSI空间互相转换

    从昨天折腾到今天.再折腾下去我都要上主楼了  大致和灰度图均衡是一样的,主要是不能像平滑什么的直接对R,G,B三个分量进行.这样出来的图像时没法看的.因此我们要对亮度进行均衡.而HSI彩色空间中的分量 ...

  6. perf + Flame Graph火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

  7. [转]perf + 火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

  8. perf + 火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

  9. 用Html5制作的一款数学教学程序Function Graphics(绘制函数图的程序)

    最近我不仅对游戏开发感兴趣,还对函数图感兴趣,特此我开发了这个程序.以下是一些介绍和下载演示地址,喜欢的朋友可以看看: 一,产品名片 产品名:Function Graphics 版本: 0.1 开发者 ...

随机推荐

  1. WinForm中使用BackgroundWorker异步加载数据并使用进度条

    在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...

  2. DevExpress中GridControl的使用笔记(转)

    转自:https://www.jianshu.com/p/badc1d2f0841 注:练习例子为: DxApplication1 -> XtraForm1 , 例子上传到本博中 2019.4. ...

  3. .NET 使用 Azure Blob 存储图片或文件

    使用的是VS2017 一.先使用 NuGet 获取这两个包. 执行以下步骤: 在“解决方案资源管理器”中,右键单击你的项目并选择“管理 NuGet 包”. 1.在线搜索“WindowsAzure.St ...

  4. Hanlp自然语言处理工具之词法分析器

    本章是接前两篇<分词工具Hanlp基于感知机的中文分词框架>和<基于结构化感知机的词性标注与命名实体识别框架>的.本系统将同时进行中文分词.词性标注与命名实体识别3个任务的子系 ...

  5. Share Today

    当问[一生中最大的错误是什么?]时,佛陀回答: 最大的错误就是你以为你还有时间 时间是免费的也是无价的 你无法拥有 但可以花费 而一旦失去 就无法挽回 一般人一生有78年 我们有28.3年在睡觉 几乎 ...

  6. System.DllNotFoundException: Unable to load DLL 'libgdiplus': The specified module could not be found.

    netcore 使用System.Drawing 出现如下错误: Unhandled Exception: System.TypeInitializationException: The type i ...

  7. MySQL 数据库索引

    数据库索引 在数据库中.索引使数据库程序无须对整个表进行全表扫描就可以在其中找到所需的数据: 数据库中的索引是某个表中一列或者若干列值的集合.以及物理标识这些值的数据页的逻辑指针清单: MySQL 索 ...

  8. 解除Portal for ArcGIS与ArcGIS Server的联合

    将ArcGIS Server站点添加到Portal中,可以实现ArcGIS Server站点的单点登录特性,并可以与Portal共享Server站点发布的内容,同时通过将联合服务器注册为托管服务器后还 ...

  9. 给查询出的SQL记录添加序号列,解决方法有以下两种

    第一: select ROW_NUMBER() OVER (ORDER BY a.字段 ASC) AS XUHAO,a.* from table a (table 为表名,字段为表a中的字段名) 第二 ...

  10. samba、ftp和ssh服务

    samba服务 Smb主要作为网络通信协议; Smb是基于cs架构: 完成Linux与windows之间的共享:linux与linux之间共享用NFS 第一步:安装samba [root@ken ~] ...