-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpythonconv_image.py
More file actions
69 lines (49 loc) · 1.83 KB
/
pythonconv_image.py
File metadata and controls
69 lines (49 loc) · 1.83 KB
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
import numpy as np
import matplotlib.pyplot as plt
# Partie A - Importer une image comme un tableau
import imageio.v3 as imageio
f = imageio.imread('image_avant.png')
# img02 : flou
# img05 : piqué
# img04 : contour
# img10 : emboss
# img09 : verticale
# Partie B - Motif de convolution
# Identité
g = np.array([[0,0,0],[0,1,0],[0,0,0]]) # identité
# Flou
# g = 1/9*np.array([[1,1,1],[1,1,1],[1,1,1]]) # flou 3x3
# g = 1/25*np.ones((5,5)) # flou 5x5
# g = 1/16*np.array([[1,2,1],[2,4,2],[1,2,1]]) # flou gaussien 3x3
# g = 1/256*np.array([[1,2,4,6,41],[4,16,24,16,4],[6,24,36,24,6],[4,16,24,16,4],[1,2,4,6,41]]) # flou gaussien 5x5
# Piqué
# g = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]]) # piqué
# Contour
# g = 1/9*np.array([[1,0,-1],[0,0,0],[-1,0,1]]) # contour
# g = np.array([[0,1,0],[1,-4,1],[0,1,0]]) # contour
# g = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]) # contour
g = np.array([[-2,-1,0],[-1,1,1],[0,1,2]]) # 'emboss'
# g = np.array([[-1,2,-1],[-1,2,-1],[-1,2,-1]]) # verticale
# g = np.array([[-1,2,-1],[0,0,0],[-1,2,-1]]) # verticale (Sobel)
# g = np.array([[-1,-1,-1],[2,2,2],[-1,-1,-1]]) # horizontale
# g = np.array([[-1,-1,2],[-1,2,-1],[2,-1,-1]]) # 45 degrés
# Partie C - Calcul de la convolution
from scipy import signal
h = signal.convolve2d(f, g, boundary='fill', mode='same')
# Partie D - Affichage des images avant/après
fig = plt.figure(figsize = (10,5))
ax = plt.subplot(1,2,1)
ax.set_title("Image originale")
# fig, ax = subplots(figsize=(18, 2))
ax.imshow(f, cmap='gray')
# plt.xlim(0, 200)
# plt.ylim(0, 200)
ax = plt.subplot(1,2,2)
ax.set_title("Image sortie")
plt.imshow(h, cmap='gray')
plt.tight_layout()
plt.show()
# Partie E - Sauvegarde de l'image
h = np.clip(h,0,255) # limite les valeurs entre 0 et 255
h = h.astype(np.uint8) # conversion en entier
imageio.imwrite('image_apres.png', h)