用一条线链接骨架的两个附近端点

时间:2019-04-22 11:03:31

标签: python opencv image-processing

我想通过在点之间的距离最短的点之间画一条线来链接图像的两个接近的端点。

green lines, with endpoints marked in yellow, and some nearby yellow dots linked by manually drawn line

我尝试了连接散点的方法,但不适用于我的代码。我已经使用Shi-Tomasi拐角检测来绘制拐角点。

import cv2
import numpy as np
from matplotlib import pyplot as plt
import cv2 as cv
from scipy.spatial import distance

img = cv2.imread('edge.png', cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape
canny = cv2.Canny(img, 50, 240)
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(canny,kernel,iterations = 1)

size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
skel = cv2.bitwise_or(skel,dilation)
corners = cv2.goodFeaturesToTrack(skel,30,0.01,11)
corners = np.int0(corners)
for i in corners:
   x,y = i.ravel()
   cv2.circle(img,(x,y),3,255,-1)

print(corners)

plt.imshow(img),plt.show()

cv2.imshow('Original', dilation)
cv2.imshow('canny', canny)

我有图像的端点。我只想用一条线链接最近的端点。如上图所示,我手动进行了尝试。

1 个答案:

答案 0 :(得分:0)

没关系的家伙我明白了!

我将点数组转换为列表,并使用欧氏距离函数找到了最小距离并画了一条线