当前位置:网站首页>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.28Images :

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
边栏推荐
- Granularity of blocking of concurrency control
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- 航海日答题小程序之航海知识竞赛初赛
- Sub total of Pico development
- opencv 判断点在多边形内外
- Microservice link risk analysis
- Two stage locking protocol for concurrency control
- Basic grammar of interview (Part 1)
- Blocking of concurrency control
猜你喜欢

Implementation technology of recovery

50. Pow(x, n). O(logN) Sol

Leetcode simple question: find the nearest point with the same X or Y coordinate

How can Bluetooth in notebook computer be used to connect headphones

Qtquick3d real time reflection

Promql demo service

Oracle triggers

Bitbucket installation configuration

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

Overview of database recovery
随机推荐
Database recovery strategy
点到直线的距离直线的交点及夹角
Cobaltstrike builds an intranet tunnel
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Technology cloud report: how many hurdles does the computing power network need to cross?
Qtquick3d real time reflection
Concurrency control of performance tuning methodology
Type of fault
Microservice link risk analysis
Text组件新增内容通过tag_config设置前景色、背景色
Meituan dynamic thread pool practice ideas, open source
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
航海日答题小程序之航海知识竞赛初赛
"Chris Richardson microservices series" uses API gateway to build microservices
[Yugong series] go teaching course in July 2022 004 go code Notes
Draw a red lantern with MATLAB
Create a virtual machine on VMware (system not installed)
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析