forked from exo7math/deepmath-exo7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrelation_2d.py
More file actions
104 lines (74 loc) · 2.27 KB
/
Copy pathcorrelation_2d.py
File metadata and controls
104 lines (74 loc) · 2.27 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
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
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
##########################################
# Correlation 2d
def retourner(M):
M1 = M.flatten()
M2 = M1[::-1]
M3 = np.reshape(M2,np.shape(M))
return M3
def convolution(f,g):
return signal.convolve2d(f, g, mode='same')
def correlation(f,g):
h = convolution(f,retourner(g))
return h
##########################################
# Exemple
N = 4
A = np.arange(1,N**2+1).reshape((N,N))
N = 3
M = np.arange(1,N**2+1).reshape((N,N))
B = correlation(A,M)
print("=== Exemple ===")
print('A =', A)
print('M =', M)
print('B =', B)
##########################################
# Génération d'un signal émis par radar
# Cela peut être n'importe quoi
import imageio.v3 as imageio
icone = imageio.imread('smiley.png')
# Transformation en gris
epsilon = 0.2
icone = icone/128 -1 # valeurs entre -1 et 1
icone = epsilon*icone # diminution des écarts (=> gris)
Noutx, Nouty = icone.shape
print('Taille du smiley ', icone.shape)
# Version image pour affichage
icone_image = 128 + 128*icone
plt.imshow(icone_image, cmap='gray', vmin=0, vmax=255)
plt.tight_layout()
# plt.savefig('correlation2d-1.png')
plt.show()
##########################################
# Génération d'un signal bruité reçus par le radar
# Cela peut être n'importe quoi
# Bruit
Nin = 500
# bruit = np.random.random((Nin,Nin))-0.5)
bruit = np.random.normal(0,1,(Nin,Nin))
# Version image pour affichage
bruit_image = 128 + 128*bruit
bruit_image = np.clip(bruit_image,0,255)
plt.imshow(bruit_image, cmap='gray', vmin=0, vmax=255, interpolation="none")
plt.tight_layout()
# plt.savefig('correlation2d-2.png')
plt.show()
# Ajout du smiley
posx, posy = 150, 250
iconebruit = bruit + np.pad(icone,((posy,Nin-Nouty-posy),(posx,Nin-Noutx-posx)))
# Version image pour affichage
iconebruit_image = 128 + 128*iconebruit
iconebruit_image = np.clip(iconebruit_image,0,255)
plt.imshow(iconebruit_image, cmap='gray', vmin=0, vmax=255, interpolation="none")
plt.tight_layout()
# plt.savefig('correlation2d-3.png')
plt.show()
# Correlation
correl = correlation(iconebruit,icone)
print("Min/max :",np.min(correl),np.max(correl))
plt.imshow(correl, cmap='gray', interpolation="none")
plt.tight_layout()
# plt.savefig('correlation2d-4.png')
plt.show()