简体   繁体   English

如何在OPENCV C ++中将元素存储到向量的向量

[英]How to store elements to vector of vectors in OPENCV C++

I have the vector "lines2" that I get from HoughLinesP transformation. 我有从HoughLinesP转换中获得的向量“ lines2”。 And I have the distance transformation of the binary image. 而且我有二进制图像的距离转换。 Lines2 only has the starting and ending points of the lines, so with LineIterator I go through all the lines and get the coordinates in between those points. Lines2仅具有线条的起点和终点,因此使用LineIterator可以遍历所有线条并获取这些点之间的坐标。 I then use these coordinates to get the values from the distance image. 然后,我使用这些坐标从距离图像中获取值。 Finally I would like to store all the values in "lines3", than when i++, store the next etc. Here is the code: 最后,我想将所有值存储在“ 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
    }
}

Question: how can I do that? 问题:我该怎么做? The push_back is not working here. push_back在这里不起作用。

EDIT: added some initial size to 'lines3'. 编辑:添加一些初始大小到'lines3'。 But still don't know how can I fill the vector by [i][j] with the lines and values of lines. 但仍然不知道如何用[i] [j]用线和线的值填充向量。

lines3[j].push_back(val); lines3 [j] .push_back(val); //THIS LINE compiles but fails runtime with out of bounds error (of course) //此行可编译,但运行时失败,出现超出范围的错误(当然)

I post this for the future, so maybe others won't stuck like me with this. 我将其发布以备将来使用,所以也许其他人不会像我这样坚持下去。 As Dan Mašek mentioned in the comments I needed to replace 正如DanMašek在评论中提到的,我需要替换

lines3[i][j].push_back(val);

with

lines3[i].push_back(val);

And now it works fine! 现在工作正常! Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM