OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 2900|回复: 0

判断一个点是否在RotatedRect中

[复制链接]
发表于 2018-10-6 17:13:11 | 显示全部楼层 |阅读模式
openCV函数pointPolygonTest():
C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)
用于判断一个点是否在轮廓中
当measureDist设置为true时,若返回值为正,表示点在轮廓内部,返回值为负,表示在轮廓外部,返回值为0,表示在轮廓上。
当measureDist设置为false时,若返回值为+1,表示点在轮廓内部,返回值为-1,表示在轮廓外部,返回值为0,表示在轮廓上。
例:

……/// 查找轮廓std::vector<std::vector<cv:oint> >
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,就需要首先将[size=10.270270347595215px]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;
}



需要注意的是,在这里[size=11.891891479492188px]indicator [size=11.891891479492188px]>[size=11.891891479492188px]=[size=11.891891479492188px] [size=11.891891479492188px]0,如果你是判断是否在轮廓上,要修改为[size=11.891891479492188px]indicator =[size=11.891891479492188px]=[size=11.891891479492188px] [size=11.891891479492188px]0
[size=11.891891479492188px]

[size=11.891891479492188px]参考:[size=11.891891479492188px]http://answers.opencv.org/question/30330/check-if-a-point-is-inside-a-rotatedrect/
[size=10.270270347595215px]



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-4-23 19:30 , Processed in 0.010147 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表