문제

I create skeleton images from binary images, like that

I detect end points of that skeletons with mahotas python library but it returns me the full image array with 1 value for end point and zero the others. I prefer to detect end points' coordinates. How can i get them?

My Code that compute endpoints:

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]])

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, 2], [0, 2, 1]])
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]])

jpg = 'skel.jpg'
skel = cv2.imread(jpg, 0)

sk = pymorph.binary(skel)

complete_path = 'skel.jpg'
print pymorph.gray(sk).dtype


br1=mah.morph.hitmiss(sk,branch1)
br2=mah.morph.hitmiss(sk,branch2)
br3=mah.morph.hitmiss(sk,branch3)
br4=mah.morph.hitmiss(sk,branch4)
br5=mah.morph.hitmiss(sk,branch5)
br6=mah.morph.hitmiss(sk,branch6)
br7=mah.morph.hitmiss(sk,branch7)
br8=mah.morph.hitmiss(sk,branch8)
br9=mah.morph.hitmiss(sk,branch9)

ep1=mah.morph.hitmiss(sk,endpoint1)
ep2=mah.morph.hitmiss(sk,endpoint2)
ep3=mah.morph.hitmiss(sk,endpoint3)
ep4=mah.morph.hitmiss(sk,endpoint4)
ep5=mah.morph.hitmiss(sk,endpoint5)
ep6=mah.morph.hitmiss(sk,endpoint6)
ep7=mah.morph.hitmiss(sk,endpoint7)
ep8=mah.morph.hitmiss(sk,endpoint8)

br=br1+br2+br3+br4+br5+br6+br7+br8+br9
ep=ep1+ep2+ep3+ep4+ep5+ep6+ep7+ep8

br and ep are the array with all branches and endpoint from i want get coordinates.

도움이 되었습니까?

해결책

So ep should just be a binary numpy array that is 1 only at your endpoint coordinates? In which case you can use either numpy.where or numpy.nonzero to get the indices of the nonzero values in ep:

pseudo_ep = (np.random.rand(512,512) > 0.9)
rows,cols = np.where(pseudo_ep)

These should correspond to the y,x coordinates of your endpoints

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top