import cv2
import numpy as np

def skeleton(img, need_threshold=False):
    size = np.size(img)
    skel = np.zeros(img.shape,np.uint8)
    if need_threshold: ret,img = cv2.threshold(img,127,255,0)
    element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))

    done = False
    while( not done):
        eroded = cv2.erode(img,element)
        temp = cv2.dilate(eroded,element)
        temp = cv2.subtract(img,temp)
        skel = cv2.bitwise_or(skel,temp)
        img = eroded.copy()
    
        zeros = size - cv2.countNonZero(img)
        if zeros==size:
            done = True

    return skel

if __name__ == "__main__":
    img = cv2.imread('closeup_corner.png',0)
    skel = skeleton(img)
    cv2.imshow("corner_skel",skel)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    cv2.imwrite('skel.png', skel)
