title: Experiencing problems using OpenCVSharp HoughLines2 method with CvMat
body: <p>I'm using OpenCVSharp 2.3 and I'm experiencing problems using CvMat and the Cv.Houghlines2 Method ! HoughLines2 doesn't accept any CvMat object I'm creating. I used the Cv.MemStorage structure before, and this works, but it doesn't let you set a limit on how many lines you want to get detected??</p>
  
  <p>I've tried something like this:</p>
  
  <pre><code>CvMat testCvMat1 = new CvMat(maxNoOfLines,1, MatrixType.F32C2);
  // Line detection
  OpenCvSharp.CvSeq lines = srcImgGray.HoughLines2(storage, OpenCvSharp.HoughLinesMethod.Standard, this.rhoSteps, this.thetaSteps, this.accuThreshold, 0, 0);
  testCvMat1.Dispose();
  </code></pre>
  
  <p>or with PPHL</p>
  
  <pre><code>CvMat testCvMat2 = new CvMat(maxNoOfLines, 1, MatrixType.S32C4);
  lines = srcImgGray.HoughLines2(testCvMat2, HoughLinesMethod.Probabilistic, this.rhoSteps, this.thetaSteps, this.accuThreshold, 50, 10);
  testCvMat2.Dispose();
  </code></pre>
  
  <p>and I get always the same exception, which obviously is a C# exception:</p>
  
  <blockquote>
    <p>Error: Value cannot be null. Parameter name: ptr</p>
  </blockquote>
  
  <p>Even when I try somthing like this</p>
  
  <pre><code>CvMat testCvMat1 = new CvMat(maxNoOfLines,1, MatrixType.F32C2, new float[limit,2]);
  CvMat testCvMat1 = new CvMat(1, maxNoOfLines, MatrixType.F32C2, new float[maxNoOfLines, 2]);
  CvMat testCvMat1 = new CvMat(1, maxNoOfLines, MatrixType.F32C2);
  CvMat testCvMat1 = new CvMat(1, maxNoOfLines, MatrixType.F32C2, 0);
  </code></pre>
  
  <p>I always get the same exception, which obviously cannot be right.</p>
  
  <p>I also searched the OpenCVSharp source code for any wrong code, but everything seems to be valid?! </p>
  
  <p>So, what am I doing wrong?? Has anybody made the same experience?</p>
  
  <hr>
  
  <p>Ok, I found the solution to this problem myself:</p>
  
  <p>The wrapper has some erroneous code at the position where the unmanaged call of the HoughLines2 funktion takes place:</p>
  
  <pre><code>public static CvSeq HoughLines2(CvArr image, CvMat line_storage, HoughLinesMethod method, double rho, double theta, int threshold, double param1, double param2)
      {
          if (image == null)
              throw new ArgumentNullException("image");
          if (line_storage == null)
              throw new ArgumentNullException("line_storage");
          IntPtr result = CvInvoke.cvHoughLines2(image.CvPtr, line_storage.CvPtr, method, rho, theta, threshold, param1, param2);
          return new CvSeq(result);
      }
  </code></pre>
  
  <p>the return value causes the exception, because when a valid CvMat object has been chosen as lineStorage Structure, then HoughLines2 returns nullPtr, which is not valid for building the CvSeq object in the last line of this method !! This has to be changed ! Simply check wether the result pointer is a valid pointer or not and handle the return value, that can be null as well.</p>
  
  <p>I now use CvInvoke.Houghlines2(...) instead and handle that problem myself. And it works great :) !</p>