-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask4.py
More file actions
61 lines (49 loc) · 2.01 KB
/
Copy pathtask4.py
File metadata and controls
61 lines (49 loc) · 2.01 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
# task4_cavo - visualizzazione con CNR e salvataggio PDF
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Caricamento immagine
img = np.loadtxt(r"C:/Users/user/Desktop/Programming/es8_Medipix/task4_immagine_cavo/cavo_rame.txt")
# Coordinate e dimensioni rettangoli (modificabili)
x_det, y_det, w_det, h_det = 175, 143, 20, 9
x_det2, y_det2, w_det2, h_det2 = 82, 160, 10, 13
x_bg, y_bg, w_bg, h_bg = 5, 185, 50, 50
# Estrazione delle regioni
roi_det = img[y_det:y_det + h_det, x_det:x_det + w_det]
roi_det2 = img[y_det2:y_det2 + h_det2, x_det2:x_det2 + w_det2]
roi_bg = img[y_bg:y_bg + h_bg, x_bg:x_bg + w_bg]
# Calcolo delle medie e del CNR
nd = np.mean(roi_det)
nd2 = np.mean(roi_det2)
nb = np.mean(roi_bg)
sigma = np.sqrt(nb)
cnr = abs((nd - nb) / sigma)
cnr2 = abs((nd2 - nb) / sigma)
# Stampa dei risultati
print(f"Media dettaglio (nd): {nd:.2f}; {nd2:.2f}")
print(f"Media background (nb): {nb:.2f}")
print(f"Deviazione std stimata (sqrt(nb)): {sigma:.2f}")
print(f"CNR: {cnr:.2f}; {cnr2:.2f}")
# Visualizzazione e salvataggio
fig, ax = plt.subplots(figsize=(8, 6))
cax = ax.imshow(img, cmap='gray', vmin=10, vmax=100)
# Titoli e etichette
ax.set_title("Radiografia del cavo USB - MinipixEDU", fontsize=14)
ax.set_xlabel("Pixel X", fontsize=12)
ax.set_ylabel("Pixel Y", fontsize=12)
# Aggiunta barra colori
cbar = fig.colorbar(cax, ax=ax)
cbar.set_label("Intensità (scala di grigi)", fontsize=12)
# Disegno rettangoli
rect1 = Rectangle((x_det, y_det), w_det, h_det, linewidth=2, edgecolor='red', facecolor='none', label='Dettaglio 1')
rect3 = Rectangle((x_det2, y_det2), w_det2, h_det2, linewidth=2, edgecolor='red', facecolor='none', label='Dettaglio 2')
rect2 = Rectangle((x_bg, y_bg), w_bg, h_bg, linewidth=2, edgecolor='blue', facecolor='none', label='Background')
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)
# Legenda e layout
ax.legend(loc='upper right', fontsize=10)
plt.tight_layout()
# Salvataggio a PDF
plt.savefig("radiografia_cavo_minipix.pdf", format='pdf')
plt.show()