Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 5.32 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. OpenNI Face Detection in .NET Windows Form Application
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using OpenNI;
  11. using System.Threading;
  12. using System.Drawing.Imaging;
  13.  
  14.  
  15. namespace CameraApp
  16.     {
  17.         public partial class MainWindow : Form
  18.  
  19.     {
  20.     public MainWindow()
  21.     {
  22.     InitializeComponent();
  23.  
  24.     this.context = Context.CreateFromXmlFile(SAMPLE_XML_FILE, out scriptNode);
  25.     this.depth = context.FindExistingNode(NodeType.Depth) as DepthGenerator;
  26.     if (this.depth == null)
  27.     {
  28.     throw new Exception("Viewer must have a depth node!");
  29.     }
  30.  
  31.     this.histogram = new int[this.depth.DeviceMaxDepth];
  32.  
  33.     MapOutputMode mapMode = this.depth.MapOutputMode;
  34.  
  35.     this.bitmap = new Bitmap((int)mapMode.XRes, (int)mapMode.YRes, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  36.     this.shouldRun = true;
  37.     this.readerThread = new Thread(ReaderThread);
  38.     this.readerThread.Start();
  39.     }
  40.  
  41.     protected override void OnPaint(PaintEventArgs e)
  42.     {
  43.     base.OnPaint(e);
  44.  
  45.     lock (this)
  46.     {
  47.     e.Graphics.DrawImage(this.bitmap,
  48.     this.panelView.Location.X,
  49.     this.panelView.Location.Y,
  50.     this.panelView.Size.Width,
  51.     this.panelView.Size.Height);
  52.     }
  53.     }
  54.  
  55.     protected override void OnPaintBackground(PaintEventArgs pevent)
  56.     {
  57.     //Don't allow the background to paint
  58.     }
  59.  
  60.     protected override void OnClosing(CancelEventArgs e)
  61.     {
  62.     this.shouldRun = false;
  63.     this.readerThread.Join();
  64.     base.OnClosing(e);
  65.     }
  66.  
  67.     protected override void OnKeyPress(KeyPressEventArgs e)
  68.     {
  69.     if (e.KeyChar == 27)
  70.     {
  71.     Close();
  72.     }
  73.     base.OnKeyPress(e);
  74.     }
  75.  
  76.     private unsafe void CalcHist(DepthMetaData depthMD)
  77.     {
  78.     // reset
  79.     for (int i = 0; i < this.histogram.Length; ++i)
  80.     this.histogram[i] = 0;
  81.  
  82.     ushort* pDepth = (ushort*)depthMD.DepthMapPtr.ToPointer();
  83.  
  84.     int points = 0;
  85.     for (int y = 0; y < depthMD.YRes; ++y)
  86.     {
  87.     for (int x = 0; x < depthMD.XRes; ++x, ++pDepth)
  88.     {
  89.     ushort depthVal = *pDepth;
  90.     if (depthVal != 0)
  91.     {
  92.     this.histogram[depthVal]++;
  93.     points++;
  94.     }
  95.     }
  96.     }
  97.  
  98.     for (int i = 1; i < this.histogram.Length; i++)
  99.     {
  100.     this.histogram[i] += this.histogram[i - 1];
  101.     }
  102.  
  103.     if (points > 0)
  104.     {
  105.     for (int i = 1; i < this.histogram.Length; i++)
  106.     {
  107.     this.histogram[i] = (int)(256 * (1.0f - (this.histogram[i] / (float)points)));
  108.     }
  109.     }
  110.     }
  111.  
  112.     private unsafe void ReaderThread()
  113.     {
  114.     DepthMetaData depthMD = new DepthMetaData();
  115.  
  116.     while (this.shouldRun)
  117.     {
  118.     try
  119.     {
  120.     this.context.WaitOneUpdateAll(this.depth);
  121.     }
  122.     catch (Exception)
  123.     {
  124.     }
  125.  
  126.     this.depth.GetMetaData(depthMD);
  127.  
  128.     CalcHist(depthMD);
  129.  
  130.     lock (this)
  131.     {
  132.     Rectangle rect = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height);
  133.     BitmapData data = this.bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  134.  
  135.     //ushort* pDepth = (ushort*)this.depth.DepthMapPtr.ToPointer();
  136.  
  137.     //// set pixels
  138.     //for (int y = 0; y < depthMD.YRes; ++y)
  139.     //{
  140.     // byte* pDest = (byte*)data.Scan0.ToPointer() + y * data.Stride;
  141.     // for (int x = 0; x < depthMD.XRes; ++x, ++pDepth, pDest += 3)
  142.     // {
  143.     // byte pixel = (byte)this.histogram[*pDepth];
  144.     // pDest[0] = 0;
  145.     // pDest[1] = pixel;
  146.     // pDest[2] = pixel;
  147.     // }
  148.     //}
  149.  
  150.     // This will point to the depth image.
  151.     ushort* pDepth = (ushort*)this.depth.DepthMapPtr.ToPointer();
  152.  
  153.     // This will point to the RGB image.
  154.     RGB24Pixel* pRGB =
  155.     (RGB24Pixel*)this.depth.DepthMapPtr.ToPointer();
  156.  
  157.     // Go over the depth and RGB image and set the bitmaps
  158.     // we're copying to based on our depth & RGB values.
  159.     for (int y = 0; y < depthMD.YRes; ++y)
  160.     {
  161.     // Assuming that the size of each data frame is
  162.     // 640x480.
  163.     // Scan line by line (480 lines), each line
  164.     // consists of 640 pointers.
  165.     byte* pDest_Depth =
  166.     (byte*)data.Scan0.ToPointer() + y *
  167.     data.Stride;
  168.     byte* pDest_Rgb = (byte*)data.Scan0.ToPointer()
  169.     + y * data.Stride;
  170.  
  171.     for (int x = 0; x < depthMD.XRes; ++x,
  172.     ++pDepth, pDest_Depth += 3,
  173.     ++pRGB, pDest_Rgb += 3)
  174.     {
  175.     // Change the color of the bitmap
  176.     // based on depth value.
  177.     byte pixel = (byte)this.histogram[*pDepth];
  178.     pDest_Depth[0] = 0;
  179.     pDest_Depth[1] = pixel;
  180.     pDest_Depth[2] = pixel;
  181.  
  182.     // Get the RGB values to generate
  183.     // a whole RGB image.
  184.     byte red = pRGB->Red;
  185.     byte green = pRGB->Green;
  186.     byte blue = pRGB->Blue;
  187.  
  188.     // Get depth information.
  189.     ushort depthVal = *pDepth;
  190.     }
  191.     }
  192.  
  193.     this.bitmap.UnlockBits(data);
  194.     }
  195.  
  196.     this.Invalidate();
  197.     }
  198.     }
  199.  
  200.     private readonly string SAMPLE_XML_FILE = @"C:/Program Files/Microsoft Visual Studio 10.0/Microsoft Visual Studio 2010 Ultimate - ENU/OpenNI/Data/SamplesConfig.xml";
  201.  
  202.     private Context context;
  203.     private ScriptNode scriptNode;
  204.     private DepthGenerator depth;
  205.     private Thread readerThread;
  206.     private bool shouldRun;
  207.     private Bitmap bitmap;
  208.     private int[] histogram;
  209.  
  210.     private void button1_Click(object sender, EventArgs e)
  211.     {
  212.     this.readerThread.Abort();
  213.     this.bitmap.Save("D:\Screenshot.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
  214.     this.readerThread = new Thread(ReaderThread);
  215.     this.readerThread.Start();
  216.     }
  217.     }
  218.     }