将两个 std::vector<cv::P oint> 向量和安全公共点与第三个 std::vector<cv::P oint 进行比较>
Compare two std::vector<cv::Point> vectors and safe common points to a third std::vector<cv::Point>
我正在寻找std::set_intersection
函数的替代版本,但std::vector<cv::Point>
向量除外。
我尝试比较两个不同大小的std::vector<cv::Point>
矢量。这两个包含坐标列表。类似交集的方法现在的主要任务应该是检测公共对,并通过push_back()
将它们安全地发送给第三个std::vector<cv::Point>
我搜索了一个类似std::set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));
的函数
有什么办法解决这个问题吗?
正如@BoBTFish和@NaCl已经提到的,您需要使用自定义比较器,并在排序的向量上应用set_intersection
。
由于您需要调用比较器三次,因此使用函数而不是lambda表达式非常有用。
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
// Custom less comparator
bool lessPoints(const Point& lhs, const Point& rhs) {
return (lhs.x < rhs.x) || ((lhs.x == rhs.x) && (lhs.y < rhs.y));
}
vector<Point> intersection(vector<Point> v1, vector<Point> v2)
{
vector<Point> v3;
// Sort vectors
sort(v1.begin(), v1.end(), lessPoints);
sort(v2.begin(), v2.end(), lessPoints);
// Intersect
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3), lessPoints);
return v3;
}
int main()
{
// Your vectors
vector<Point> v1{ Point(2,3), Point(1,2), Point(5,5), Point(3,4) };
vector<Point> v2{ Point(2,1), Point(1,2), Point(3,4), Point(6,7), Point(0,3) };
// Find intersections
vector<Point> v3 = intersection(v1, v2);
// Print out vector content
std::copy(v3.begin(), v3.end(), std::ostream_iterator<Point>(std::cout, " "));
return 0;
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 呼叫运营商<<临时
- 概念中的cv限定符需要表达式参数列表
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 将CHW格式的浮点向量转换为cv::Mat
- 错误的cv::face FacemarkLBF实例化
- 如何检查给定的参数是否为 cv::noArray()?
- 开放 CV 中的动态内存分配,用于视频处理
- 如何在 opencv 中使用 cv::VideoCapture::waitAny()
- 错误:未定义对cv::cudacodec::createVideoReader的引用
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 将 cv::mat 转换为 QImage
- Inference pytorch C++ with alexnet and cv::imread image
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用
- cv::Normalise() 中的 L2_NORM 和 NORM_MINMAX 实现有什么区别?
- 将 cv::Mat 转换为 std::vector 的通用函数