如何在骨骼化图像中找到连接点?

2024-03-29 01:54:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我发现了here一个用于查找骨架化图像的连接点的代码。在

代码并没有找到所有的交叉点,只有主要的连接点。在

如何调整此代码以查找骨架图像中的所有连接点?在

在我的示例图像中:https://docs.google.com/file/d/0ByS6Z5WRz-h2U3NBWWZ6V3FqeUk/edit?usp=sharing我想在骨架中找到至少6个连接!在

我的代码:

在找到连接点之前,我准备好图像:我转换成二进制,填充空洞并进行骨架化。在

import numpy as np
from numpy import array
import cv2
import pymorph as m
import mahotas
import scipy.ndimage.morphology as mo


def skeletonize(img):
    h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]]) 
    m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]]) 
    h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]]) 
    m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]])    
    hit_list = [] 
    miss_list = []
    for k in range(4): 
        hit_list.append(np.rot90(h1, k))
        hit_list.append(np.rot90(h2, k))
        miss_list.append(np.rot90(m1, k))
        miss_list.append(np.rot90(m2, k))    
    img = img.copy()
    while True:
        last = img
        for hit, miss in zip(hit_list, miss_list): 
            hm = mo.binary_hit_or_miss(img, hit, miss) 
            img = np.logical_and(img, np.logical_not(hm)) 
        if np.all(img == last):  
            break
    return img

#input image
nomeimg = 'DUPLInuova/fionda 3/c (3).jpg'
img = cv2.imread(nomeimg)
gray = cv2.imread(nomeimg,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(6,6)) #con 4,4 si vede tutta la stella e riconosce piccoli oggetti
graydilate = cv2.erode(gray, element) #imgbnbin

ret,thresh = cv2.threshold(graydilate,127,255,cv2.THRESH_BINARY_INV) 
imgbnbin = thresh

cv2.imshow('binaria',imgbnbin)
cv2.waitKey()





#finding a unique contour
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(len(contours))

Areacontours = list()
calcarea = 0.0
unicocnt = 0.0
for i in range (0, len(contours)):
    area = cv2.contourArea(contours[i])
    if (area > 90 ):  #con 90 trova i segni e togli puntini
        if (calcarea<area):
            calcarea = area
            unicocnt = contours[i]

cnt = unicocnt            
print(len(cnt))
cv2.drawContours(thresh,contours,-1,(0,255,0),3)

#fill holes
des = imgbnbin
cv2.drawContours(des,[unicocnt],0,255,-1)

gray = cv2.bitwise_not(des)

cv2.imshow('gray tappabuchi grAY',gray)
cv2.waitKey()


kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
res = cv2.morphologyEx(gray,cv2.MORPH_OPEN,kernel)

cv2.imshow('res tappabuchi',res)
cv2.waitKey()

rest = cv2.bitwise_not(res)

print(rest)


cv2.imshow('rest tappabuchi 2',rest)
cv2.waitKey()

skel = skeletonize(rest)
skel = skel.astype(np.uint8)*255
print(skel)
cv2.imshow('skel',skel)
cv2.waitKey(0)

imgbnbin = m.binary(skel)

#FINDING junction and PRUNING
print("mohatas imgbnbin")
print(imgbnbin)

b2 = m.thin(imgbnbin)
b3 = m.thin(b2, m.endpoints('homotopic'), 15) # prune small branches, may need tuning

outputimage = m.overlay(imgbnbin, b3)
mahotas.imsave('outputs.png', outputimage)

# structuring elements to search for 3-connected pixels
seA1 = array([[False,  True, False],
       [False,  True, False],
       [ True, False,  True]], dtype=bool)

seB1 = array([[False, False, False],
       [ True, False,  True],
       [False,  True, False]], dtype=bool)

seA2 = array([[False,  True, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)

seB2 = array([[ True, False,  True],
       [False, False, False],
       [False,  True, False]], dtype=bool)

# hit or miss templates from these SEs
hmt1 = m.se2hmt(seA1, seB1)
hmt2 = m.se2hmt(seA2, seB2)

# locate 3-connected regions
b4 = m.union(m.supcanon(b3, hmt1), m.supcanon(b3, hmt2))

# dilate to merge nearby hits
b5 = m.dilate(b4, m.sedisk(10))

# locate centroids
b6 = m.blob(m.label(b5), 'centroid')

outputimage = m.overlay(imgbnbin, m.dilate(b6,m.sedisk(5)))
mahotas.imsave('output.png', outputimage)

Tags: importfalsetrueimgnparraycv2list
1条回答
网友
1楼 · 发布于 2024-03-29 01:54:24

根据我的理解,骨架中的一个连接点是一个像素,它有两个以上的黑色像素作为邻居(如果背景=白色,骨架=黑色)。在

因此,一个简单的解决方案是迭代黑色像素,并计算当前像素周围的黑色像素数。需要密码吗?可以修改用于骨架化的代码来执行此操作。在

相关问题 更多 >