Kinect获取深度图,鼠标点击获取该点三维坐标并保存txt

// Standard Library
#include <iostream>
// OpenCV Header
#include<opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
// Kinect for Windows SDK Header
#include <Kinect.h>
#include<fstream>
using namespace cv;
using namespace std;
vector<Point> src;
int i = 0;

ofstream fout("01.txt");
void writeMatToFile(cv::Mat& m, Point p, const char* filename)
{
	//ofstream fout(filename);

	if (!fout)
	{
		cout << "File Not Opened" << endl;  return;
	}

	fout <<p.x<<" "<<p.y<<" "<< m.at<unsigned short>(p.x, p.y) << endl;



	fout.close();
}

//点击两下获取部分三维坐标


Point p;
void onMouse(int event, int x, int y, int flags, void *param)
{
	Mat *img = reinterpret_cast<Mat*>(param);
	if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标,并在图像上该点处划圆  
	{
		i++;//统计点击的次数

	

			p.x = x;
			p.y = y;
			src.push_back(p);
			cout << p << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;
			cout << i << endl;
	

		//cout << image.at<unsigned short> << endl;
		//cout << p << static_cast<int>(img->at<uchar>(cv::Point(x, y))) << endl;

		//cout << img << endl;
	
			fout << p.x << " " << p.y << " " << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;

		
			//writeMatToFile(*img, p, "01.txt");

		
	}

}
//using namespace std;
int main(int argc, char** argv)
{
	// 1a. Get default Sensor
	IKinectSensor* pSensor = nullptr;
	GetDefaultKinectSensor(&pSensor);
	// 1b. Open sensor
	pSensor->Open();
	// 2a. Get frame source
	IDepthFrameSource* pFrameSource = nullptr;
	pSensor->get_DepthFrameSource(&pFrameSource);
	// 2b. Get frame description
	int        iWidth = 0;
	int        iHeight = 0;
	IFrameDescription* pFrameDescription = nullptr;
	pFrameSource->get_FrameDescription(&pFrameDescription);
	pFrameDescription->get_Width(&iWidth);
	pFrameDescription->get_Height(&iHeight);
	pFrameDescription->Release();
	pFrameDescription = nullptr;
	// 2c. get some dpeth only meta
	UINT16 uDepthMin = 0, uDepthMax = 0;

	pFrameSource->get_DepthMinReliableDistance(&uDepthMin);
	pFrameSource->get_DepthMaxReliableDistance(&uDepthMax);
	cout << "Reliable Distance: "
		<< uDepthMin << " – " << uDepthMax << endl;
	// perpare OpenCV
	cv::Mat mDepthImg(iHeight, iWidth, CV_16UC1);
	cv::Mat mImg8bit(iHeight, iWidth, CV_8UC1);
	cv::namedWindow("Depth Map");
	// 3a. get frame reader
	IDepthFrameReader* pFrameReader = nullptr;
	pFrameSource->OpenReader(&pFrameReader);
	// Enter main loop
	while (true)
	{
		// 4a. Get last frame
		IDepthFrame* pFrame = nullptr;
		if (pFrameReader->AcquireLatestFrame(&pFrame) == S_OK)
		{
			// 4c. copy the depth map to image
			pFrame->CopyFrameDataToArray(iWidth * iHeight,
				reinterpret_cast<UINT16*>(mDepthImg.data));
			// 4d. convert from 16bit to 8bit


			mDepthImg.convertTo(mImg8bit, CV_8U, 255.0f / uDepthMax);

			namedWindow("image", CV_WINDOW_AUTOSIZE);
			setMouseCallback("image", onMouse, &mDepthImg);
			imshow("image", mImg8bit);

			//cv::imshow("Depth Map", mImg8bit);
			// 4e. release frame
			pFrame->Release();
		}
		// 4f. check keyboard input
		if (cv::waitKey(30) == VK_ESCAPE){
			break;
		}
	}
	// 3b. release frame reader
	pFrameReader->Release();
	pFrameReader = nullptr;
	// 2d. release Frame source
	pFrameSource->Release();
	pFrameSource = nullptr;
	// 1c. Close Sensor
	pSensor->Close();
	// 1d. Release Sensor
	pSensor->Release();
	pSensor = nullptr;
	return 0;
}

 

常见错误:

错误 38 error LNK2001: 无法解析的外部符号 "private: virtual void __thiscall pcl::NormalEstimationOMP<struct pcl::PointXYZ,struct pcl::Normal>::computeFeature(class pcl::PointCloud<struct pcl::Normal> &)" (?computeFeature@?$NormalEstimationOMP@UPointXYZ@pcl@@UNormal@2@@pcl@@EAEXAAV?$PointCloud@UNormal@pcl@@@2@@Z)

改正:debug模式win32改为x64或者x64改为win32;(与自己安装的版本要匹配)

 

觉得有帮助,可以来点赞赏哦!!   谢谢客官 (* ̄︶ ̄)

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值