-3

请看下面的代码

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

void houghTransform(int,void*);
Mat image,lines,dst,cdst;
int thresh;
const char* imageWindow = "Image Window";

int main()
{
    image = imread("DSC01894.jpg");

    //Turning the dst image into greyscale


    if(image.data!=0)
    {
        cv::Canny(image,dst,50,200,3);
        cv::cvtColor(dst,cdst,CV_GRAY2BGR);

        cv::createTrackbar("Threshold",imageWindow,&thresh,255,houghTransform);
        houghTransform(0,0);
    }
    else
    {
        cout << "Image cannot be read" << endl;
    }

    namedWindow("Image");
    imshow("Image",image);

    waitKey(0);

}

void houghTransform(int, void *)
{
    vector<Vec4i>lines;

    cv::HoughLinesP(dst,lines,1,CV_PI/180,thresh,50,10);

    for(size_t i=0;i<lines.size();i++)
    {
        Vec4i l = lines[i];

        cv::line(cdst,Point(l[0],l[1]),Point(l[2],l[3]),Scalar(0,0,255),3,CV_AA);
    }

    imshow(imageWindow,cdst);
}

当它运行时,我收到运行时错误,

One of arguments' values is out of range. 它应该在

cv::HoughLinesP(dst,lines,1,CV_PI/180,thresh,50,10);或者

cv::line(cdst,Point(l[0],l[1]),Point(l[2],l[3]),Scalar(0,0,255),3,CV_AA);

为什么是这样?

4

2 回答 2

3

我得到了这个例外,即

OpenCV Error: One of arguments' values is out of range (rho, theta and threshold
 must be positive) in unknown function, file C:\slave\builds\WinInstallerMegaPac
 k\src\opencv\modules\imgproc\src\hough.cpp, line 718

这是在这个代码

    if( rho <= 0 || theta <= 0 || threshold <= 0 )
        CV_Error( CV_StsOutOfRange, "rho, theta and threshold must be positive" );

其中cvHoughLines2()由 cv::HoughLinesP() 调用。

传入的参数HoughLinesP()是:

rho=1
theta=0.0174533
threshold=0

存在问题:阈值不允许为0。

于 2013-06-06T11:34:12.537 回答
0
Vec4i l = lines[i];

这里, l可能只有一个元素

或在:

cv::line(cdst,Point(l[0],l[1]),Point(l[2],l[3]),Scalar(0,0,255),3,CV_AA);

线端点可以指示图像的边界之外。您可以检查Point(l[0],l[1])Point(l[2],l[3])如果l元素少于 4 个,那么其他剩余点只是垃圾,可能有一些line()方法无法自然处理的巨大值。

于 2013-06-06T06:54:59.053 回答