Segmentation fault while accessing Mat object

asked 2015-05-20 07:11:29 -0500

aries gravatar image

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;
            }
    }
edit retag flag offensive close merge delete

Comments

2
  • images from webcam are 24bit bgr, so prevFrame.at<Vec3b>() not Vec3f.
  • try to avoid slow and error-prone loops. you could just use absdiff() and sum().
berak gravatar imageberak ( 2015-05-20 07:43:58 -0500 )edit