1.接口说明
#include "opencv2/imgproc/imgproc.hpp"
double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)
参数说明:
contour – 输入区域的各个点,围成一个区域。
pt – 需要判断的像素点。
measureDist – 如果为false的话,返回值:在区域外部为-1,在区域内为1,在区域上为0。
如果为ture,则计算实际的像素符号距离,在区域外的点像素距离为负值,在区域内的点像素距离为正值。
2.使用例程
模拟点是否在矩形框内,不规则多边形一样通用。
std::vector<cv::Point> contourAreas;
cv::Point point[4];
point[0].x = 10;point[0].y = 10;
point[1].x = 10;point[1].y = 1000;
point[2].x = 1000;point[2].y = 1000;
point[3].x = 1000;point[3].y = 10;
for( int j = 0; j < 4; j++ )
contourAreas.push_back( point[j] );
cv::Point2f pointCenter(50,10);
double ret1 = cv::pointPolygonTest(contourAreas, pointCenter, false);
if(ret1 > 0)
cout<<"in area"<<endl;
double ret2 = cv::pointPolygonTest(contourAreas, pointCenter, true);
cout<<"distance is:"<<ret2 <<endl;