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
|
def filtrer1(filtreA,matB):
"""filtreA est une matrice carree
filtrage sur une couleur seulement
"""
nA=filtreA.shape[0] # taille de filtreA
nb_ligneB=matB.shape[0] # nb de lignes de matB
nb_colonneB=matB.shape[1]
C=matB.copy() # on copie matB
bordure=nA//2 ## taille du bord
for i in range(bordure,nb_ligneB-bordure): # on eclut les lignes du bord
for j in range(bordure,nb_colonneB-bordure): # idem pour les colonnes
Bij=matB[i-bordure:i+bordure+1,j-bordure:j+bordure+1] # on crée la matrice autour de l'élément central bij
C[i,j]=np.sum(np.dot(Bij,filtreA)) # on calcule le terme Cij
return(C)
def filtrer(filtreA,matB):
nA=filtreA.shape[0]
nb_ligneB=matB.shape[0]
nb_colonneB=matB.shape[1]
C=np.zeros((nb_ligneB,nb_colonneB,3),dtype='uint8')
for k in range(3):
Ck=filtrer1(filtreA,matB[:,:,k])
for i in range(nb_ligneB):
for j in range(nb_colonneB):
C[i,j,k]=Ck[i,j]
return(C)
def matriceFlouGaussien(taille,sigma):
"""
taille : taille de la matrice impaire
sigma : ecart-type
retourne un niveau gaussien
"""
mat=np.zeros([taille,taille])
taille=taille//2
for x in range(-taille,taille+1):
for y in range(-taille,taille+1):
mat[taille+x,taille+y]=np.exp(-(x**2+y**2)/(2*sigma**2))
s=np.sum(mat)
return((1/s)*mat)
def FloutageGaussien(tabPix,taille,sigma):
filtreA=matriceFlouGaussien(taille,sigma)
return(filtrer(filtreA,tabPix))
picture_floute=FloutageGaussien(Picture,5,0.9)
plt.figure(3)
plt.imshow(picture_floute)
plt.show() |
Partager