Kinect get Depth map, mouse click to get the point three-dimensional coordinates and save txt

Source: Internet
Author: User
Tags win32
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 ();
//Click Two to get part of the three-dimensional coordinate 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)//Left click, read the initial coordinates, and on the image at that point of the circle {i++;//count clicks p.x = x;
			P.y = y;
			Src.push_back (P); cout << P << static_cast<int> (Img->at<unsigned short> (CV::P oint (x, y))) << Endl;
	

		cout << i << Endl;
		cout << image.at<unsigned short> << Endl;

		cout << P << static_cast<int> (img->at<uchar> (CV::P oint (x, y)) << Endl;
	
			cout << img << endl; Fout << p.x << "<< p.y <<" "<< static_cast<int> img->at<unsigned short> (

		
			CV::P oint (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 (&Amp;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->copy Framedatatoarray (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 = = 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; }


Common errors:

Error LNK2001: unresolved external symbol "private:virtual void __thiscall pcl::normalestimationomp<struct PCL::P ointxyz,struct Pcl::normal>::computefeature (class PCL::P ointcloud<struct pcl::normal> &) "(? computefeature@?$ normalestimationomp@upointxyz@pcl@ @UNormal @2@ @pcl @ @EAEXAAV? $PointCloud @unormal@pcl@@@2@@z)

Correction: Debug mode Win32 change to x64 or x64 to Win32; (match the version you installed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.