2016-02-26 33 views
0

我有一個向量,有很多NaN的x,y位置,我想刪除(做一些opencv工作)。我無法弄清楚如何使用remove_if來刪除NaN(當與擦除一起使用時)。我看過很多例子,如果vector是float或int而不是point2f。任何簡單的例子都會非常有幫助。謝謝。如何使用remove_if與矢量<point2f>

+1

加你嘗試了一些代碼示例。 –

+0

類似這樣的東西:vector myv; myv.erase(的remove_if(myv.begin,myv.end,isnan(MYV)),myv.end); – Clay

+0

我不是opencv的專家。我告訴將這些添加到您的問題和格式,以便其他人可以輕鬆找出您的問題。我實際上是從審查。 –

回答

1

您可以使用lambda函數,函子或函數指針。這是一個lambda函數的例子:

#include <opencv2/opencv.hpp> 
#include <algorithm> 
#include <iostream> 
#include <cmath> 

using namespace cv; 
using namespace std; 

int main(int argc, char ** argv) 
{ 
    vector<Point2f> pts{ Point2f(1.f, 2.f), Point2f(3.f, sqrt(-1.0f)), Point2f(2.f, 3.f) }; 

    cout << "Before" << endl; 
    for (const auto& p : pts) { 
     cout << p << " "; 
    } 
    cout << endl; 

    pts.erase(remove_if(pts.begin(), pts.end(), [](const Point2f& p) 
    { 
     // Check if a coordinate is NaN 
     return isnan(p.x) || isnan(p.y); 
    }), pts.end()); 

    cout << "After" << endl; 
    for (const auto& p : pts) { 
     cout << p << " "; 
    } 
    cout << endl; 

    return 0; 
} 

,將打印:

Before 
[1, 2] [3, -1.#IND] [2, 3] 
After 
[1, 2] [2, 3] 
+1

非常感謝三木!我是C++的新手,所以不知道它是如何工作的,但它確實如此。魔法!我今天將閱讀Lambda函數。乾杯。 – Clay

相關問題