I'm looking for an alternative version for the std::set_intersection function but for std::vector<cv::Point> vectors.
I try to compare two std::vector<cv::Point> vectors with different sizes. Those two contain coordinate lists. The main task of the intersection-like Method should now be to detect common pairs and safe them to a third std::vector<cv::Point> via push_back()
I searched for a function like std::set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));
Any ideas how to solve this?
As already mentioned by @BoBTFish and @NaCl, you need to use a custom comparator, and apply set_intersection on sorted vectors.
Since you need to call the comparator three times, it's useful to use a function instead of a lambda expression.
#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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With