Search code examples
pythonopencvimage-processingcontourline-intersection

Finding intersections of a skeletonised image in python opencv


I have a skeletonised image (shown below).

Skeleton Image

I would like to get the intersections of the lines. I have tried the following method below, skeleton is a openCV image and the algorithm returns a list of coordinates:

def getSkeletonIntersection(skeleton):
    image = skeleton.copy();
    image = image/255;
    intersections = list();
    for y in range(1,len(image)-1):
        for x in range(1,len(image[y])-1):
            if image[y][x] == 1:
                neighbourCount = 0;
                neighbours = neighbourCoords(x,y);
                for n in neighbours:
                    if (image[n[1]][n[0]] == 1):
                        neighbourCount += 1;
                if(neighbourCount > 2):
                    print(neighbourCount,x,y);
                    intersections.append((x,y));
    return intersections;

It finds the coordinates of white pixels where there are more than two adjacent pixels. I thought that this would only return corners but it does not - it returns many more points.

Skeleton with marked coordinates

This is the output with the points it detects marked on the image. This is because it detects some of the examples shown below that are not intersections.

0 0 0    1 1 0    0 1 1
1 1 1    0 1 0    1 1 0
0 0 1    0 0 1    0 0 0

And many more examples. Is there another method I should look at to detect intersections. All input and ideas appreciated, thanks.


Solution

  • I am not sure about OpenCV features, but you should maybe try using Hit and Miss morphology which is described here.

    Read up on Line Junctions and see the 12 templates you need to test for:

    enter image description here