我有从HoughLinesP转换中获得的向量“ lines2”。而且我有二进制图像的距离转换。
Lines2仅具有线条的起点和终点,因此使用LineIterator可以遍历所有线条并获取这些点之间的坐标。然后,我使用这些坐标从距离图像中获取值。最后,我想将所有值存储在“ lines3”中,而不是在i ++时存储下一个等。
这是代码:
distanceTransform(img_bin, img_dist, CV_DIST_C, 3);
imshow("Distance Transform Image", img_dist);
//Go through the merged lines with LineIterator to get all the points between the endings
vector<vector<int>> lines3(lines2.size());
int val;
for (size_t i = 0; i < lines2.size(); i++) {
LineIterator it(img_dist, Point(lines2[i][0], lines2[i][1]), Point(lines2[i][2], lines2[i][3]), 8);
vector<Point> points(it.count);
for (int j = 0; j < it.count; j++, ++it)
{
points[j] = it.pos();
//cout << "points[j]: " << points[j] << endl;
//check the value of pos() at the distance transformated image and store it in lines3
val = img_dist.at<float>(points[j].y, points[j].x);
//lines3(i,j)[0].push_back(val);
//lines3[i][j].push_back(val);
//lines3[i][j] = val;
//lines3(i,j) = val;
//lines3[j].push_back(val);
lines3[i][j].push_back(val); //THIS LINE doeasn't even compile
}
}
问题:我该怎么做? push_back在这里不起作用。
编辑:
为“ lines3”添加了一些初始大小。但仍然不知道如何用[i] [j]用线和线的值填充向量。
lines3 [j] .push_back(val); //此行可编译,但运行时失败,出现超出范围的错误(当然)
最佳答案
我将其发布以备将来使用,因此也许其他人不会像我这样坚持下去。
正如DanMašek在评论中提到的,我需要替换
lines3[i][j].push_back(val);
与
lines3[i].push_back(val);
现在工作正常!谢谢!
关于c++ - 如何在OPENCV C++中将元素存储到 vector 的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40476673/