static void Main(string[] args) { Console.WriteLine("------------------------------------------"); Console.WriteLine("MetriCam 2 Minimal Sample."); Console.WriteLine("Get MetriCam 2 at http://www.metricam.net/"); Console.WriteLine("------------------------------------------"); // Create camera object Camera camera; try { camera = new Kinect2(); } catch (Exception e) { Console.WriteLine(Environment.NewLine + "Error: Could not create a PrimeSense camera object."); Console.WriteLine((e.InnerException == null) ? e.Message : e.InnerException.Message); Console.WriteLine("Press any key to exit."); Console.ReadKey(); return; } // Connect, get one frame, disconnect Console.WriteLine("Connecting camera"); camera.Connect(); Console.WriteLine("Fetching one frame"); camera.Update(); try { Console.WriteLine("Accessing color data"); ColorCameraImage img = (ColorCameraImage)camera.CalcChannel(ChannelNames.Color); Bitmap rgbBitmapData = img.ToBitmap(); } catch (ArgumentException ex) { Console.WriteLine(String.Format("Error getting channel {0}: {1}.", ChannelNames.Color, ex.Message)); } try { Console.WriteLine("Accessing distance data"); FloatCameraImage distancesData = (FloatCameraImage)camera.CalcChannel(ChannelNames.Distance); } catch (ArgumentException ex) { Console.WriteLine(String.Format("Error getting channel {0}: {1}.", ChannelNames.Distance, ex.Message)); } Console.WriteLine("Disconnecting camera"); camera.Disconnect(); Console.WriteLine("Finished. Press any key to exit."); Console.ReadKey(); }
/// <summary> /// Device-specific implementation of Update. /// Updates data buffers of all active channels with data of current frame. /// </summary> /// <remarks>This method is implicitely called by <see cref="Camera.Update"/> inside a camera lock.</remarks> /// <seealso cref="Camera.Update"/> protected override unsafe void UpdateImpl() { bool synced = true; // Wait until a frame is ready: Synchronized or Asynchronous pxcmStatus status; status = pp.AcquireFrame(synced); if (status < pxcmStatus.PXCM_STATUS_NO_ERROR) { log.Error(Name + ": " + status.ToString()); return; } // get image sample = pp.QuerySample(); long imgTS = sample.color.timeStamp; if (imgTS <= lastTimeStamp) { throw new TimeoutException("THIS IS NOT A TIMEOUT!"); } lastTimeStamp = imgTS; // color image PXCMImage.ImageData colorData; if (sample.color != null) { sample.color.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB24, out colorData); Bitmap bmp = new Bitmap(sample.color.info.width, sample.color.info.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); var bmpData = bmp.LockBits(new Rectangle(0, 0, sample.color.info.width, sample.color.info.height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); memcpy(bmpData.Scan0, colorData.planes[0], new UIntPtr(3 * (uint)sample.color.info.width * (uint)sample.color.info.height)); bmp.UnlockBits(bmpData); Bitmap bmp32 = bmp.Clone(new Rectangle(0, 0, widthColor, heightColor), System.Drawing.Imaging.PixelFormat.Format32bppPArgb); colorImage = new ColorCameraImage(bmp32); sample.color.ReleaseAccess(colorData); } // depth PXCMImage.ImageData depthData; if (sample.depth != null) { sample.depth.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH_F32, out depthData); depthImage = new FloatCameraImage(sample.depth.info.width, sample.depth.info.height); CopyImageWithStride(sample.depth.info.width, sample.depth.info.height, 4, depthData, new IntPtr(depthImage.Data)); sample.depth.ReleaseAccess(depthData); } pp.ReleaseFrame(); }
public void ColorImage() { if (camera.IsConnected) { camera.Update(); ColorCameraImage img = (ColorCameraImage)camera.CalcChannel(MetriCam2.ChannelNames.Color); img_color = img.ToBitmap(); System.Drawing.Bitmap b = new System.Drawing.Bitmap(img_color); Texture2D tx = new Texture2D(img_color.Width, img_color.Height, TextureFormat.BGRA32, false); tx.LoadRawTextureData(imageToByteArray(b)); tx.Apply(); ColorImg.texture = tx; img.Dispose(); } }
/// <summary> /// Calculates the color image for the current frame. /// </summary> /// <returns>Color image.</returns> protected virtual unsafe ColorCameraImage CalcColor() { Bitmap bmp = null; lock (this.colorFrameData) { lock (cameraLock) { bmp = new Bitmap(ColorWidth, ColorHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); BitmapData bData = bmp.LockBits(new Rectangle(new Point(0, 0), new Size(ColorWidth, ColorHeight)), ImageLockMode.WriteOnly, bmp.PixelFormat); System.Runtime.InteropServices.Marshal.Copy(this.colorFrameData, 0, bData.Scan0, bData.Width * bData.Height * this.bytesPerPixel); bmp.UnlockBits(bData); bmp.RotateFlip(RotateFlipType.RotateNoneFlipX); //Kinect images are flipped in x-direction } ColorCameraImage img = new ColorCameraImage(bmp); img.TimeStamp = timestampColor; return(img); } }
/// <summary> /// Extracts a bitmap representation from the current raw-data. /// </summary> /// <returns>Bitmap representation of the current frame.</returns> private ColorCameraImage CalcColor() { ColorCameraImage result = new ColorCameraImage((int)Width, (int)Height); result.FrameNumber = FrameNumber; if (IsMonochrome) { FloatCameraImage img = CalcChannelFloat(0); float factor = (32 == bitsPerPixel) ? 255.0f : 1.0f; BitmapData bData = result.Data.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); for (int y = 0; y < result.Data.Height; y++) { byte *linePtr = (byte *)bData.Scan0 + y * bData.Stride; for (int x = 0; x < result.Data.Width; x++) { byte d = (byte)(img[y, x] * factor); * linePtr++ = d; * linePtr++ = d; * linePtr++ = d; } } result.Data.UnlockBits(bData); } else { BitmapData bData = result.Data.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); if (bData.Stride != stride) { result.Data.UnlockBits(bData); throw new InvalidOperationException("Stride is incompatible."); } CopyMemory((byte *)bData.Scan0, imagePtr, (uint)(stride * imageHeight)); result.Data.UnlockBits(bData); } return(result); }