private void toolStripButton1_Click(object sender, EventArgs e) { // create a stopwatch for FPS calculation this.stopwatch = new Stopwatch(); // for Alpha, one sensor is supported kinectSensor = KinectSensor.GetDefault(); if (kinectSensor != null) { // open sensor kinectSensor.Open(); var frameDescription = kinectSensor.DepthFrameSource.FrameDescription; // get the coordinate mapper coordinateMapper = kinectSensor.CoordinateMapper; // create a new particle depth infos #region Point Cloud #if false List<FxVector3f> Points = new List<FxVector3f>(); List<FxVector3f> Colors = new List<FxVector3f>(); for (int x = 0; x < 512; x += 2) { for (int y = 0; y < 420; y += 2) { for (int z = 0; z < 1; z++) { FxVector3f p; p.x = x * 0.1f; p.z = y * 0.1f; p.y = (float)Math.Log(p.x * p.x * p.x + p.z * p.z * p.z - 3 * p.x - 3 * p.z); Points.Add(p); Colors.Add(rand.NextFxVector3f()); } } } PointCloud pc = new PointCloud(Points, Colors); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(pc); #endif #endregion // open the reader for the depth frames reader = kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color); // allocate space to put the pixels being received and converted frameData = new ushort[frameDescription.Width * frameDescription.Height]; depthImageMatrix = new FxMatrixF(frameDescription.Width, frameDescription.Height); depthImageMatrixAve = depthImageMatrix.Copy(); colorPoints = new ColorSpacePoint[frameDescription.Width * frameDescription.Height]; reader.MultiSourceFrameArrived += reader_MultiSourceFrameArrived; } else MessageBox.Show("Error: failed to open kinect sensor"); }
/// <summary> /// Handles the depth frame data arriving from the sensor /// </summary> /// <param name="sender">object sending the event</param> /// <param name="e">event arguments</param> private void Reader_FrameArrived(object sender, DepthFrameArrivedEventArgs e) { DepthFrameReference frameReference = e.FrameReference; if (this.startTime == null) { this.startTime = frameReference.RelativeTime; } try { DepthFrame frame = frameReference.AcquireFrame(); if (frame != null) { // DepthFrame is IDisposable using (frame) { FrameDescription frameDescription = frame.FrameDescription; #region FPS this.framesSinceUpdate++; // update status unless last message is sticky for a while if (DateTime.Now >= nextStatusUpdate) { // calcuate fps based on last frame received double fps = 0.0; if (stopwatch.IsRunning) { stopwatch.Stop(); fps = framesSinceUpdate / stopwatch.Elapsed.TotalSeconds; stopwatch.Reset(); } nextStatusUpdate = DateTime.Now + TimeSpan.FromSeconds(1); toolStripLabel_fps.Text = fps + " " + (frameReference.RelativeTime - this.startTime).ToString() + "mS"; } if (!stopwatch.IsRunning) { framesSinceUpdate = 0; stopwatch.Start(); } #endregion // verify data and write the new depth frame data to the display bitmap if (((frameDescription.Width * frameDescription.Height) == frameData.Length)) { // Copy the pixel data from the image to a temporary array frame.CopyFrameDataToArray(frameData); coordinateMapper.MapDepthFrameToColorSpace(frameData, colorPoints); // Get the min and max reliable depth for the current frame ushort minDepth = frame.DepthMinReliableDistance; ushort maxDepth = frame.DepthMaxReliableDistance; float imaxDepth = 1.0f / maxDepth; for (int i = 0; i < this.frameData.Length; ++i) { // Get the depth for this pixel ushort depth = this.frameData[i]; // To convert to a byte, we're discarding the most-significant // rather than least-significant bits. // We're preserving detail, although the intensity will "wrap." // Values outside the reliable depth range are mapped to 0 (black). //pixels[i] = 1.0f - ((depth >= minDepth && depth <= maxDepth) ? depth : 0) * imaxDepth; depthImageMatrix[i] = 1.0f - depth * imaxDepth; } if (!oneTimeShot) { oneTimeShot = true; ColorMap cm = new ColorMap(ColorMapDefaults.DeepBlue); List<FxVector3f> Points = new List<FxVector3f>(); List<FxVector3f> Colors = new List<FxVector3f>(); for (int x = 0; x < frameDescription.Width; x++) { for (int y = 0; y < frameDescription.Height; y++) { FxVector3f p; p.x = x * 0.08f; p.z = y * 0.08f; p.y = depthImageMatrix[x,y]*5.0f; Points.Add(p); #if false // calculate index into depth array int depthIndex = (x * frameDescription.Width) + y; // retrieve the depth to color mapping for the current depth pixel ColorSpacePoint colorPoint = this.colorPoints[depthIndex]; // make sure the depth pixel maps to a valid point in color space int colorX = (int)Math.Floor(colorPoint.X + 0.5); int colorY = (int)Math.Floor(colorPoint.Y + 0.5); // calculate index into color array int colorIndex = ((colorY * colorWidth) + colorX) * this.bytesPerPixel; #else byte b = (byte)(depthImageMatrix[x, y]*256); Colors.Add(new FxVector3f(cm[b,0]/256.0f, cm[b,1]/256.0f, cm[b,2]/256.0f)); #endif } } PointCloud pc = new PointCloud(Points, Colors); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(pc); } } } } } catch (Exception) { // ignore if the frame is no longer available } }
private void addPointCloudToolStripMenuItem_Click(object sender, EventArgs e) { mp.Initialize(Engine.g_device); List<FxVector3f> Points = new List<FxVector3f>(); List<FxVector3f> Colors = new List<FxVector3f>(); Random rand = new Random(); FxVector3f min = new FxVector3f(10); FxVector3f max = new FxVector3f(1000); for (int x = 0; x < 512; x += 2) { for (int y = 0; y < 420; y += 2) { for (int z = 0; z < 1; z++) { FxVector3f p; p.x = x * 0.1f; p.z = y * 0.1f; //p.y = (float)(Math.Cos(p.x) * Math.Cos(p.x) + Math.Cos(p.z) * Math.Cos(p.z) + Math.Cos(z) * Math.Cos(z)) * 5; p.y = (float)Math.Log(p.x * p.x * p.x + p.z * p.z * p.z - 3 * p.x - 3 * p.z); Points.Add(p); Colors.Add(rand.NextFxVector3f()); } } } PointCloud pc = new PointCloud(Points, Colors); /// add the mesh to the engine mesh list Engine.g_MeshManager.AddMesh(pc); }