OpenCV c ++ HoughLines转换无法正常工作

时间:2015-03-20 00:43:03

标签: c++ opencv

我使用32位操作系统上的Windows 7上的opencv 2.4.10在visual studio express 2012中用c ++编码,我正在创建一个程序,它将从相机抓取一个帧,检测边缘,然后进行一些测量基于这些边缘的像素位置和长度,然而在成功的canny边缘检测之后,houghlines或findcontours似乎不起作用。下面是我的一些代码和相关的错误,发生了什么?

Mat src, srcEdge, ROImat, templ;
double lowThreshold = 105, highThreshold = lowThreshold*2.2;
int roi_xstart = 140, roi_ystart = 180, roi_width = 360, roi_height = 140, counter = 0, badpartcounter = 0;
Rect ROIrect = cvRect(roi_xstart, roi_ystart, roi_width, roi_height);
Rect matchrect = cvRect(roi_xstart - 10, roi_ystart - 10, roi_width + 20, roi_height + 20);

int main ( int argc, const char** argv )
{   
int input;
if (goodpart == true)
{
    while (looper)
    {   
        cout<<"1. Capture Image\n";
        cout<<"2. Detect Edges of image/create template if first image capture/read\n";
        cout<<"3. Compare Template with Captured/Read Image\n";
        cout<<"4. Reset counter to reset template\n";
        cout<<"5. Read Image\n";
        cout<<"6. HoughLines\n";
        cout<<"7. Find Contours of img\n";
        cout<<"8. Exit program\n";
        cin >> input;   
        switch (input) 
        {
        case 1:
            src = FrameCapture();
            break;
        case 2: 
            srcEdge = CannyEdge ( src, templ);
            cout << "counter = " << counter << endl;
            break;
        case 3:
            MatchingMethod ( srcEdge, templ, goodpart, badpartcounter);
            break;
        case 4:
            counter = 0;
            break;
        case 5:
            src = ReadImage();
            imshow (window1, src);
            waitKey(0);
            destroyWindow(window1);
            break;
        case 6:
            HoughLineTransform (srcEdge);
            break;
        case 7:
             ContourFinding (srcEdge);
             break;
        case 8:
            looper = false;
            break;
        default:
            cout << "incorrect input"<< endl;
        }
        destroyWindow(window1);
    }
}else
{
    looper = false;
}
return 0;
}

这是我创建的功能

//blur image, detect edges, result will be binary (black/white) image of edges detected
Mat CannyEdge (Mat& src, Mat& templ)
{
//declare matrices to be used in edge detection
Mat dst;
Mat src_gray;
Mat detected_edges;
//dst is same size as src, all zeros, 8bit 1 channel
dst.zeros(src.rows, src.cols, CV_8UC1); 
//convert src from 8 bit 3 channel to 8 bit 1 channel grayimage, output is to src_gray
cvtColor( src, src_gray, CV_RGB2GRAY);
//applies normal blur to image to reduce image noise using a kernel of size 3, takes 8bit 1 channel grayimage of src_gray, blurs it and outputs it to detected_edges
blur( src_gray, detected_edges, Size(3,3) );
//applies canny algorithm for edge detection, takes input of detected edges and outputs back to same matrix
Canny ( detected_edges, detected_edges, lowThreshold, highThreshold, 3 ); 
//copies image to dst with the mask output from the canny edge detection function, so every pixel that doesn't fit the mask drops to 0, leaving the edges
src_gray.copyTo(dst, detected_edges); 
//displays image of edges
threshold (dst, dst, 100, 255, 0);
namedWindow(window1, CV_WINDOW_AUTOSIZE);
imshow ( window1, dst);
waitKey(0);
destroyWindow(window1);
//if this is the first image taken, it creates a template from the specified region of interest to compare with the next images taken
if (counter == 0)
{
    Mat ROImat (dst, ROIrect);
    threshold (ROImat, ROImat, 100, 255, 0);
    imshow ( window2, ROImat);
    waitKey(0);
    destroyWindow(window2);
    ROImat.copyTo (templ);
}
counter++;
return dst;
}

//find countours
void ContourFinding (Mat srcEdge)
{

vector<vector<Point> > contouroutput;
vector<Vec4i> hierarchy;
Point ROIstart;
ROIstart.x = roi_xstart, ROIstart.y = roi_ystart;
Mat contourinput (srcEdge, ROIrect);
Mat contourimage = Mat::zeros(contourinput.size(), CV_8UC3);
findContours(contourinput, contouroutput, hierarchy, 0, 1, ROIstart);
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
    Scalar color( rand()&255, rand()&255, rand()&255 );
    drawContours( contourimage, contouroutput, idx, color, CV_FILLED, 8, hierarchy, 2, ROIstart);
}

namedWindow( "Components", 1 );
imshow( "Components", contourinput);
waitKey(0);

}

//Hough Line Transformation function
void HoughLineTransform (Mat srcEdge)
{
Mat cdst;
Mat hdst (srcEdge, matchrect);
vector<Vec4i> lines;
HoughLinesP( hdst, lines, 1, CV_PI/180, 150, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
    line( cdst, Point(lines[i][0], lines[i][1]),
        Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
}

这是轮廓引发的错误

Unhandled exception at 0x54CD1600 (opencv_core2410.dll) in Template Matching.exe: 0xC0000005: Access violation reading location 0x00389738.

然后当我去反汇编

00BD4B8E E8 C1 3B 00 00       call        cv::findContours (0BD8754h)  
00BD4B93 83 C4 1C             add         esp,1Ch 

第二行为&#34;下一个要执行的命令&#34;

然后为hough

Unhandled exception at 0x008952D3 in Template Matching.exe: 0xC0000005: Access violation reading location 0x0036E004.

并在反汇编中

008952CD 89 A5 50 FE FF FF    mov         dword ptr [ebp-1B0h],esp  
008952D3 8B 08                mov         ecx,dword ptr [eax] 

底线是要执行的下一个语句 我不熟悉指针堆栈,内存分配或汇编代码

1 个答案:

答案 0 :(得分:1)

所以这是一个非常简单的修复,在调试模式下,我已经包含了所有其他库,即

opencv_core2410.lib

opencv_core2410d.lib

对于所有模块,删除发布库并保留opencv_core2410d.lib等,使一切正常。当我最初设置项目属性时,我正在配置调试和发布模式,因此包括每个库。