内存位置的异常(向量问题)opencv

时间:2016-12-08 12:53:19

标签: c++ image opencv vector

我试图在整个图像大小mxn的6x6窗口内找到平均2x2块像素。我能够找到块的平均值直到第一行结束,当代码必须移动到下一行时,它会在内存位置引发运行时错误"异常"

vector<int>m; vector<int>m1; vector<int>m2; vector<int>m3;vector<int>m4; vector<int>m5; vector<int>m6; vector<int>m7; vector<int>m8;

 for (int i = 2; i < road.rows - 2 ; i++){
    for (int j = 2; j < road.cols - 2 ; j++){           

        //center block          
        int avg=(round((road.at<uchar>(i, j) + road.at<uchar>(i, j + 1) + road.at<uchar>(i + 1, j) + road.at<uchar>(i + 1, j + 1)) / 4));
        //top left block            
        int avg1= (round((road.at<uchar>(i - 2, j - 2) + road.at<uchar>(i - 2, j - 1) + road.at<uchar>(i - 1, j - 2) + road.at<uchar>(i - 1, j - 1)) / 4));
        //top           
        int avg2 = (round((road.at<uchar>(i - 2, j) + road.at<uchar>(i - 2, j + 1) + road.at<uchar>(i - 1, j) + road.at<uchar>(i - 1, j + 1)) / 4));
        //top right block           
        int avg3 = (round((road.at<uchar>(i - 2, j + 2) + road.at<uchar>(i - 2, j + 3) + road.at<uchar>(i - 1, j + 2) + road.at<uchar>(i - 1, j + 3)) / 4));
        //left block            
        int avg4 = (round((road.at<uchar>(i, j - 2) + road.at<uchar>(i, j - 1) + road.at<uchar>(i + 1, j - 2) + road.at<uchar>(i + 1, j - 1)) / 4));
        //right block           
        int avg5 = (round((road.at<uchar>(i, j + 2) + road.at<uchar>(i, j + 3) + road.at<uchar>(i + 1, j + 2) + road.at<uchar>(i + 1, j + 3)) / 4));
        //bottom left block         
        int avg6 = (round((road.at<uchar>(i + 2, j - 2) + road.at<uchar>(i + 2, j - 1) + road.at<uchar>(i + 3, j - 2) + road.at<uchar>(i + 3, j - 1)) / 4));
        //bottom            
        int avg7 = (round((road.at<uchar>(i + 2, j) + road.at<uchar>(i + 2, j + 1) + road.at<uchar>(i + 3, j) + road.at<uchar>(i + 3, j + 1)) / 4));
        //bottom right block            
        int avg8 = (round((road.at<uchar>(i + 2, j + 2) + road.at<uchar>(i + 2, j + 3) + road.at<uchar>(i + 3, j + 2) + road.at<uchar>(i + 3, j + 3)) / 4));
        m.push_back(avg);
        m1.push_back(avg1); 
        m2.push_back(avg2);
        m3.push_back(avg3);
        m4.push_back(avg4);
        m5.push_back(avg5);
        m6.push_back(avg6);
        m7.push_back(avg7);
        m8.push_back(avg8);         
    }
  }

帮助我摆脱这个错误;

0 个答案:

没有答案