判断一个点是否在RotatedRect中

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/

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值