-2

I made a little piece of code which causes a segmentation error but I do not understand why. Hope you guys can clarify.

Code:

   for(unsigned int i = 0; i < Objects.size(); i++)
   {

   if (Objects[i].size() > 10)
           Objects2[i] = Objects[i];
   }

Objects is declared as vector > because of the opencv function findContour().

Gr!

4

3 回答 3

2

让我猜猜。您没有初始化Objects2为正确的大小,因此

Objects2[i] = Objects[i];

是越界访问。

你可能会考虑

Objects2.push_back(Objects[i]);
于 2013-11-05T10:02:30.627 回答
0

由于下标运算符没有抛出异常,因此似乎分段错误的原因是语句

Objects2[i] = Objects[i];

你可以用它代替

Objects2.at( i ) = Objects[i];

并捕获异常以确定哪个索引无效。

于 2013-11-05T10:04:23.717 回答
0

轮廓的面积可以使用contourArea来计算。尝试:

for(int i = 0; i < Objects.size(); i++) {
    if (contourArea(Objects[i]) > 10) {            
        Objects.erase(Objects.begin() + i);
        i--;
    }
}
于 2013-11-05T10:05:01.630 回答