问题
I use Python OpenCV to skeletonize an image like this:
and I want to find the branch point of the skeleton
I have no idea how to do it. Any thoughts?
回答1:
The main idea here is to look in the neighborhood. You can use a 8-conected neighborhood for every pixels ([1 1 1; 1 1 1; 1 1 1], and the center is the pixel for which the neighborhood being explored!).
At every branch point, the degree of the pixel will be > 2, while regular pixels will have a degree of 2 (i.e., connected to 2 pixels in their neighborhood).
In your case your objective is finding a 'crossing point', where the degree is > 3.
回答2:
You can use the library Skan to find the branch points.
from skan import skeleton_to_csgraph
from skimage import io, morphology
# loading image
image = io.imread('https://i.stack.imgur.com/FpSt9.jpg')
# make image binary
image_binary = image >= 200
# skeletonize
sk = morphology.skeletonize(image_binary).astype(bool)
# receive a degree matrix
_, _, degrees = skeleton_to_csgraph(sk)
# consider all values larger than two as intersection
intersection_matrix = degrees > 2
The result here is not a single pixel as multiple branches meet at one point. One could determine this center region with regionprops and then find the centroid if one needs exactly one pixel.
回答3:
Output (overlaid on input image):
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Find branch point in example image.
"""
import numpy as np
import matplotlib.pyplot as plt
from mahotas.morph import hitmiss as hit_or_miss
from skimage.morphology import medial_axis as skeletonize
# from scipy.ndimage import binary_hit_or_miss as hit_or_miss
def find_branch_points(skel):
X=[]
#cross X
X0 = np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]])
X1 = np.array([[1, 0, 1],
[0, 1, 0],
[1, 0, 1]])
X.append(X0)
X.append(X1)
#T like
T=[]
#T0 contains X0
T0=np.array([[2, 1, 2],
[1, 1, 1],
[2, 2, 2]])
T1=np.array([[1, 2, 1],
[2, 1, 2],
[1, 2, 2]]) # contains X1
T2=np.array([[2, 1, 2],
[1, 1, 2],
[2, 1, 2]])
T3=np.array([[1, 2, 2],
[2, 1, 2],
[1, 2, 1]])
T4=np.array([[2, 2, 2],
[1, 1, 1],
[2, 1, 2]])
T5=np.array([[2, 2, 1],
[2, 1, 2],
[1, 2, 1]])
T6=np.array([[2, 1, 2],
[2, 1, 1],
[2, 1, 2]])
T7=np.array([[1, 2, 1],
[2, 1, 2],
[2, 2, 1]])
T.append(T0)
T.append(T1)
T.append(T2)
T.append(T3)
T.append(T4)
T.append(T5)
T.append(T6)
T.append(T7)
#Y like
Y=[]
Y0=np.array([[1, 0, 1],
[0, 1, 0],
[2, 1, 2]])
Y1=np.array([[0, 1, 0],
[1, 1, 2],
[0, 2, 1]])
Y2=np.array([[1, 0, 2],
[0, 1, 1],
[1, 0, 2]])
Y2=np.array([[1, 0, 2],
[0, 1, 1],
[1, 0, 2]])
Y3=np.array([[0, 2, 1],
[1, 1, 2],
[0, 1, 0]])
Y4=np.array([[2, 1, 2],
[0, 1, 0],
[1, 0, 1]])
Y5=np.rot90(Y3)
Y6 = np.rot90(Y4)
Y7 = np.rot90(Y5)
Y.append(Y0)
Y.append(Y1)
Y.append(Y2)
Y.append(Y3)
Y.append(Y4)
Y.append(Y5)
Y.append(Y6)
Y.append(Y7)
bp = np.zeros(skel.shape, dtype=int)
for x in X:
bp = bp + hit_or_miss(skel,x)
for y in Y:
bp = bp + hit_or_miss(skel,y)
for t in T:
bp = bp + hit_or_miss(skel,t)
return bp
def find_end_points(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=hit_or_miss(skel,endpoint1)
ep2=hit_or_miss(skel,endpoint2)
ep3=hit_or_miss(skel,endpoint3)
ep4=hit_or_miss(skel,endpoint4)
ep5=hit_or_miss(skel,endpoint5)
ep6=hit_or_miss(skel,endpoint6)
ep7=hit_or_miss(skel,endpoint7)
ep8=hit_or_miss(skel,endpoint8)
ep = ep1+ep2+ep3+ep4+ep5+ep6+ep7+ep8
return ep
# --------------------------------------------------------------------------------
# script
# --------------------------------------------------------------------------------
img = plt.imread("test.jpg")
# for some reason (screenshot?), example image is not binary
binary = img >= 250
skeleton = skeletonize(binary)
branch_pts = find_branch_points(skeleton)
plt.imshow(binary + branch_pts, cmap='gray', interpolation='nearest'); plt.show()
来源:https://stackoverflow.com/questions/43037692/how-to-find-branch-point-from-binary-skeletonize-image