2012-04-02 77 views
1

我有一個帶輪廓線的二進制圖像,需要淨化所有不必要像素的每個輪廓線,留下最小連接線。在二進制圖像中細化輪廓線

有人可以給我一個來源,代碼示例或有關此類問題的進一步信息,請在哪裏尋找幫助?

+0

我做了一個谷歌搜索「圖形算法細化線」,並得到了不少點擊。你試過這個嗎? – 2012-04-02 16:48:12

回答

1

二元圖像上的腐蝕和膨脹(反之亦然)的組合可以幫助擺脫鹽和胡椒一樣的噪音,使小線條完好無損。關鍵詞是「排序過濾器」和「形態過濾器」。

2

實際上有一種叫做張孫細化算法的算法。你可以在這裏找到它的代碼:http://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm

另外我寫了一個Python的向量化版本,比那個代碼快10倍。下面是代碼:

def neighbours_vec(image): 
    return image[2:,1:-1], image[2:,2:], image[1:-1,2:], image[:-2,2:], image[:-2,1:-1],  image[:-2,:-2], image[1:-1,:-2], image[2:,:-2] 

def transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9): 
    return ((P3-P2) > 0).astype(int) + ((P4-P3) > 0).astype(int) + \ 
    ((P5-P4) > 0).astype(int) + ((P6-P5) > 0).astype(int) + \ 
    ((P7-P6) > 0).astype(int) + ((P8-P7) > 0).astype(int) + \ 
    ((P9-P8) > 0).astype(int) + ((P2-P9) > 0).astype(int) 

def zhangSuen_vec(image, iterations): 
    for iter in range (1, iterations): 
     print iter 
     # step 1  
     P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image) 
     condition0 = image[1:-1,1:-1] 
     condition4 = P4*P6*P8 
     condition3 = P2*P4*P6 
     condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1 
     condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6) 
     cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1) 
     changing1 = numpy.where(cond == 1) 
     image[changing1[0]+1,changing1[1]+1] = 0 
     # step 2 
     P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image) 
     condition0 = image[1:-1,1:-1] 
     condition4 = P2*P6*P8 
     condition3 = P2*P4*P8 
     condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1 
     condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6) 
     cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1) 
     changing2 = numpy.where(cond == 1) 
     image[changing2[0]+1,changing2[1]+1] = 0 
    return image 
0

有PyPI上提供了一個叫做thinning,你可以只安裝點子。它實現了郭和霍爾針對顛簸陣列/ opencv灰度圖像的細化算法。