Why I get a segmentation fault?

Posted by frx08 on Stack Overflow See other posts from Stack Overflow or by frx08
Published on 2010-03-15T17:48:27Z Indexed on 2010/03/15 17:49 UTC
Read the original article Hit count: 226

Filed under:
|
|

Why I get a segmentation fault?

int main() {
    int height, width, step, step_mono, channels;
    int y, x;
    char str[15];
    uchar *data, *data_mono;

    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* contour = 0;
    CvPoint* p;
    CvFont font;
    CvCapture *capture;
    IplImage *frame = 0, *mono_thres = 0;

    capture = cvCaptureFromAVI("source.avi"); //capture video
    if(!cvGrabFrame(capture)) exit(0);
    frame = cvRetrieveFrame(capture); //capture the first frame from video source

    cvNamedWindow("Result", 1);
    while(1){
        mono_thres = cvCreateImage(cvGetSize(frame), 8, 1);

        height = frame -> height;
        width = frame -> width;
        step = frame -> widthStep;
        step_mono = mono_thres -> widthStep;
        channels = frame -> nChannels;
        data = (uchar *)frame -> imageData;
        data_mono = (uchar *)mono_thres -> imageData;
        //converts the image to a binary highlighting the lightest zone
        for(y=0;y < height;y++)
            for(x=0;x < width;x++)
                data_mono[y*step_mono+x*1+0] = data[y*step+x*channels+0]; 
        cvThreshold(mono_thres, mono_thres, 230, 255, CV_THRESH_BINARY);
        cvFindContours(mono_thres, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
        //gets the coordinates of the contours and draws a circle and the coordinates in that point
        p = CV_GET_SEQ_ELEM(CvPoint, contour, 1);
        cvCircle(frame, *p, 1, CV_RGB(0,0,0), 2);
        cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.1, 1.1, 0, 1);
        sprintf(str, "(%d ,%d)", p->x, p->y);
        cvPutText(frame, str, cvPoint(p->x+5,p->y-5), &font, CV_RGB(0,0,0));
        cvShowImage("Result", mono_thres);
        //next frame
        if(!cvGrabFrame(capture)) break;
        frame = cvRetrieveFrame(capture);
        if((cvWaitKey(10) & 255) == 27) break;
    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("Result");

    return 0;
}

© Stack Overflow or respective owner

Related posts about segmentation-fault

Related posts about c