OpenCV|多边形点测试

1.学习内容 本节我们将学习OpenCV的pointPolygonTest()函数:

C++: double cv::pointPolygonTest (InputArray contour, Point2f pt, bool measureDist) Python: cv.pointPolygonTest(contour, pt, measureDist) ->retval

参数:
contour:输入轮廓。
pt:要测试的点。
measureDist:如果为真,该函数估计从点到最近的轮廓边缘的符号距离。否则,函数只检查点是否在轮廓线内。
该函数确定点是在轮廓线内、外,还是位于边缘(或与顶点重合)。它会相应地返回正(内部)、负(外部)或零(边缘)值。当measureDist=false时,返回值分别是**+1**,-1和0。否则,返回值是点和最近的轮廓边缘之间的带符号距离。
2.代码案例 2.1 Python代码
from __future__ import print_function from __future__ import division import cv2 as cv import numpy as np # Create an image r = 100 src = https://www.it610.com/article/np.zeros((4*r, 4*r), dtype=np.uint8) # 创建六边形的6个顶点 vert = [None]*6 vert[0] = (3*r//2, int(1.34*r)) vert[1] = (1*r, 2*r) vert[2] = (3*r//2, int(2.866*r)) vert[3] = (5*r//2, int(2.866*r)) vert[4] = (3*r, 2*r) vert[5] = (5*r//2, int(1.34*r)) # 根据六个顶点画6边形 for i in range(6): cv.line(src, vert[i],vert[(i+1)%6], ( 255 ), 3) # 获取六边形的轮廓 contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # 计算图上点到六边形的距离(带符号) raw_dist = np.empty(src.shape, dtype=np.float32) for i in range(src.shape[0]): for j in range(src.shape[1]): raw_dist[i,j] = cv.pointPolygonTest(contours[0], (j,i), True) # 查找带符号的最大最小值 minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist) minVal = abs(minVal) maxVal = abs(maxVal) # 用图形表示距离 drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8) for i in range(src.shape[0]): for j in range(src.shape[1]): if raw_dist[i,j] < 0: drawing[i,j,0] = 255 - abs(raw_dist[i,j]) * 255 / minVal elif raw_dist[i,j]> 0: drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal else: drawing[i,j,0] = 255 drawing[i,j,1] = 255 drawing[i,j,2] = 255 cv.circle(drawing,maxDistPt, int(maxVal),(255,255,255), 1, cv.LINE_8, 0) cv.imshow('Source', src) cv.imshow('Distance and inscribed circle', drawing) cv.waitKey()

2.2 C++代码
#include #include #include using namespace cv; using namespace std; int main( void ) { const int r = 100; Mat src = https://www.it610.com/article/Mat::zeros( Size( 4*r, 4*r ), CV_8U ); vector vert(6); vert[0] = Point( 3*r/2, static_cast(1.34*r) ); vert[1] = Point( 1*r, 2*r ); vert[2] = Point( 3*r/2, static_cast(2.866*r) ); vert[3] = Point( 5*r/2, static_cast(2.866*r) ); vert[4] = Point( 3*r, 2*r ); vert[5] = Point( 5*r/2, static_cast(1.34*r) ); for( int i = 0; i < 6; i++ ) { line( src, vert[i],vert[(i+1)%6], Scalar( 255 ), 3 ); } vector contours; findContours( src, contours, RETR_TREE, CHAIN_APPROX_SIMPLE); Mat raw_dist( src.size(), CV_32F ); for( int i = 0; i < src.rows; i++ ) { for( int j = 0; j < src.cols; j++ ) { raw_dist.at(i,j) = (float)pointPolygonTest( contours[0], Point2f((float)j, (float)i), true ); } } double minVal, maxVal; Point maxDistPt; // inscribed circle center minMaxLoc(raw_dist, &minVal, &maxVal, NULL, &maxDistPt); minVal = abs(minVal); maxVal = abs(maxVal); Mat drawing = Mat::zeros( src.size(), CV_8UC3 ); for( int i = 0; i < src.rows; i++ ) { for( int j = 0; j < src.cols; j++ ) { if( raw_dist.at(i,j) < 0 ) { drawing.at(i,j)[0] = (uchar)(255 - abs(raw_dist.at(i,j)) * 255 / minVal); } else if( raw_dist.at(i,j) > 0 ) { drawing.at(i,j)[2] = (uchar)(255 - raw_dist.at(i,j) * 255 / maxVal); } else { drawing.at(i,j)[0] = 255; drawing.at(i,j)[1] = 255; drawing.at(i,j)[2] = 255; } } } circle(drawing, maxDistPt, (int)maxVal, Scalar(255,255,255)); imshow( "Source", src ); imshow( "Distance and inscribed circle", drawing ); waitKey(); return 0; }

OpenCV|多边形点测试
文章图片

参考目录 【OpenCV|多边形点测试】https://docs.opencv.org/4.x/dc/d48/tutorial_point_polygon_test.html

    推荐阅读