visual-studio-2010 - 向量超出范围

标签 visual-studio-2010 opencv image-processing vector c++-cli

我正在尝试使用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保留为指针而不是局部变量非常重要,因为局部变量以令人难以置信的方式降低了程序的速度。

最佳答案

您需要找到对哪个 vector 的访问超出了范围。

循环直到轮廓的大小,

for(int i=0;i<contours->size();i++)

但是然后访问(*contoursPoly)[i]
我可能会冒昧地认为contoursPoly已超出其范围,您可以按照建议进入调试器进行检查。

将循环更改为
for(int i=0;i<contours->size() && i<contoursPoly->size();i++)

也许可以解决眼前的问题。

关于visual-studio-2010 - 向量超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17649955/

相关文章:

visual-studio-2010 - 在 powershell 中获取 app.config 元素

opencv - 从外部轮廓创建蒙版以移除图像背景

python - 如何将 cv2 矩形边界框合并为多边形? (不是通过重叠/阈值)

c++ - 错误 : ‘calcOpticalFlowPyrLK’ is not a member of ‘cv’ - OpenCV 2. 4.2

image-processing - 在opencv中查找轮廓的长度

python - 使用python处理来自多个目录的同名数据

c# - 从代码在 VS 中创建 UML 类图

Javascript读取XML文件

c++ - xcode 4 中的赋值运算符崩溃,在 MSVS2010 中运行良好

python - openCV的特征脸