21 lines
718 B
Python
21 lines
718 B
Python
import cv2
|
|
import numpy as np
|
|
'''
|
|
两个区域间最短距离
|
|
https://blog.csdn.net/weixin_42515093/article/details/112713403
|
|
'''
|
|
# 1、在二值图像中获取轮廓
|
|
import cv2
|
|
img = cv2.imread('demo/73.png')
|
|
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
|
|
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
# 2、绘制轮廓
|
|
# To draw all the contours in an image:
|
|
cv2.drawContours(img, contours, -1, (0,255,0), 3)
|
|
# To draw an individual contour, say 4th contour:
|
|
cv2.drawContours(img, contours, 3, (0,255,0), 3)
|
|
# But most of the time, below method will be useful:
|
|
cnt = contours[4]
|
|
cv2.drawContours(img, [cnt], 0, (0,255,0), 3) |