Use OPENNI2 to Kinect2.0. Oni Format Video Acquisition _ depth camera

Source: Internet
Author: User

Based on the first two blogs, the Kinect2.0 can be used for video capture in oni format.

The acquisition procedure is as follows

Status Ccaptruethread::gr_init_kinect (CString videopath) {Status Xresult = STATUS_OK;
	Device Device;
	Const char* Deviceuri = Any_device;
		do {openni::shutdown ();
		Xresult = Openni::initialize ();
			if (STATUS_OK!= xresult) {strlogmsg = _t ("Initialization environment fails, please check that the environment is installed correctly");
			Gr_send_log_message_to_dialog (STRLOGMSG);
			Openni::shutdown ();
		return xresult;
	} while (Device.open (Deviceuri)!= STATUS_OK);
	Strlogmsg = _t ("Initialization of the Kinect environment succeeded");
	Gr_send_log_message_to_dialog (STRLOGMSG);
	Xresult = depth.create (device, sensor_depth);
		if (Xresult!= status_ok) {strlogmsg = _t ("Create depth builder failed");
		Gr_send_log_message_to_dialog (STRLOGMSG);
		Depth.destroy ();
	return xresult;
		else {strlogmsg = _t ("Create depth builder succeeded");
	Gr_send_log_message_to_dialog (STRLOGMSG);
	} Xresult = Color.create (device, Sensor_color);
		if (Xresult!= status_ok) {strlogmsg = _t ("Create RGB generator failed");
		Gr_send_log_message_to_dialog (STRLOGMSG);
		Color.destroy ();
	return xresult; else {strlogmsg = _t ("Create RGB LiveBuilder Success ");
	Gr_send_log_message_to_dialog (STRLOGMSG);
	}//Generate Data Gr_create_image_and_depth_data (device, Videopath);
return xresult; }

Log function is

void Ccaptruethread::gr_send_log_message_to_dialog (cstring& strlogmsg)
{
	::P ostmessage (M_FatherHwnd, Wm_write_log, (WPARAM) strlogmsg.allocsysstring (), (LPARAM) 0);
}

Save depth information and RGB information Video flow control program for

void Ccaptruethread::gr_create_image_and_depth_data (device& Device, CString videopath) {xresult = Color.start ();
	Xresult = Depth.start ();
		if (!depth.isvalid () | | |!color.isvalid ()) {strlogmsg = _t ("Create RGB and depth builder failed");
		Gr_send_log_message_to_dialog (STRLOGMSG);
	Openni::shutdown ();
	int nframes = 0;
	int initframe = 0;
	BOOL bflag = FALSE;
	BOOL bflagdestory = TRUE;
			while (1) {if (g_bsavebackground) {CTime TM = Ctime::getcurrenttime ();
			CString Strbknum; Strbknum.format (_t ("%u"), TM.
			GetTime ());
			CString Bkimgpath = Videopath + _t ("bk\\") + Strbknum + _t (". jpg");
			Bkdepthpath = Videopath + _t ("bk\\") + Strbknum + _t (". bmp");
			CString Bkdepthimgpath = Videopath + _t ("bk\\depthimg") + _t (". bmp");
			Gr_save_image (Bkimgpath, bgrimg);
			Gr_save_image (Bkdepthpath, depthimg);
			Gr_save_image (Bkdepthimgpath, depthimg);
		G_bsavebackground = FALSE;

			} if (G_bsavevideo) {gr_record_video (FilePath);//recording video bflag = TRUE;
			G_section.lock (); G_bsAvevideo = FALSE;
			G_section.unlock ();
		Nframes = 0; } if (Bflag) {if (Nframes <) {::P ostmessage (M_fatherhwnd, Wm_update_progressbar, (WPARAM) Nframes, (
				LPARAM) 0);
			nframes++;
				else {m_record.stop ();
				::P ostmessage (M_fatherhwnd, Wm_update_progressbar, (WPARAM) 0, (LPARAM) 0);
				Break
				Bflag = FALSE;
			Bflagdestory = FALSE; } if (Color.readframe (&onicolorimg) = = STATUS_OK) {Mat cvrgbimg (Onicolorimg.getheight (), ONICOLORIMG.GETW
			Idth (), CV_8UC3, (void*) onicolorimg.getdata ());
			Cvtcolor (cvrgbimg, cvbgrimg, CV_RGB2BGR);
		Resize (cvbgrimg, cvbgrimg, Size (512, 424)); } if (Depth.readframe (&onidepthimg) = = STATUS_OK) {Mat cvrawimg16u (Onidepthimg.getheight (), Onidepthimg.getwid
			Th (), CV_16UC1, (void*) onidepthimg.getdata ());
		Cvrawimg16u.convertto (cvdepthimg, cv_8u, 255.0/(Float (Depth.getmaxpixelvalue ()));
		}//cvtcolor (CVDEPTHIMG,DEPTHTEMP,CV_GRAY2BGR);
		bgrimg = &iplimage (cvbgrimg); Depthimg = &iplimage (cvdepthimg);
		if (Initframe < MB) {initframe++;
			else {if (initframe==100) {::P ostmessage (M_fatherhwnd, WM_CLICKBK, NULL, NULL);
			initframe++;
			}::P ostmessage (M_fatherhwnd, Wm_show_rgbimg, (WPARAM) bgrimg, (LPARAM) 0);
			::P ostmessage (M_fatherhwnd, Wm_show_depthimg, (WPARAM) depthimg, (LPARAM) 1);
		Sleep (20);
			} if (bflagdestory = = FALSE) {M_record.destroy ();
			Bflagdestory = TRUE;
		::P ostmessage (M_fatherhwnd, wm_finish_capture, NULL, NULL);
	} Depth.destroy ();
	Color.destroy ();
	M_record.destroy ();
	Device.close ();
	Openni::shutdown ();
	Cvreleaseimage (&AMP;BGRIMG);
Cvreleaseimage (&AMP;DEPTHIMG); }

The program for customizing the ONI format is as follows

void Ccaptruethread::gr_record_video (CString videoname)
{
	//cstring converted to char*
	TRACE (videoname + _t ("\ n") );
	CStringA Videopathoni (videoname);
	char* Tempvideo = new Char[max_path];
	memset (tempvideo, 0, MAX_PATH);
	memcpy (Tempvideo, Videopathoni.getbuffer (0), strlen (Videopathoni));
	M_record.create (tempvideo);
	M_record.attach (depth, TRUE);
	M_record.attach (color, TRUE);
	M_record.start ();
	Delete Tempvideo;
	Tempvideo = NULL;
}



The above code is based on MFC with OPENNI2 Drive Kinect2.0 acquisition oni format part of the code, do not know how to blog contact bloggers.

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.