Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Segmentation fault while accessing Mat object

I am facing segmentation fault while accessing the Mat object. I set the resolution of my webcam to 1024X780 and then I loop around the image from index 0 to less than image.rows and image.columns. I am getting segmentation faults at only index values 778 or 779(Near the boundary). Below is my code. Could someone please explain the reason behind this.

cap.set(CV_CAP_PROP_FRAME_WIDTH,1024);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,780);
cap >> src;
src.copyTo(prevFrame);
for(;;){
    cap >> src;
    src.copyTo(nextFrame);
    pixelCount = 0;
    for( int x = 0; x < nextFrame.rows; x++ )
        for( int y = 0; y < nextFrame.cols; y++ )
            if(prevFrame.at<Vec3f>(x,y)[0] == nextFrame.at<Vec3f>(x,y)[0] &&
               prevFrame.at<Vec3f>(x,y)[1] == nextFrame.at<Vec3f>(x,y)[1] &&
               prevFrame.at<Vec3f>(x,y)[2] == nextFrame.at<Vec3f>(x,y)[2]){
                    pixelCount++;
                    if (pixelCount > threshold){
                        cout << pixelCount;
                    return false;
            }
    }