我有二进制骨架图像,我使用 python 库 mahotas 来提取端点和分支点。
我不喜欢 mahotas瘦函数(有太多的小分支),所以我选择了 scikit-image骨架化函数。
现在麻烦开始了:在某些图像中,它不再提取分支点。为什么?
Scikit 图像函数接受布尔值和整数值(mahotas 使用布尔值)。
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()