当前位置:网站首页>opencv 判断点在多边形内外
opencv 判断点在多边形内外
2022-07-05 22:17:00 【alex1801】
基于Python 和 OpenCV 画出多边形,以及判断某个点是不是在多边形内。
1、cv2.pointPolygonTest() 函数
函数定义:cv2.pointPolygonTest(contour, pt, measureDist)
函数功能:找到图像里的点和轮廓之间的最短距离. 它返回的距离当点在轮廓外的时候是负值,当点在轮廓内是正值,如果在轮廓上是0。
其中,contour 为轮廓多边形;pt 为坐标点;measureDist, 若为True,是找带符号的距离;若为False,会找点是否在内,外,或轮廓上(相应返回+1, -1, 0)。
测试用例:
import cv2
mask = cv2.imread(r"mask.jpg", 0)
mask[mask > 100] = 255
mask[mask != 255] = 0
cnts, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("mask info:", mask.shape, len(cnts))
pt0 = (131, 104) # 外点, 红色
pt1 = (166, 157) # 轮廓上的点
pt2 = (260, 170) # 内点
img = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
img = cv2.circle(img, pt0, 2, (0, 0, 255), 2) # 红色
img = cv2.circle(img, pt1, 2, (0, 255, 0), 2) # 绿色
img = cv2.circle(img, pt2, 2, (255, 0, 0), 2) # 蓝色
dst0 = cv2.pointPolygonTest(cnts[0], pt0, 1)
dst1 = cv2.pointPolygonTest(cnts[0], pt1, 1)
dst2 = cv2.pointPolygonTest(cnts[0], pt2, 1)
print("dst:", dst0, dst1, dst2)
cv2.imwrite(r"ret.jpg", img)测试结果:
dst: -58.52 2.82 44.28图像:

2、cv2.polylines() 函数
函数定义:img = cv2.polylines(img, pts, isClosed, color, thickness, lineType, shift)
其中:
pts 是多边形定点构成的矩阵,将传入的点一次连接。
isClosed - 表示绘制的多边形是否闭合. 如闭合(True),则每个曲线的最后一个顶点到第一个顶点是连接的,若不闭合(False),则多边形就不闭合。
示例代码:
import cv2
import numpy as np
# 纯白图像
img = np.zeros((500, 500, 3), np.uint8)
img[:] = [255, 255, 255]
# 四个顶点坐标
pts = np.array([[100, 100], [200, 20], [370, 6], [450, 200]], np.int32)
# 顶点个数:4,矩阵变成4*1*2维
pts = pts.reshape((-1, 1, 2)) # (4,1,2)
cv2.polylines(img, [pts], isClosed=True, color=(0, 0, 255), thickness=3)
cv2.imwrite(r"a.jpg", img)结果:

3、关键代码
# 四个顶点坐标
pts = np.array([[100, 100], [200, 20], [370, 6], [450, 200]], np.int32)
# 顶点个数:4,矩阵变成4*1*2维
pts = pts.reshape((-1, 1, 2)) # (4,1,2)
# 画多边形
img = cv2.polylines(img, [pts], isClosed=True, color=(0, 0, 255), thickness=3)
# 测试点与多边形的距离
dist = cv2.pointPolygonTest(pts, (307, 100), True)扩展阅读:
1、opencv python 轮廓/凸缺陷/PointPolygonTest/形状匹配
opencv python 轮廓/凸缺陷/PointPolygonTest/形状匹配 - SegmentFault 思否
形状匹配:
OpenCV附带了一个函数cv2.matchShapes(),它使我们能够比较两个形状或两个轮廓,并返回一个显示相似性的度量。 结果越低,匹配就越好.它是根据hu-moment值计算的。
ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
2、比好好的opencv专栏
边栏推荐
- 如何開發引入小程序插件
- 微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)
- Oracle views the data size of a table
- 极狐公司官方澄清声明
- 了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
- Metaverse Ape获Negentropy Capital种子轮融资350万美元
- Practice: fabric user certificate revocation operation process
- MySQL actual combat 45 lecture learning (I)
- Performance monitoring of database tuning solutions
- boundary IoU 的计算方式
猜你喜欢

Granularity of blocking of concurrency control

2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*

Learning of mall permission module

Concurrency control of performance tuning methodology

MySQL actual combat 45 lecture learning (I)

database mirroring

Dbeaver executes multiple insert into error processing at the same time

A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition

科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖

Calculation method of boundary IOU
随机推荐
Search: Future Vision (moving sword)
Bitbucket installation configuration
The real situation of programmers
Leetcode simple question: find the nearest point with the same X or Y coordinate
Storage optimization of performance tuning methodology
Meituan dynamic thread pool practice ideas, open source
Common interview questions of redis factory
Shell script, awk condition judgment and logic comparison &||
科技云报道:算力网络,还需跨越几道坎?
A substring with a length of three and different characters in the leetcode simple question
Platform bus
Overview of concurrency control
Livelocks and deadlocks of concurrency control
Oracle hint understanding
Daily question brushing record (XIV)
Recovery technology with checkpoints
Talking about MySQL index
Go语言学习教程(十五)
Text组件新增内容通过tag_config设置前景色、背景色
微服务入门(RestTemplate、Eureka、Nacos、Feign、Gateway)