Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
PCSI 1 Correction TD Informatique 2 : 2018/2019
Traitement d'images
Exercice 1. Composantes
1
1.
def image_rouge(im) :
t=np.array(im)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
for k in range(1,3) :
t[i][j][k]=0
return Image.fromarray(t)
lena_rouge=image_rouge(lena)
lena_rouge.show()
2
2.
def image_verte(im) :
t=np.array(im)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
for k in [0,2] :
t[i][j][k]=0
return Image.fromarray(t)
lena_verte=image_verte(lena)
lena_verte.show()
3
3.
def image_bleue(im) :
t=np.array(im)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
for k in [0,1] :
t[i][j][k]=0
return Image.fromarray(t)
lena_bleue=image_bleue(lena)
lena_bleue.show()
4
Exercice 2. Négatif
5
def negatif(im) :
t=np.array(im)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
for k in range(0,3) :
t[i][j][k]=255-t[i][j][k]
return Image.fromarray(t)
lena_negatif=negatif(lena)
lena_negatif.show()
6
Exercice 3. Cadre
7
1.
def cadre_noir(im,ep) :
t=np.array(im)
h,l,r=t.shape
for i in range(ep) : #boucle pour la ligne du haut
for j in range(l) :
for k in range(3) :
t[i][j][k]=0
for i in range(h-ep,h) : #boucle pour la ligne du bas
for j in range(l) :
for k in range(3) :
t[i][j][k]=0
for i in range(h) : #boucle pour la colone de gauche
for j in range(ep) :
for k in range(3) :
t[i][j][k]=0
for i in range(h) : #boucle pour la colonne de droite
for j in range(l-ep,l) :
for k in range(3) :
t[i][j][k]=0
return Image.fromarray(t)
cadre_noir(lena,20).show()
8
2.
def rajout_cadre(im,ep) :
t=np.array(im)
h,l,r=t.shape
r=np.zeros((h+2*ep,l+2*ep,3),dtype="uint8")
for i in range(h+2*ep) :
for j in range(l+2*ep) :
for k in range(3) :
if i>ep and i<h+ep and j>ep and j<l+ep :
r[i][j][k]=t[i-ep][j-ep][k]
return Image.fromarray(r)
rajout_cadre(lena,20).show()
9
Exercice 4. Niveaux de gris
10
def niveau_gris(im) :
t=np.array(im)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
x=int(0.299*t[i][j][0]+0.587*t[i][j][1]+0.114*t[i][j][2])
(t[i][j][0],t[i][j][1],t[i][j][2])=(x,x,x)
return Image.fromarray(t)
niveau_gris(lena).show()
11
Exercice 5. Pixellisation
12
def pixellisation(im,n) :
t=np.array(im)
h,l,r=t.shape
for i in range(h//n) :
for j in range(l//n) :
r_moy=0
v_moy=0
b_moy=0
for k in range(n) :
for p in range(n) :
r_moy+=t[n*i+k][n*j+p][0]
v_moy+=t[n*i+k][n*j+p][1]
b_moy+=t[n*i+k][n*j+p][2]
r_moy=r_moy//n**2
v_moy=v_moy//n**2
b_moy=b_moy//n**2
for k in range(n) :
for p in range(n) :
t[n*i+k][n*j+p][0]=r_moy
t[n*i+k][n*j+p][1]=v_moy
t[n*i+k][n*j+p][2]=b_moy
return Image.fromarray(t)
pixellisation(lena,8).show() 13
Exercice 6. E�et popart (facultatif)
14
1.
def �ltre(t,p) :
L=np.copy(t)
h,l,r=t.shape
for i in range(h) :
for j in range(l) :
for k in range(r) :
if p[k]==0 :
L[i][j][k]=0
return L
15
2.
def popart(im) :
t=np.array(im)
tab_hg=�ltre(t,[255,255,0])
tab_hd=�ltre(t,[255,0,0])
tab_bg=�ltre(t,[255,0,255])
tab_bd=�ltre(t,[0,255,0])
tab_h=np.concatenate((tab_hg,tab_hd),axis=1)
tab_b=np.concatenate((tab_bg,tab_bd),axis=1)
tab=np.concatenate((tab_h,tab_b),axis=0)
return Image.fromarray(tab)
popart(lena).show()
16