您现在的位置是:首页 > 文章详情

判断一个点是否在RotatedRect中

日期:2018-10-03点击:628

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;
}
……

    但是这个函数是用来处理“轮廓”,也就是点的集合的;对于这里RotatedRect,它本身只是一个OpenCV的数据结构,所以我们如果想使用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/



来自为知笔记(Wiz)


目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
原文链接:https://yq.aliyun.com/articles/649366
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章