如何在scikit图像骨架化后检测点?

时间:2013-05-22 12:34:05

标签: python image-processing scikit-image

我有二元骨架化图像,我使用python库mahotas来提取端点和分支点。

我不喜欢mahotas 功能(有太多小分支)所以我选择了scikit-image skeletonize 功能。

现在麻烦开始了:在某些图像中,它不会提取分支点。 为什么呢?

Scikit image function接受布尔值和整数值(mahotas使用布尔值)。

image with no branched point detected

iamge with branched point detected

from skimage import morphology
import mahotas as mh
import pymorph as pm
import numpy as np
import cv2
from matplotlib import pyplot as plt
import scipy

def branchedPoints(skel):
    branch1=np.array([[2, 1, 2], [1, 1, 1], [2, 2, 2]])
    branch2=np.array([[1, 2, 1], [2, 1, 2], [1, 2, 1]])
    branch3=np.array([[1, 2, 1], [2, 1, 2], [1, 2, 2]])
    branch4=np.array([[2, 1, 2], [1, 1, 2], [2, 1, 2]])
    branch5=np.array([[1, 2, 2], [2, 1, 2], [1, 2, 1]])
    branch6=np.array([[2, 2, 2], [1, 1, 1], [2, 1, 2]])
    branch7=np.array([[2, 2, 1], [2, 1, 2], [1, 2, 1]])
    branch8=np.array([[2, 1, 2], [2, 1, 1], [2, 1, 2]])
    branch9=np.array([[1, 2, 1], [2, 1, 2], [2, 2, 1]])
    br1=mh.morph.hitmiss(skel,branch1)
    br2=mh.morph.hitmiss(skel,branch2)
    br3=mh.morph.hitmiss(skel,branch3)
    br4=mh.morph.hitmiss(skel,branch4)
    br5=mh.morph.hitmiss(skel,branch5)
    br6=mh.morph.hitmiss(skel,branch6)
    br7=mh.morph.hitmiss(skel,branch7)
    br8=mh.morph.hitmiss(skel,branch8)
    br9=mh.morph.hitmiss(skel,branch9)
    return br1+br2+br3+br4+br5+br6+br7+br8+br9

def endPoints(skel):
    endpoint1=np.array([[0, 0, 0],[0, 1, 0],[2, 1, 2]])
    endpoint2=np.array([[0, 0, 0],[0, 1, 2],[0, 2, 1]])
    endpoint3=np.array([[0, 0, 2],[0, 1, 1],[0, 0, 2]])
    endpoint4=np.array([[0, 2, 1],[0, 1, 2],[0, 0, 0]])
    endpoint5=np.array([[2, 1, 2],[0, 1, 0],[0, 0, 0]])
    endpoint6=np.array([[1, 2, 0],[2, 1, 0],[0, 0, 0]])
    endpoint7=np.array([[2, 0, 0],[1, 1, 0],[2, 0, 0]])
    endpoint8=np.array([[0, 0, 0],[2, 1, 0],[1, 2, 0]])
    ep1=mh.morph.hitmiss(skel,endpoint1)
    ep2=mh.morph.hitmiss(skel,endpoint2)
    ep3=mh.morph.hitmiss(skel,endpoint3)
    ep4=mh.morph.hitmiss(skel,endpoint4)
    ep5=mh.morph.hitmiss(skel,endpoint5)
    ep6=mh.morph.hitmiss(skel,endpoint6)
    ep7=mh.morph.hitmiss(skel,endpoint7)
    ep8=mh.morph.hitmiss(skel,endpoint8)
    ep = ep1+ep2+ep3+ep4+ep5+ep6+ep7+ep8
    return ep

def pruning(skeleton, size):

    for i in range(1, size):
        endpoints = endPoints(skeleton)
        endpoints = np.logical_not(endpoints)
        skeleton = np.logical_and(skeleton,endpoints)
    return skeleton


path = 'signs/a (0).jpg'

fork = mh.imread(path)  
imgbnbin = fork[:,:,0]

shape = list(fork.shape)

w =  (shape[0]/100 )*3.5

#structuring elements
disk7 = pm.sedisk(w)
disk5 = pm.sedisk(3)
disk3 = pm.sedisk(0.5)      

bfork = imgbnbin < 150

plt.gray()
plt.subplot(121)
plt.title("after binarization")
plt.imshow(bfork)
plt.show()

bfork = mh.morph.dilate(bfork, disk7)

bfork = np.array(bfork, dtype=np.bool)
#Pota cose inutili

bfork = mh.morph.close(bfork, disk3)

# Skeleton+Pruning
#skelFk = mh.thin(bfork)
bfork = np.array(bfork, dtype=np.uint8)
skelFk = morphology.skeletonize(bfork)
skelFk = np.array(skelFk, dtype=np.bool)

skelF_pruned = pruning(skelFk, 15)

#end points (Ep) from skeletons
## fork (Fk) sign
print("skelfpruned before of endpoint")
print(skelF_pruned[70])
EpFk = endPoints(skelF_pruned)
EpFk_p = endPoints(skelF_pruned)
EpFk_p = mh.dilate(EpFk_p,disk5)

# counting end-points
lab_Ek, n1 = mh.label(EpFk)
lab_Ekp, n1p = mh.label(EpFk_p)

print n1, ' end points on fork like image'
print n1p, ' end points on fork like image, after pruning'

#branched points
## Merge too close points by morphological dilation
### Fork
BpFk = branchedPoints(skelF_pruned)# br points on Fork

print("branched point")
bcols,brows = np.where(BpFk)
print(brows)
print(bcols)

print("end point")
ecols,erows = np.where(EpFk)
print(erows)

img = skelF_pruned

# viene dilatato per mostrare meglio il punto di giunzione
BpFk = mh.morph.dilate(BpFk, disk5)

## count branched points
lab_Ek, n3 = mh.label(BpFk)

print n3, ' branched points on fork like image'

#Overlay:
#Display end-points in blue
#        branched-points in yellow
#        skeleton in red 
display_Fk = pm.overlay(imgbnbin, red = img>0, blue = EpFk_p>0, yellow = BpFk>0)     
plt.gray()
plt.subplot(121)
plt.imshow(imgbnbin)
plt.imshow(display_Fk)
plt.show()

1 个答案:

答案 0 :(得分:2)

我认为问题可能是实际上有18种分支类型,而您的代码只搜索9。

尝试用以下代码替换分支结构:

xbranch0  = np.array([[1,0,1],[0,1,0],[1,0,1]])
xbranch1 = np.array([[0,1,0],[1,1,1],[0,1,0]])
tbranch0 = np.array([[0,0,0],[1,1,1],[0,1,0]])
tbranch1 = np.flipud(tbranch0)
tbranch2 = tbranch0.T
tbranch3 = np.fliplr(tbranch2)
tbranch4 = np.array([[1,0,1],[0,1,0],[1,0,0]])
tbranch5 = np.flipud(tbranch4)
tbranch6 = np.fliplr(tbranch4)
tbranch7 = np.fliplr(tbranch5)  
ybranch0 = np.array([[1,0,1],[0,1,0],[2,1,2]])
ybranch1 = np.flipud(ybranch0)
ybranch2 = ybranch0.T
ybranch3 = np.fliplr(ybranch2)
ybranch4 = np.array([[0,1,2],[1,1,2],[2,2,1]])
ybranch5 = np.flipud(ybranch4)
ybranch6 = np.fliplr(ybranch4)
ybranch7 = np.fliplr(ybranch5)

这些分支结构配置为防止任何单个分支点的多次命中。如果这不是问题,您可以随时在数组结构中将'0'替换为'2'。