本文介绍了如何将 remove_if 与 vector<point2f> 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量,它有很多我想要删除的 x,y 位置的 NaN(做一些 opencv 工作).我不知道如何使用 remove_if 来删除 NaN(与擦除一起使用时).如果向量是 float 或 int 但不是 point2f,我已经看到了很多例子.任何简单的例子都会非常有帮助.谢谢.

I have a vector that has lots of NaN's for x,y positions that I want to remove(doing some opencv work). I cannot figure out how to use remove_if to remove the NaNs(when used in conjunction with erase). I've seen lots of examples if the vector is float or int but not point2f. Any simple examples would be very helpful. Thanks.

推荐答案

您可以使用 lambda 函数、函子或函数指针.这是一个带有 lambda 函数的示例:

You can use a lambda function, or a functor or a function pointer. This is an example with a lambda function:

#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]

这篇关于如何将 remove_if 与 vector&lt;point2f&gt; 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:49