深度颜色3
来源:互联网 发布:蓝牙怎么传软件 编辑:程序博客网 时间:2024/06/06 09:03
#include <opencv2\opencv.hpp> #include<iostream>#include <Windows.h>#include "NuiApi.h"#include <d3d11.h>using namespace std;using namespace cv; static float Depth_value;static float Show_value;const int MAX_DISTANCE = 4000;const int MIN_DISTANCE = 400;const LONG m_depthWidth = 640;const LONG m_depthHeight = 480;const LONG m_colorWidth = 640;const LONG m_colorHeight = 480;const LONG cBytesPerPixel = 4;int main(){ Mat image_rgb; Mat image_depth; image_rgb.create(480,640,CV_8UC3); image_depth.create(480,640,CV_8UC1); //一个KINECT实例指针 INuiSensor* m_pNuiSensor = NULL; if (m_pNuiSensor != NULL) { return 0; } int iSensorCount; HRESULT hr = NuiGetSensorCount(&iSensorCount); hr = NuiCreateSensorByIndex(iSensorCount - 1, &m_pNuiSensor); //初始化,让其可以接收彩色和深度数据流 hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); if (FAILED(hr)) { cout<<"NuiInitialize failed"<<endl; return hr; } //彩色图像获取下一帧事件 HANDLE nextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //彩色图像事件句柄 HANDLE colorStreamHandle = NULL; //深度图像获取下一帧事件 HANDLE nextDepthFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //深度图像事件句柄 HANDLE depthStreamHandle = NULL; //实例打开数据流,这里NUI_IMAGE_TYPE_COLOR表示彩色图像 hr = m_pNuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, 0,2,nextColorFrameEvent,&colorStreamHandle); if( FAILED( hr ) )//判断是否提取正确 { cout<<"Could not open color image stream video"<<endl; m_pNuiSensor->NuiShutdown(); return hr; } //实例打开数据流,这里NUI_IMAGE_TYPE_DEPTH表示深度图像 hr = m_pNuiSensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480, 0,2,nextDepthFrameEvent, &depthStreamHandle); if( FAILED( hr ) )//判断是否提取正确 { cout<<"Could not open color image stream video"<<endl; m_pNuiSensor->NuiShutdown(); return hr; } //cv::namedWindow("depth", CV_WINDOW_AUTOSIZE); // moveWindow("depth",300,600); cv::namedWindow("colorImage",CV_WINDOW_AUTOSIZE); moveWindow("colorImage",0,200); while (1) { NUI_IMAGE_FRAME pImageFrame_rgb; NUI_IMAGE_FRAME pImageFrame_depth; //无限等待新的彩色数据,等到后返回 if (WaitForSingleObject(nextColorFrameEvent, 0) == 0) { //从刚才打开数据流的流句柄中得到该帧数据,读取到的数据地址存于pImageFrame hr = m_pNuiSensor->NuiImageStreamGetNextFrame(colorStreamHandle, 0, &pImageFrame_rgb); if (FAILED(hr)) { cout<<"Could not get color image"<<endl; m_pNuiSensor->NuiShutdown(); return -1; } INuiFrameTexture *pTexture = pImageFrame_rgb.pFrameTexture; NUI_LOCKED_RECT lockedRect; //提取数据帧到LockedRect,它包括两个数据对象:pitch每行字节数,pBits第一个字节地址 //并锁定数据,这样当我们读数据的时候,kinect就不会去修改它 pTexture->LockRect(0, &lockedRect, NULL, 0); //确认获得的数据是否有效 if (lockedRect.Pitch != 0) { //将数据转换为OpenCV的Mat格式 for (int i = 0; i < image_rgb.rows; i++) { //第i行的指针 uchar *prt = image_rgb.ptr(i); //每个字节代表一个颜色信息,直接使用uchar uchar *pBuffer = (uchar*)(lockedRect.pBits) + i * lockedRect.Pitch; for (int j = 0; j < image_rgb.cols; j++) { prt[3 * j] = pBuffer[4 * j];//内部数据是4个字节,0-1-2是BGR,第4个现在未使用 prt[3 * j + 1] = pBuffer[4 * j + 1]; prt[3 * j + 2] = pBuffer[4 * j + 2]; } }Point center = Point(image_rgb.cols / 2, image_rgb.rows / 2);int r = 1;circle(image_rgb, center, r, Scalar(255, 255, 255));char output[100] = { 0 };char p[100] = { 0 };sprintf_s(p, "Height");char m[100] = { 0 };sprintf_s(m, "Distance");char t[100] = { 0 };sprintf_s(t, "Gesture");sprintf_s(output, "C:\\Users\\Administrator\\Desktop\\1222\\Kinect\\m_Kinect1\\%s_%d_%s_%d_%s_%d.jpg", p, 1000, m, 1000, t, 1);imwrite(output, image_rgb);//rectangle(image_rgb, Point(image_rgb.rows / 2 - 1, image_rgb.cols / 2 - 1),//Point(image_rgb.rows / 2 + 1, image_rgb.cols / 2 + 1), Scalar(255, 255, 255)); imshow("colorImage",image_rgb); //解除锁定 pTexture->UnlockRect(0); //释放帧 m_pNuiSensor->NuiImageStreamReleaseFrame(colorStreamHandle, &pImageFrame_rgb ); } else { cout<<"Buffer length of received texture is bogus\r\n"<<endl; } BOOL nearMode; INuiFrameTexture* pColorToDepthTexture; //深度图像的处理 if (WaitForSingleObject(nextDepthFrameEvent, INFINITE) == 0) { hr = m_pNuiSensor->NuiImageStreamGetNextFrame(depthStreamHandle, 0 , &pImageFrame_depth); if (FAILED(hr)) { cout<<"Could not get color image"<<endl; NuiShutdown(); return -1; } hr = m_pNuiSensor->NuiImageFrameGetDepthImagePixelFrameTexture( depthStreamHandle, &pImageFrame_depth, &nearMode, &pColorToDepthTexture); INuiFrameTexture *pTexture = pImageFrame_depth.pFrameTexture; NUI_LOCKED_RECT lockedRect; NUI_LOCKED_RECT ColorToDepthLockRect; pTexture->LockRect(0, &lockedRect, NULL, 0); pColorToDepthTexture->LockRect(0,&ColorToDepthLockRect,NULL,0);if (lockedRect.Pitch != 0){for (int i = 0; i < image_depth.rows; i++){uchar *ptr = image_depth.ptr<uchar>(i);uchar *pBufferRun = (uchar*)(lockedRect.pBits) + i * lockedRect.Pitch;USHORT *pBuffer = (USHORT*)pBufferRun;for (int j = 0; j < image_depth.cols; j++){if (i == 240){Depth_value = ((float)pBuffer[image_depth.cols / 2] * 0.123138 + 162.7355);ptr[j] = (uchar)(256 * pBuffer[j] / 0x0fff);}else{ptr[j] = (uchar)(256 * pBuffer[j] / 0x0fff); //直接将数据归一化处理}}}}Mat img_pseudocolor(image_depth.rows, image_depth.cols, CV_8UC3);//构造RGB图像,参数CV_8UC3教程文档里面有讲解 int tmp = 0;for (int y = 0; y<image_depth.rows; y++)//转为伪彩色图像的具体算法 {for (int x = 0; x<image_depth.cols; x++){double L = 256;tmp = image_depth.at<uchar>(y, x);if (tmp < L / 4){img_pseudocolor.at<Vec3b>(y, x)[0] = L; //blue img_pseudocolor.at<Vec3b>(y, x)[1] = 0; //green img_pseudocolor.at<Vec3b>(y, x)[2] = 4 * tmp; //red }else if (tmp < L / 2){img_pseudocolor.at<Vec3b>(y, x)[0] = L; //blue img_pseudocolor.at<Vec3b>(y, x)[1] = 4 * (L / 2 - tmp); //green img_pseudocolor.at<Vec3b>(y, x)[2] = 0; //red }else if (tmp < 3 * L / 4){img_pseudocolor.at<Vec3b>(y, x)[0] = 4 * (L / 2 - tmp); //blue img_pseudocolor.at<Vec3b>(y, x)[1] = 0; //green img_pseudocolor.at<Vec3b>(y, x)[2] = L; //red }else{img_pseudocolor.at<Vec3b>(y, x)[0] = 0; //blue img_pseudocolor.at<Vec3b>(y, x)[1] = 4 * (L - tmp); //green img_pseudocolor.at<Vec3b>(y, x)[2] = L; //red }}}namedWindow("img_pseudocolor");Point center = Point(img_pseudocolor.cols / 2, 240);int r = 1;circle(img_pseudocolor, center, r, Scalar(255, 255, 255));stringstream depth_value;depth_value << Depth_value;putText(img_pseudocolor, depth_value.str(), center, CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255));imshow("img_pseudocolor", img_pseudocolor);char output[100] = { 0 };char p[100] = { 0 };sprintf_s(p, "Height");char m[100] = { 0 };sprintf_s(m, "Distance");char t[100] = { 0 };sprintf_s(t, "Gesture");sprintf_s(output, "C:\\Users\\Administrator\\Desktop\\1222\\Kinect\\m_Kinect1\\%s_%d_%s_%d_%s_%d_%d.jpg", p, 1000, m, 1000, t, 1, 1);imwrite(output, img_pseudocolor); // imshow("depth", image_depth); //接下来是对齐部分,将前景抠出来 //存放深度点的参数 NUI_DEPTH_IMAGE_POINT* depthPoints = new NUI_DEPTH_IMAGE_POINT[640 * 480];if (ColorToDepthLockRect.Pitch != 0) { HRESULT hrState = S_OK; //一个能在不同空间坐标转变的类(包括:深度,彩色,骨骼) INuiCoordinateMapper* pMapper; //设置KINECT实例的空间坐标系 hrState = m_pNuiSensor->NuiGetCoordinateMapper(&pMapper); if (FAILED(hrState)) { return hrState; } //重要的一步:从颜色空间映射到深度空间。参数说明: //【参数1】:彩色图像的类型 //【参数2】:彩色图像的分辨率 //【参数3】:深度图像的分辨率 //【参数4】:深度图像的个数 //【参数5】:深度像素点数 //【参数6】:取内存的大小,个数。类型为NUI_DEPTH_IMAGE_PIXEL //【参数7】:存放映射结果点的参数 hrState = pMapper->MapColorFrameToDepthFrame(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, NUI_IMAGE_RESOLUTION_640x480,640 * 480, (NUI_DEPTH_IMAGE_PIXEL*)ColorToDepthLockRect.pBits,640 * 480, depthPoints); if (FAILED(hrState)) { return hrState; } //显示的图像 Mat show; show.create(480,640,CV_8UC3); show = 0; for (int i = 0; i < image_rgb.rows; i++) {uchar *prt_rgb = image_rgb.ptr(i); uchar *prt_show = show.ptr(i); for (int j = 0; j < image_rgb.cols; j++) { //在内存中偏移量 long index = i * 640 + j; //从保存了映射坐标的数组中获取点 NUI_DEPTH_IMAGE_POINT depthPointAtIndex = depthPoints[index]; Show_value = ((float)prt_show[image_rgb.cols / 2] * 0.123138 + 162.7355); //边界判断 if (depthPointAtIndex.x >= 0 && depthPointAtIndex.x < image_depth.cols && depthPointAtIndex.y >=0 && depthPointAtIndex.y < image_depth.rows) { //深度判断,在MIN_DISTANCE与MAX_DISTANCE之间的当成前景,显示出来 //这个使用也很重要,当使用真正的深度像素点再在深度图像中获取深度值来判断的时候,会出错 if (depthPointAtIndex.depth >= MIN_DISTANCE && depthPointAtIndex.depth <= MAX_DISTANCE) { prt_show[3 * j] = prt_rgb[j * 3]; prt_show[3 * j + 1] = prt_rgb[j * 3 + 1]; prt_show[3 * j + 2] = prt_rgb[j * 3 + 2]; } } } }namedWindow("show");Point center = Point(show.cols / 2, 240);int r = 1;circle(show, center, r, Scalar(255, 255, 255));stringstream show_value;show_value << Show_value;putText(show, show_value.str(), center, CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255));imshow("show", show);char output[100] = { 0 };char p[100] = { 0 };sprintf_s(p, "Height");char m[100] = { 0 };sprintf_s(m, "Distance");char t[100] = { 0 };sprintf_s(t, "Gesture");sprintf_s(output, "C:\\Users\\Administrator\\Desktop\\1222\\Kinect\\m_Kinect1\\%s_%d_%s_%d_%s_%d_%d.jpg", p, 1000, m, 1000, t, 1, 2);imwrite(output, show); // imshow("show", show); } delete []depthPoints; pTexture->UnlockRect(0); m_pNuiSensor->NuiImageStreamReleaseFrame(depthStreamHandle, &pImageFrame_depth); } else { cout<<"Buffer length of received texture is bogus\r\n"<<endl; } } if (cvWaitKey(20) == 27) break; } return 0;}
阅读全文
0 0
- 深度颜色3
- 颜色深度
- 什么是颜色深度(色深)
- rdesktop颜色深度修改
- bpp、颜色深度、FSAA
- 深度颜色1
- 深度颜色2
- framebuffer / 像素深度 / 颜色深度 / RGB565
- 颜色缓冲区和深度缓冲区的对比
- 图像的深度和颜色类型
- 3:颜色
- 手动修改Icon图标的颜色深度(张佩)
- 将深度值压缩进颜色通道的方法
- OpenGL 编程实践 之 改变屏幕分辨率和颜色深度
- 常见颜色深度的 bitmap 之间的相互转换
- wince的24bpp(24位)颜色深度实现
- 常见颜色深度的 bitmap 之间的相互转换
- kinect+openni获取kinect的颜色图像和深度图像
- linux常用文件管理命令
- spring技术内幕笔记:IoC容器的依赖注入
- 国外硕博论文下载网址
- 深度颜色2
- Linux下使用oneinstack安装apache和tomcat
- 深度颜色3
- 用QTJSON、curl与QTSQL、tufao来实现客户端与服务端间通信
- 五、实现关联表查询
- CF501B STL
- Android Studio 切换fragment重叠问题
- 使用Jsoup 抓取页面的数据
- Java常用类之String
- 主席树练习
- Udcity-机器学习进阶-基础-课程10-15