V
V
VictoriaKas2022-03-13 17:17:46
Python
VictoriaKas, 2022-03-13 17:17:46

How to find branch and intersection points of a binary skeletonized image?

Hello! I have the following code which looks for feature points in a binary skeletonized image. I need to find end points, branch points and intersection points separately and display their coordinates as (x, y, point type). For example, (147, 45, 3), where 3 is the number of adjacent pixels (branch point).

import cv2 as cv
import numpy as np


def extraction(img):
    # Find row and column locations that are non-zero
    (rows, cols) = np.nonzero(img)

    # Initialize empty list of co-ordinates
    skel_coords = []

    # For each non-zero pixel
    for (r, c) in zip(rows, cols):

        # Extract an 8-connected neighbourhood
        (col_neigh, row_neigh) = np.meshgrid(np.array([c - 1, c, c + 1]), np.array([r - 1, r, r + 1]))

        # Cast to int to index into image
        col_neigh = col_neigh.astype('int')
        row_neigh = row_neigh.astype('int')

        # Convert into a single 1D array and check for non-zero locations
        pix_neighbourhood = img[row_neigh, col_neigh].ravel() != 0

        # If the number of non-zero locations, add this to our list of co-ordinates
        if np.sum(pix_neighbourhood) == 2:
            skel_coords.append((c, r, 1))
        elif np.sum(pix_neighbourhood) == 4:
            skel_coords.append((c, r, 3))
        elif np.sum(pix_neighbourhood) == 5:
            skel_coords.append((c, r, 4))

    return skel_coords


img = cv.imread('abc.png', 0)
coord = extraction(img)
for element in coord:
    print(element)


The code correctly finds the number of neighboring pixels, but they are not branching and crossing points. You can see it in the picture below (the found point is marked in gray):
622df45ac607f353214246.png
Enlarged image of a 3x3 pixel matrix (below, two white pixels are in a row):
622df8373c67e085106948.png
I need to find points of the following kind for branch points (so that neighboring pixels alternate):
kernel_linejunctions.gifkernel_linejunctions3.gif

Does anyone have then ideas how to implement it? I would be very grateful for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
U235U235, 2022-03-13
@U235U235

If you already have a skeleton, then the simplest thing is to take the average over the 3x3 neighborhood, and then select branches or end points with a threshold.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question