1

我正在使用 OpenCVSharp 2.3,但在使用 CvMat 和 Cv.Houghlines2 方法时遇到问题!HoughLines2 不接受我正在创建的任何 CvMat 对象。我之前使用过 Cv.MemStorage 结构,这很有效,但是它不允许您对要检测的行数设置限制?

我试过这样的事情:

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();

或与 PPHL

CvMat testCvMat2 = new CvMat(maxNoOfLines, 1, MatrixType.S32C4);
lines = srcImgGray.HoughLines2(testCvMat2, HoughLinesMethod.Probabilistic, this.rhoSteps, this.thetaSteps, this.accuThreshold, 50, 10);
testCvMat2.Dispose();

我总是得到同样的异常,这显然是一个 C# 异常:

错误:值不能为空。参数名称:ptr

即使我尝试这样的事情

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);

我总是得到同样的例外,这显然是不对的。

我还搜索了 OpenCVSharp 源代码是否有任何错误代码,但一切似乎都是有效的?!

那么,我做错了什么??有没有人有同样的经历?


好的,我自己找到了解决这个问题的方法:

包装器在 HoughLines2 函数的非托管调用发生的位置有一些错误代码:

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);
    }

返回值导致异常,因为当一个有效的 CvMat 对象被选为 lineStorage 结构时,HoughLines2 返回 nullPtr,这对于在该方法的最后一行构建 CvSeq 对象无效!这必须改变!只需检查结果指针是否为有效指针并处理返回值,该值也可以为 null。

我现在改用 CvInvoke.Houghlines2(...) 并自己处理这个问题。而且效果很好:)!

4

0 回答 0