Pieter
Pieter

Reputation: 32775

Intersecting points with a polygon in OpenCV

My inputs

I have a vector<Point2f> that contains the contours of a polygon. I also have a list of points that need to be intersected with this polygon.

The problem

I want to calculate how much of these points intersect with the polygon. I want to repeat this calculation on a number of polygons to see which one contains the highest number of points.

Does OpenCV implement such intersection functionality of its own or will I need to implement an intersection function myself? I'm worried that if I try to implement it myself, the result will be unnecessarily slow. If OpenCV can't do it, are there other free graphics libraries that can perform this task?

Upvotes: 4

Views: 2958

Answers (1)

btown
btown

Reputation: 2281

pointPolygonTest does exactly what you're looking for, and it's pretty well optimized. The parameter is a Mat which you can make with the constructor that takes your vector of points.

The function determines whether the point is inside a contour, outside, or lies on an edge (or coincides with a vertex). It returns positive (inside), negative (outside) or zero (on an edge) value, correspondingly. When measureDist=false , the return value is +1, -1 and 0, respectively. Otherwise, the return value it is a signed distance between the point and the nearest contour edge.

Your problem seems easily parallelizable, though, i.e. each batch of candidate polygons could run on a different thread, so I'd definitely look into that if you're concerned about performance.

Upvotes: 3

Related Questions