当前位置:网站首页>Opencv judgment points are inside and outside the polygon
Opencv judgment points are inside and outside the polygon
2022-07-05 22:19:00 【alex1801】
be based on Python and OpenCV Draw a polygon , And judge whether a point is in the polygon .
1、cv2.pointPolygonTest() function
Function definition :cv2.pointPolygonTest(contour, pt, measureDist)
The functionality : Find the shortest distance between the point and the contour in the image . The distance it returns is negative when the point is outside the contour , When the point is positive in the contour , If it is 0.
among ,contour Is a contour polygon ;pt It's a coordinate point ;measureDist, if True, Is to find the signed distance ; if False, I will find out whether it is included , Outside , Or contour ( Corresponding return +1, -1, 0).
The test case :
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) # External point , Red
pt1 = (166, 157) # Points on the outline
pt2 = (260, 170) # Inside point
img = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
img = cv2.circle(img, pt0, 2, (0, 0, 255), 2) # Red
img = cv2.circle(img, pt1, 2, (0, 255, 0), 2) # green
img = cv2.circle(img, pt2, 2, (255, 0, 0), 2) # Blue
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)
test result :
dst: -58.52 2.82 44.28
Images :
2、cv2.polylines() function
Function definition :img = cv2.polylines(img, pts, isClosed, color, thickness, lineType, shift)
among :
pts It is a matrix composed of polygon fixed points , Connect the incoming points once .
isClosed - Indicates whether the drawn polygon is closed . If closed (True), Then the last vertex of each curve is connected to the first vertex , If not closed (False), Then the polygon is not closed .
Sample code :
import cv2
import numpy as np
# Pure white image
img = np.zeros((500, 500, 3), np.uint8)
img[:] = [255, 255, 255]
# Four vertex coordinates
pts = np.array([[100, 100], [200, 20], [370, 6], [450, 200]], np.int32)
# Number of vertices :4, Matrix becomes 4*1*2 dimension
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)
result :
3、 Key code
# Four vertex coordinates
pts = np.array([[100, 100], [200, 20], [370, 6], [450, 200]], np.int32)
# Number of vertices :4, Matrix becomes 4*1*2 dimension
pts = pts.reshape((-1, 1, 2)) # (4,1,2)
# Draw a polygon
img = cv2.polylines(img, [pts], isClosed=True, color=(0, 0, 255), thickness=3)
# Test the distance between the point and the polygon
dist = cv2.pointPolygonTest(pts, (307, 100), True)
Extended reading :
1、opencv python outline / Convex defect /PointPolygonTest/ Shape match
opencv python outline / Convex defect /PointPolygonTest/ Shape match - SegmentFault Think no
Shape match :
OpenCV With a function cv2.matchShapes(), It allows us to compare two shapes or two outlines , And return a measure that shows similarity . The lower the result , The better the match . It is based on hu-moment Calculated by value .
ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
2、 Better than opencv special column
边栏推荐
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- 如何開發引入小程序插件
- Common interview questions of redis factory
- Search: Future Vision (moving sword)
- C language knowledge points link
- Basic grammar of interview (Part 1)
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- New 3D particle function in QT 6.3
- A substring with a length of three and different characters in the leetcode simple question
- Go语言学习教程(十五)
猜你喜欢
Qtquick3d real time reflection
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Overview of concurrency control
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
极狐公司官方澄清声明
Unique occurrence times of leetcode simple questions
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
Decorator learning 01
Database recovery strategy
随机推荐
Pl/sql basic case
How to view Apache log4j 2 remote code execution vulnerability?
2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
Performance testing of software testing
Search: Future Vision (moving sword)
Database tuning solution
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Livelocks and deadlocks of concurrency control
Kubernetes Administrator certification (CKA) exam notes (IV)
What about data leakage? " Watson k'7 moves to eliminate security threats
Practice: fabric user certificate revocation operation process
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
Evolution of large website architecture and knowledge system
Oracle hint understanding
微服务链路风险分析
70. Climbing Stairs. Sol
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Damn, window in ie open()
Index optimization of performance tuning methodology
IIC bus realizes client device