title: How can I find a template image in another image? Kinect and AForge preferred
body: <p>I copied the AForge-Sample from here:
  <a href="http://www.aforgenet.com/framework/features/template_matching.html" rel="nofollow">http://www.aforgenet.com/framework/features/template_matching.html</a>
  And hoped, it would work with 2 Bitmaps as sources as in the following code:</p>
  
  <pre><code>    Bitmap findTemplate (Bitmap sourceImage, Bitmap template)
      {
  
      // create template matching algorithm's instance
      // (set similarity threshold to x.y%, 1.0f = 100%)
      ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching( 0.4f );
      // find all matchings with specified above similarity
      TemplateMatch[] matchings = tm.ProcessImage( sourceImage, template ); **// "Unsupported pixel format of the source or template image." as error message**
      // highlight found matchings
      BitmapData data = sourceImage.LockBits(
          new Rectangle( 0, 0, sourceImage.Width, sourceImage.Height ),
          ImageLockMode.ReadWrite, sourceImage.PixelFormat );
      foreach ( TemplateMatch m in matchings )
      {
          AForge.Imaging.Drawing.Rectangle( data, m.Rectangle, System.Drawing.Color.White );
          // do something else with matching
      }
      sourceImage.UnlockBits( data );
      return sourceImage;
      }
  </code></pre>
  
  <p>But when calling TemplateMatch[] matchings = tm.P.... it gives the error mentioned above.
  The template is generated this way:</p>
  
  <pre><code>Bitmap templatebitmap=(Bitmap)AForge.Imaging.Image.FromFile("template.jpg");
  </code></pre>
  
  <p>the source is generated with the kinect-webcam, where the PlanarImage is formatted as Bitmap (method copied from somewhere, but it was working up to now)</p>
  
  <pre><code> Bitmap PImageToBitmap(PlanarImage PImage)
          {
              Bitmap bmap = new Bitmap(
                PImage.Width,
                PImage.Height,
                System.Drawing.Imaging.PixelFormat.Format32bppRgb);
              BitmapData bmapdata = bmap.LockBits(
                new Rectangle(0, 0, PImage.Width,
                                     PImage.Height),
                ImageLockMode.WriteOnly,
                bmap.PixelFormat);
              IntPtr ptr = bmapdata.Scan0;
              Marshal.Copy(PImage.Bits,
                           0,
                           ptr,
                           PImage.Width *
                              PImage.BytesPerPixel *
                                   PImage.Height);
              bmap.UnlockBits(bmapdata);
              return bmap;
          }
  </code></pre>
  
  <p>So, is anbody able to help me, where my mistake might be?
  Or maybe anyone knows a better way to match a template with a Kinect?
  The overall job is to detect a known object with the kinect, in my case a rubberduck.</p>
  
  <p>Thank you in advamce.</p>