rows, cols = np.nonzero(img)
skel_coords = []
for r, c in zip(rows, cols):
    col_neigh, row_neigh = np.meshgrid(np.array([c - 1, c, c + 1]), np.
        array([r - 1, r, r + 1]))
col_neigh = col_neigh.astype('int')
row_neigh = row_neigh.astype('int')
pix_neighbourhood = img[row_neigh, col_neigh].ravel() != 0
if np.sum(pix_neighbourhood) == 2:
    skel_coords.append((r, c))
How can I find endpoints of binary skeleton image in OpenCV?