0

我在 这里找到了用于查找骨架化图像连接点的代码。

代码没有找到所有的交汇点,而只找到了主要的交汇点。

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

在我的示例图片中: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)
4

1 回答 1

0

根据我的理解,骨架中的连接点是具有超过 2 个黑色像素作为邻居的像素(如果背景 = 白色,骨架 = 黑色)。

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

于 2013-12-13T15:58:54.453 回答