openCV函数pointPolygonTest():
C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)
用于判断一个点是否在轮廓中
当measureDist设置为true时,若返回值为正,表示点在轮廓内部,返回值为负,表示在轮廓外部,返回值为0,表示在轮廓上。
当measureDist设置为false时,若返回值为+1,表示点在轮廓内部,返回值为-1,表示在轮廓外部,返回值为0,表示在轮廓上。
例:
/// 查找轮廓std::vector<std::vector<cv::Point> >
contours;
cv
:
:Mat src;
//src为输入图像
cv
:
:findContours( src, contours, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE,Point(
0,
0));
//判断p1(x,y)是否在轮廓内
cv
:
:Point p1(x,y);
if (pointPolygonTest(Contours[j],cv
:
:Point(x1,y1),
false)
==
1)
{
cout
<<p1
<<
"在轮廓内"
<<endl;
}
……
pointPolygonTest,就需要首先将
RotatedRect转换为轮廓。对于RotatedRect,其实转换很简单,直接将它的四个角的坐标塞到一个Vector<point>里面就可
以,当然了,对于其它复杂轮廓来说,可能会需要更多操作。
可以参考https://stackoverflow.com/questions/8777603/what-is-the-simplest-way-to-convert-array-to-vector/8777619#8777619
那么,最后合成的程序为:
bool DoesRectangleContainPoint(RotatedRect rectangle, Point2f point) {
//Get the corner points.
Point2f corners[
4];
rectangle.points(corners);
//Convert the point array to a vector.
//https://stackoverflow.com/a/8777619/1997617
Point2f
* lastItemPointer
= (corners
+
sizeof corners
/
sizeof corners[
0]);
vector
<Point2f
> contour(corners, lastItemPointer);
//Check if the point is within the rectangle.
double indicator
= pointPolygonTest(contour, point,
false);
bool rectangleContainsPoint
= (indicator
>
=
0);
return rectangleContainsPoint;
}
需要注意的是,在这里 indicator > = 0,如果你是判断是否在轮廓上,要修改为 indicator = = 0
http://answers.opencv.org/question/30330/check-if-a-point-is-inside-a-rotatedrect/