title: How to call OpenCV's MatchTemplate method from C#
body: <p>I'm trying to get the template's position on the image using <a href="https://github.com/shimat/opencvsharp" rel="nofollow">OpenCVSharp</a> library from <a href="https://www.nuget.org/packages/OpenCvSharp-AnyCPU/" rel="nofollow">NuGet</a>. Here is the code I've wrote:</p>
  
  <pre><code>var image = Cv.LoadImage("Image.png");
  var template = Cv.LoadImage("Template.png");
  var w = (image.Width - template.Width) + 1;
  var h = (image.Height - template.Height) + 1;
  IplImage result = new IplImage(w, h, BitDepth.F32, 1);
  
  Console.WriteLine("Image: {0} {1}", image.GetSize(), image.ElemType);
  Console.WriteLine("Template: {0} {1}", template.GetSize(), template.ElemType);
  Console.WriteLine("Result: {0} {1}", result.GetSize(), result.ElemType);
  
  image.MatchTemplate(image, result, MatchTemplateMethod.CCoeffNormed); // throws exception
  
  double minVal, maxVal;
  CvPoint minLoc, maxLoc;
  result.MinMaxLoc(out minVal, out maxVal, out minLoc, out maxLoc);
  Console.WriteLine(maxLoc);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Image: CvSize (Width:2048 Height:1536) U8C3
  Template: CvSize (Width:169 Height:128) U8C3
  Result: CvSize (Width:1880 Height:1409) F32C1
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>OpenCvSharp.OpenCVException : result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1) &amp;&amp; result.type() == CV_32F</p>
  </blockquote>
  
  <p>What's wrong? Where is the error? The size, bit depth and channel number of the result array look correct, but the method still rises the exception.</p>