Skip to content

Instantly share code, notes, and snippets.

@jamjar919
Created July 22, 2017 12:14
Show Gist options
  • Save jamjar919/05a76cf21035cef3cc86ac1979588d6d to your computer and use it in GitHub Desktop.
Save jamjar919/05a76cf21035cef3cc86ac1979588d6d to your computer and use it in GitHub Desktop.
Skeleton intersection code for
def getSkeletonIntersection(skeleton):
""" Given a skeletonised image, it will give the coordinates of the intersections of the skeleton.
Keyword arguments:
skeleton -- the skeletonised image to detect the intersections of
Returns:
List of 2-tuples (x,y) containing the intersection coordinates
"""
# A biiiiiig list of valid intersections 2 3 4
# These are in the format shown to the right 1 C 5
# 8 7 6
validIntersection = [[0,1,0,1,0,0,1,0],[0,0,1,0,1,0,0,1],[1,0,0,1,0,1,0,0],
[0,1,0,0,1,0,1,0],[0,0,1,0,0,1,0,1],[1,0,0,1,0,0,1,0],
[0,1,0,0,1,0,0,1],[1,0,1,0,0,1,0,0],[0,1,0,0,0,1,0,1],
[0,1,0,1,0,0,0,1],[0,1,0,1,0,1,0,0],[0,0,0,1,0,1,0,1],
[1,0,1,0,0,0,1,0],[1,0,1,0,1,0,0,0],[0,0,1,0,1,0,1,0],
[1,0,0,0,1,0,1,0],[1,0,0,1,1,1,0,0],[0,0,1,0,0,1,1,1],
[1,1,0,0,1,0,0,1],[0,1,1,1,0,0,1,0],[1,0,1,1,0,0,1,0],
[1,0,1,0,0,1,1,0],[1,0,1,1,0,1,1,0],[0,1,1,0,1,0,1,1],
[1,1,0,1,1,0,1,0],[1,1,0,0,1,0,1,0],[0,1,1,0,1,0,1,0],
[0,0,1,0,1,0,1,1],[1,0,0,1,1,0,1,0],[1,0,1,0,1,1,0,1],
[1,0,1,0,1,1,0,0],[1,0,1,0,1,0,0,1],[0,1,0,0,1,0,1,1],
[0,1,1,0,1,0,0,1],[1,1,0,1,0,0,1,0],[0,1,0,1,1,0,1,0],
[0,0,1,0,1,1,0,1],[1,0,1,0,0,1,0,1],[1,0,0,1,0,1,1,0],
[1,0,1,1,0,1,0,0]];
image = skeleton.copy();
image = image/255;
intersections = list();
for x in range(1,len(image)-1):
for y in range(1,len(image[x])-1):
# If we have a white pixel
if image[x][y] == 1:
neighbours = zs.neighbours(x,y,image);
valid = True;
if neighbours in validIntersection:
intersections.append((y,x));
# Filter intersections to make sure we don't count them twice or ones that are very close together
for point1 in intersections:
for point2 in intersections:
if (((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2) < 10**2) and (point1 != point2):
intersections.remove(point2);
# Remove duplicates
intersections = list(set(intersections));
return intersections;
@JD-Hell
Copy link

JD-Hell commented Sep 5, 2017

what is zs here on line 34 ??

@alwansm
Copy link

alwansm commented Feb 9, 2018

Hello @JD-Hell did you know what is zs?

@my-mia
Copy link

my-mia commented Sep 20, 2018

I also want to know what zs is in line 34.

@lilhead3
Copy link

what is zs here on line 34 ??

@jamjar919
Copy link
Author

Apologies - zs is a library I wrote to get neighbours of a pixel. I'll see if I can find the code. I had no idea people needed this still!

@jamjar919
Copy link
Author

jamjar919 commented Nov 4, 2018

def neighbours(x,y,image):
    """Return 8-neighbours of image point P1(x,y), in a clockwise order"""
    img = image
    x_1, y_1, x1, y1 = x-1, y-1, x+1, y+1;
    return [ img[x_1][y], img[x_1][y1], img[x][y1], img[x1][y1], img[x1][y], img[x1][y_1], img[x][y_1], img[x_1][y_1] ]   

Use this to get the neighbours in a clockwise order.

@Piyalcse
Copy link

Hi,
I'm very new to python. I need to use the function getSkeletonIntersection(skeleton) for junction points. But when i add neighbours(x,y,image) in the file like
https://stackoverflow.com/questions/41705405/finding-intersections-of-a-skeletonised-image-in-python-opencv/41708407#41708407
as stated here- it shows UnboundLocalError: local variable referenced before assignment

when I add neighbours(x,y,image) inside of getSkeletonIntersection(skeleton) it shows
TypeError: 'list' object is not callable while trying to access a list

Could you please help here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment