OpenCV:vector subscript out of range

报错

vector subscript out of range

源码

			// 查找轮廓
			vector<vector<Point>> contours;
			vector<Vec4i> hierarcy;

			// 定义外接矩形集合
			vector<Rect> boundRect(contours.size()); 
			int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
			Mat dstRect;
			dstRect = Mat::zeros(swell.size(), CV_8UC3);
			
			findContours(swell, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); 

			Mat dst;
			dst = Mat::zeros(swell.size(), CV_8UC3);

			// contours.size() 表示轮廓总数
			// contours[i].size() 表示第i个轮廓的像素点个数
			// contours[i][0] 表示第i个轮廓的第0个像素点坐标
			for (int i = 0; i < contours.size(); i++)
			{
				Scalar color = Scalar(255, 255, 255);
				boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形
				//这一行开始报错
				//………………
			}

修改

			//vector不能定义在查找轮廓之前
			
			findContours(swell, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); 
			
			// 定义外接矩形集合
			vector<Rect> boundRect(contours.size()); 
			int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
			Mat dstRect;
			dstRect = Mat::zeros(swell.size(), CV_8UC3);

你可能感兴趣的:(OpenCV)