Squelettisation - Algorithme de Hilditch
Bonjour !
Actuellement, je travaille sur la reconnaissance de formes avec mon partenaire de binôme. Lui s'occupe de l'extraction des caractéristiques, et moi du prétraitement d'image.
Je bloque à la squelettisation où on avons choisi d'utiliser l'algorithme de Hilditch. Rien à faire, mon programme est syntaxiquement correct, mais il me retourne l'image de départ sans la modifier...
J'ai repris l'algorithme exact décrit par Hilditch (que vous pouvez trouver ici si vous le souhaitez. Un peu compliqué, ma foi) et non un algorithme reformulé (et quelque peu incompréhensible) sur Internet.
Je travaille avec Python 3.3.1 en utilisant la bibliothèque PIL.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| from PIL import Image
# CONVERTIR LISTE EN INTEGER
def t2i(tup):
liste = list(tup) # Eventuelle conversion en liste si l'entree est un tuple
n0 = int(liste[0]/255)
n1 = int(liste[1]/255)
n2 = int(liste[2]/255)
return n0+n1+n2
n = (0,0,0)
b = (255,255,255)
# SQUELETTISATION
def squelettiser(img):
imgskel=Image.new(img.mode, img.size)
largeur=img.size[0]-2
hauteur=img.size[1]-2
for y in range(2,hauteur):
for x in range(2,largeur):
# VOISINAGE DE P0
P0 = img.getpixel((x,y))
P1 = img.getpixel((x,y+1))
P2 = img.getpixel((x-1,y+1))
P3 = img.getpixel((x-1,y))
P4 = img.getpixel((x+1,y-1))
P5 = img.getpixel((x,y-1))
P6 = img.getpixel((x+1,y-1))
P7 = img.getpixel((x+1,y))
P8 = img.getpixel((x+1,y+1))
# NOMBRE DE VOISINS BLANCS DE P0
N_P0 = t2i(P1) + t2i(P2) + t2i(P3) + t2i(P4) + t2i(P5) + t2i(P6) + t2i(P7) + t2i(P8)
H_P0 = 0 # Nombre de transitions du blanc vers le noir du pixel P0
H1_P0 = 0 # Voir les dernières conditions
H3_P0 = 0
N_P0 = 0
condition1 = 0
condition2 = 0
condition3 = 0
condition4 = 0
condition5 = 0
# NOMBRE DE TRANSITIONS BLANC => NOIR
if P0 == n:
if P8 == b and P1 == n:
H_P0 = H_P0 + 1
if P1 == b and P2 == n:
H_P0 = H_P0 + 1
if P2 == b and P3 == n:
H_P0 = H_P0 + 1
if P3 == b and P4 == n:
H_P0 = H_P0 + 1
if P4 == b and P5 == n:
H_P0 = H_P0 + 1
if P5 == b and P6 == n:
H_P0 = H_P0 + 1
if P6 == b and P7 == n:
H_P0 = H_P0 + 1
if P7 == b and P8 == n:
H_P0 = H_P0 + 1
# CONDITION 1
if 1 <= N_P0 <= 6:
condition1 = 1
# CONDITION 2
if condition1 and H_P0 == 1:
condition2 = 1
# CONDITION 3
if condition2 and t2i(P1) + t2i(P3) + t2i(P5) + t2i(P7) >= 1:
condition3 = 1
# CONDITION 4
if P1 == b:
H1_P0 = 1
if condition3 and (H1_P0 == 1 or P3 == n):
condition4 = 1
# CONDITION 5
if P3 == b:
H3_P0 = 1
if condition4 and (H3_P1 == 1 or P1 == n):
condition5 = 1
# EFFACER PIXEL P0
if condition5:
img.putpixel((x,y),b)
else:
img.putpixel((x,y),n)
else:
img.putpixel((x,y),b)
data=list(img.getdata())
imgskel.putdata(data)
imgskel.save("Image_Squelette.png") |
Je pense que le problème vient de la façon dont j'efface le pixel P0 car quoi que je mette après le if, j'obtiens toujours l'image de départ. Et je ne comprends pas ce qui cloche... Quelqu'un pourrait-il m'aider ?
Merci d'avance :).
PS : J'ignore si j'ai posté dans la bonne rubrique et je vous prie de m'excuser si ce n'était pas le cas.