zoukankan      html  css  js  c++  java
  • 判断一个点是否在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>里面就可

    以,当然了,对于其它复杂轮廓来说,可能会需要更多操作。


    那么,最后合成的程序为:

    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/





  • 相关阅读:
    Python的subprocess子进程和管道进行交互
    python UnicodeEncodeError: 'ascii' codec can't encode characters 解决办法
    如何创建,增加swap
    nginx.conf配置
    15个极好的Linux find命令示例(二)
    15个实用的Linux find命令示例(一)
    vmware 虚拟机 mount :no medium found解决方法
    服务器上的iptables
    源码编译安装MySQL
    ORACLE参数文件
  • 原文地址:https://www.cnblogs.com/jsxyhelu/p/9741492.html
Copyright © 2011-2022 走看看