各位大神,我程序循环运行一段时间后,出现错误vector subscript out of range。麻烦给我看一下哪儿错了,十分感谢!
vector<Vec4i> hierarchy;// Vec4i refers to four ints per vector element.
vector<vector<Point> > contours;//contours的类型,双重的vector
//Binary threshold
threshold(matSrc, matSrc, 254, 255, THRESH_BINARY);
findContours(matSrc.clone(), contours, hierarchy, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
//Computing Center Points of Maximally Connected Components
int index=0;
double area, maxArea(0);
for (int i = 0; i < contours.size(); i++)
{
area = contourArea(Mat(contours[i]));
if (area > maxArea)
{
maxArea = area;
index = i;
}
}
vector<Moments> mu(contours.size());
mu[index] = moments(contours[index], false);
//Calculate the center moment :
vector<Point2f> mc(contours.size());
int q ,w;
q = mu[index].m10 / mu[index].m00;
w = mu[index].m01 / mu[index].m00;
mc[index] = Point2f(q, w);
int width = srcImg.cols;
float r = width;
int a = q / r * 100; // Location in this picture
cout << a << endl;