张素太瘦太极端 - Python实现

时间:2016-06-02 21:58:22

标签: python algorithm image-processing

我正在尝试细化指纹图像。 下面你可以看到结果和输入重叠:

enter image description here

正如您所看到的,很多线都被打破了。我的算法的实现是问题吗? (对不起法语变量名称):

如果像素“足够黑”,则输出1

def couleur (valeur):
    if valeur > 200:
        couleur = 0
    else:
        couleur = 1
    return couleur

这会将图像转换为矩阵:

def image_vers_matrice (chemin):
    matrice = []
    image = Image.open(chemin)
    pixels = image.load()
    largeur, hauteur = image.size
    for i in range(hauteur):
        matrice.append([])
        for j in range(largeur):
            matrice[-1].append(couleur(pixels[j, i][0]))
    return matrice

这会将矩阵变成图像:

def matrice_vers_image (matrice):
    image = Image.new('RGB', (len(matrice[0]), len(matrice)), 'white')
    draw = ImageDraw.Draw(image)
    for i in range(len(matrice)):
        for j in range(len(matrice[0])):
            if(matrice[i][j] == 1):
                draw.point([(j, i)], fill=0)
    return image

这在算法中使用(点周围从白色到黑色的过渡次数):

def nombre_transitions (image, i, j):
    transitions = 0
    if image[i - 1][j] == 0 and image[i - 1][j + 1] == 1:
        transitions += 1
    if image[i - 1][j + 1] == 0 and image[i][j + 1] == 1:
        transitions += 1
    if image[i + 1][j + 1] == 0 and image[i + 1][j] == 1:
        transitions += 1
    if image[i + 1][j] == 0 and image[i + 1][j - 1] == 1:
        transitions += 1
    if image[i + 1][j - 1] == 0 and image[i][j - 1] == 1:
        transitions += 1
    if image[i][j - 1] == 0 and image[i - 1][j - 1] == 1:
        transitions += 1
    if image[i - 1][j - 1] == 0 and image[i - 1][j] == 1:
        transitions += 1
    return transitions

也用于算法(邻居数):

def nombre_voisins_8 (image, i, j):
    #on suppose que le point est noir et n'est pas sur un bord
    voisins = 0
    for k in range(i - 1, i + 2):
        for l in range(j - 1, j + 2):
            voisins += image[k][l]
    return voisins - 1

实际的事情:

def zhang_suen (argument_image):
    image = []
    for i in range(len(argument_image)):
        image.append([x for x in argument_image[i]])
    continuer = True
    while continuer:
        continuer = False
        a_supprimer = []
        for i in range(1, len(image) - 1):
            for j in range(1, len(image[0]) - 1):
                condition = True
                condition = condition and image[i][j] == 1
                condition = condition and 2 <= nombre_voisins_8(image, i, j) <= 6
                condition = condition and nombre_transitions(image, i, j) == 1
                condition = condition and (image[i - 1][j] == 0 or image[i][j + 1] == 0 or image[i + 1][j] == 0)
                condition = condition and (image[i][j - 1] == 0 or image[i][j + 1] == 0 or image[i + 1][j] == 0)
                if condition:
                    a_supprimer.append((i, j))
                    continuer = True
        for x in a_supprimer:
            i, j = x
            image[i][j] = 0
        for i in range(1, len(image) - 1):
            for j in range(1, len(image[0]) - 1):
                condition = True
                condition = condition and image[i][j] == 1
                condition = condition and 2 <= nombre_voisins_8(image, i, j) <= 6
                condition = condition and nombre_transitions(image, i, j) == 1
                condition = condition and (image[i - 1][j] == 0 or image[i][j + 1] == 0 or image[i][j - 1] == 0)
                condition = condition and (image[i][j - 1] == 0 or image[i - 1][j] == 0 or image[i + 1][j] == 0)
                if condition:
                    a_supprimer.append((i, j))
                    continuer = True
        for x in a_supprimer:
            i, j = x
            image[i][j] = 0
        a_supprimer = []
    return image

这样可以保存细化的图像(上图中的绿色图像)

matrice_vers_image(zhang_suen(image_vers_matrice('test2.jpg'))).save('test3.png')

0 个答案:

没有答案