《A fast parallel algorithm for thinning digital patterns》論文算法python代碼實現

論文地址:A fast parallel algorithm for thinning digital patterns
代碼:

def thinImage(src, maxIterations=-1):
    assert len(src.shape) == 2, 'please binarify pictures'
    img_height, img_width = src.shape
    dst = src.copy()
    count = 0
    while True:
        count +=1
        if maxIterations != -1 and count > maxIterations:
            break
        mFlag = []
        for i in range(img_height):
            for j in range(img_width):
                p1 = dst[i, j]
                if p1 != 1:
                    continue
                p4 = 0 if j == img_width-1 else dst[i, j+1]
                p8 = 0 if j == 0 else dst[i, j-1]
                p2 = 0 if i == 0 else dst[i-1, j]
                p3 = 0 if i == 0 or j == img_width-1 else dst[i-1, j+1]
                p9 = 0 if i == 0 or j == 0 else dst[i-1, j-1]
                p6 = 0 if i == img_height-1 else dst[i+1, j]
                p5 = 0 if i == img_height-1 or j == img_width-1 else dst[i+1, j+1]
                p7 = 0 if i == img_height-1 or j == 0 else dst[i+1, j-1]
                if p2+p3+p4+p5+p6+p7+p8+p9>=2 and p2+p3+p4+p5+p6+p7+p8+p9<=6:
                    ap = 0
                    if p2 == 0 and p3 ==1:
                        ap+=1
                    if p3 == 0 and p4 == 1:
                        ap+=1
                    if p4 ==0 and p5 == 1:
                        ap+=1
                    if p5 == 0 and p6 == 1:
                        ap+=1
                    if p6 == 0 and p7 == 1:
                        ap+=1
                    if p7 == 0 and p8 == 1:
                        ap+=1
                    if p8 == 0 and p9 == 1:
                        ap+=1
                    if p9 == 0 and p2 == 1:
                        ap+=1
                    if ap == 1 and p2*p4*p6 == 0 and p4*p6*p8 == 0:
                        mFlag.append([i, j])
        for flag in mFlag:
            dst[flag[0], flag[1]] = 0
        if len(mFlag) == 0:
            break
        else:
            mFlag.clear()
        for i in range(img_height):
            for j in range(img_width):
                p1 = dst[i, j]
                if p1 != 1:
                    continue
                p4 = 0 if j == img_width-1 else dst[i, j+1]
                p8 = 0 if j == 0 else dst[i, j-1]
                p2 = 0 if i == 0 else dst[i-1, j]
                p3 = 0 if i == 0 or j == img_width-1 else dst[i-1, j+1]
                p9 = 0 if i == 0 or j == 0 else dst[i-1, j-1]
                p6 = 0 if i == img_height-1 else dst[i+1, j]
                p5 = 0 if i == img_height-1 or j == img_width-1 else dst[i+1, j+1]
                p7 = 0 if i == img_height-1 or j == 0 else dst[i+1, j-1]
                if p2+p3+p4+p5+p6+p7+p8+p9>=2 and p2+p3+p4+p5+p6+p7+p8+p9<=6:
                    ap = 0
                    if p2 == 0 and p3 ==1:
                        ap+=1
                    if p3 == 0 and p4 == 1:
                        ap+=1
                    if p4 ==0 and p5 == 1:
                        ap+=1
                    if p5 == 0 and p6 == 1:
                        ap+=1
                    if p6 == 0 and p7 == 1:
                        ap+=1
                    if p7 == 0 and p8 == 1:
                        ap+=1
                    if p8 == 0 and p9 == 1:
                        ap+=1
                    if p9 == 0 and p2 == 1:
                        ap+=1
                    if ap == 1 and p2*p4*p8 == 0 and p2*p6*p8 == 0:
                        mFlag.append([i, j])
        for flag in mFlag:
            dst[flag[0], flag[1]] = 0
        if len(mFlag) == 0:
            break
        else:
            mFlag.clear()
    return dst

if __name__ == '__main__':
    startTime = time.time()
    files = ['C:/Users/tellw/Desktop/toSkeleton.png', 'C:/Users/tellw/Desktop/1.png']
    for file in files:
        img = cv2.imread(file, 0)
        _, img = cv2.threshold(img, 128, 1, cv2.THRESH_BINARY)
        img = thinImage(img)
        img[img==1] = 255
        cv2.imshow('%s after process'%file, img)
    print('running %f s'%(time.time()-startTime))
    cv2.waitKey()

運行結果:
在這裏插入圖片描述
參考鏈接:
圖像細化 A fast parallel algorithm for thinning digital patterns
基於zhang 的骨架提取

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章