0

I am coding in c++ in visual studios express 2012 using opencv 2.4.10 on windows 7 on a 32 bit operating system, i am creating a program that will grab a frame from a camera, detect edges, and then make some measurements based off the pixel location and lengths of those edges, however after a successful canny edge detection, houghlines or findcontours do not seem to work. Below is some of my code and the associated error, what's going on?

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

here are the functions i created

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

here is the error that is thrown for contours

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

and then when i go to disassembly

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

with the 2nd line as "the next command to be executed"

and then for hough

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

and in disassembly

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

the bottom line is the next statement to be executed i'm not familiar with pointer stacks, memory allocation, or assembly codes

3
  • i think it has something to do with memory allocation because the size of the lines vectors are incredibly large, but honestly I am a noob at troubleshooting/debugging so any advice/expertise is greatly appreciated
    – jschnitz1
    Mar 20, 2015 at 0:55
  • where's MatchingMethod ??
    – Micka
    Mar 20, 2015 at 6:20
  • matching method is a separate function that was working fine, so i left the function out. It was for a rudimentary inspection involving template matching, i didn't want to fill up the whole page with code that wasn't relevant to the error
    – jschnitz1
    Mar 20, 2015 at 16:30

1 Answer 1

1

so this is a really easy fix, in debug mode i had all the additional libraries included, i.e.

opencv_core2410.lib

opencv_core2410d.lib

for all of the modules, removing the release libraries and just keeping opencv_core2410d.lib, etc, made it all work. When I originally set the project properties I was configuring both debug and release modes and therefore included every library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.