如何elminate的"增加了一倍"在C向量元素+ [英] how to elminate the "doubled" elements of a vector in c++

查看:200
本文介绍了如何elminate的"增加了一倍"在C向量元素+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 HoughLines 来检测线路在一个框架,将保存在 CV ::矢量&lt线路信息;简历:: Vec2f&GT; 这是我处理的二维数组,我感兴趣的是第二个,它的线的角度,我想只保留有一个角度差大于<$ C线$ C> 1.5弧度为这里我我所做的:

I'm using the HoughLinesto detect line in a frame, the lines information are saved in a cv::vector<cv::Vec2f> which I handle as two dimensional array, I'm interested in the second one , it the angle of the line, I want to keep only the lines that have a angle difference greater than 1.5 rad for that here I what I did :

.............................
cv::vector<cv::Vec2f> lineQ;
..............................

  // ordring the vector based on the angle value in rad 
for ( int i = 0 ; i< lineQ.size()-1; i++){
                for(int j= i+1;j<lineQ.size();j++){
                        if(lineQ[i][1] > lineQ[j][1]){
                            tmp = lineQ[i];
                            lineQ[i] = lineQ[j];
                            lineQ[j] = tmp;
                        }           
                }
            }

现在我想基于角度彼此之间的矢量元素比较

now I want to compare the vector elements between each other based on the angle

cv::vector<cv::Vec2f> line;
for ( int i = 0 ; i< lineQ.size()-1; i++){
                for ( int j= i+1; j<lineQ.size(); j++){
            if(fabs(lineQ[i][1] - lineQ[j][1])>1.5){
                        line.push_back(lineQ[i]);

                        }
                }
            }

这适用于2线,但是当我到了3白衣假设1.3rad作为一个角的大小  比2,我虽然用删除但是这个改变我向量的大小!

this works for 2 lines but when I got 3 whit let's say 1.3rad as an angle the size of line is than 2. I though to use erase but this change the size of my vector !

推荐答案

一种选择是提供一个软等于,以的std :: unique_copy

One option is to supply a soft "equals" to std::unique_copy:

std::unique_copy(lineQ.begin(), lineQ.end(), std::back_inserter(line),
                 [](const cv::Vec2f & a, const cv::Vec2f & b) {
                     return b[1] - a[1] <= 1.5;
                 });

旁注:你也可以避免写自己的排序的努力(冒泡排序仅仅是最糟糕的选择。),并使用标准库。像这样的东西应该工作:

Sidenote: You can also avoid the effort of writing your own sort (Bubble sort is just about the worst choice.) and use the standard library. Something like this ought to work:

std::sort(lineQ.begin(), lineQ.end(),
          [](const cv::Vec2f & a, const cv::Vec2f & b) {
              return a[1] < b[1];
          })).

(以上code假设C ++ 11,我们大多数人都通过了。如果你被困在一个早期版本,你可以写几个函数子类来代替。)

(The above code assumes C++11, which most of us have by now. If you're stuck on an earlier version, you can write a couple of functor classes instead.)

这篇关于如何elminate的&QUOT;增加了一倍&QUOT;在C向量元素+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆