我想用OpenCV绘制围绕轮廓的边界框。这是所有的图像从摄像机实时抢下了实时应用,而以下是代码向量超出范围
的重要组成部分RTMotionDetector.h
vector<vector<Point>> *contours;
vector<vector<Point>> *contoursPoly;
RTMotionDetector.cpp
RTMotionDetector::RTMotionDetector(void)
{
current = new Mat();
currentGrey = new Mat();
canny = new Mat();
next = new Mat();
absolute = new Mat();
cam1 = new VideoCapture();
cam2 = new VideoCapture();
contours = new vector<vector<Point>>();
contoursPoly = new vector<vector<Point>>();
boundRect = new vector<Rect>();
}
double RTMotionDetector::getMSE(Mat I1, Mat I2)
{
Mat s1;
//Find difference
cv::absdiff(I1, I2, s1); // |I1 - I2|
imshow("Difference",s1);
//Do canny to get edges
cv::Canny(s1,*canny,30,30,3);
imshow("Canny",*canny);
//Find contours
findContours(*canny,*contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
//System::Windows::Forms::MessageBox::Show(""+contours->size());
//Draw contours
drawContours(*current,*contours,-1,Scalar(0,0,255),2);
for(int i=0;i<contours->size();i++)
{
cv::approxPolyDP(Mat((*contours)[i]),(*contoursPoly)[i],3,true);
//boundRect[i] = boundingRect(contoursPoly[i]);
}
}
一旦下面的部分得到执行,我得到一个错误
cv::approxPolyDP(Mat((*contours)[i]),(*contoursPoly)[i],3,true);
这是我收到的错误。
如果我注释掉的代码,则没有问题。我知道这是ArrayIndexOutOfBounds
问题,但我真的找不到修复程序。可能是因为我是Windows编程新手。
contours
作为一个指针而不是本地变量保留是非常重要的,因为局部变量以一种令人难以置信的方式减慢了程序的速度。
请帮我解决这个问题。谢谢。
我从来没有在一个地方见过这么多'新'。如果你绝对必须有指针**,请使用智能指针**。 – chris
按重试,调试应用程序。有机会你没有用任何东西来填充你的矢量。 –
@TheForest和TheTrees:我做到了。没有什么变化:( –