-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_compression.py
More file actions
220 lines (176 loc) · 7.04 KB
/
Copy pathvalidate_compression.py
File metadata and controls
220 lines (176 loc) · 7.04 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import numpy as np
from PIL import Image
import time
import csv
from compressor import compress_image
def calculate_psnr(original, compressed):
"""
Calcola il PSNR tra immagine originale e compressa.
PSNR = 10 * log10(255^2 / MSE)
"""
# Converte entrambe a float per il calcolo
orig_array = np.array(original, dtype=float)
comp_array = np.array(compressed, dtype=float)
# Calcola MSE
mse = np.mean((orig_array - comp_array) ** 2)
if mse == 0:
return float('inf')
psnr = 10 * np.log10(255**2 / mse)
return psnr
def run_compression_tests():
"""
Esegue i test di compressione su immagini reali con parametri variabili.
Calcola il tempo di esecuzione e salva i risultati in CSV.
"""
# Definizione test: (nome_immagine, parametri)
# parametri: lista di (F, d)
tests = [
("gradient.bmp", [(8, 4), (8, 8), (8, 12), (8, 14)]),
("bridge.bmp", [(8, 4), (8, 8), (8, 12), (16, 16)]),
("shoe.bmp", [(8, 4), (8, 8), (8, 12), (4, 6)]),
]
print("=" * 90)
print("VALIDAZIONE COMPRESSIONE DCT - ESPERIMENTI SU IMMAGINI REALI")
print("=" * 90)
results = []
for img_name, params in tests:
image_path = f"images/{img_name}"
try:
# Carica l'immagine originale (grayscale)
original_img = Image.open(image_path).convert('L')
original_array = np.array(original_img, dtype=float)
width, height = original_img.size
resolution = f"{width}×{height}"
print(f"\n{'─' * 90}")
print(f"Immagine: {img_name} ({resolution})")
print(f"{'─' * 90}")
print(f"{'F':>3} | {'d':>3} | {'Tempo (ms)':>12}")
print(f"{'-'*3}-+-{'-'*3}-+-{'-'*12}")
for F, d in params:
# Misura il tempo di compressione
start_time = time.perf_counter()
orig_img_trunc, compressed_img = compress_image(image_path, F, d)
end_time = time.perf_counter()
elapsed_ms = (end_time - start_time) * 1000
# Formatta i risultati
time_str = f"{elapsed_ms:.1f}"
print(f"{F:>3} | {d:>3} | {time_str:>12}")
results.append({
'image': img_name,
'resolution': resolution,
'F': F,
'd': d,
'time_ms': elapsed_ms
})
except FileNotFoundError:
print(f"⚠ Immagine non trovata: {image_path}")
except Exception as e:
print(f"❌ Errore durante l'elaborazione di {img_name}: {e}")
# Salva i risultati in CSV
csv_file = "compression_results.csv"
try:
with open(csv_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=['image', 'resolution', 'F', 'd', 'time_ms'])
writer.writeheader()
writer.writerows(results)
print(f"\n{'=' * 90}")
print(f"✓ Risultati salvati in: {csv_file}")
print(f"{'=' * 90}")
except Exception as e:
print(f"❌ Errore durante il salvataggio del CSV: {e}")
# Stampa un riepilogo in formato LaTeX per facile copia nella tabella
print(f"\n{'=' * 90}")
print("FORMATO LATEX PER LA TABELLA")
print(f"{'=' * 90}\n")
current_image = None
for result in results:
if current_image != result['image']:
current_image = result['image']
print(f"% {result['image']} ({result['resolution']})")
print(f"& {result['F']} & {result['d']} & {result['time_ms']:.1f} \\\\")
return results
def test_fixed_F_varying_d():
"""
Test con F fissato (F=8) e d variabile (d=2, 4, 8)
Misura i tempi di compressione per analizzare l'effetto del parametro d
"""
print("\n" + "="*90)
print("ANALISI PARAMETRICA: F FISSATO (F=8), d VARIABILE (d=2, 4, 8)")
print("="*90)
F = 8
d_values = [2, 4, 8]
image_path = "images/gradient.bmp"
try:
original_img = Image.open(image_path).convert('L')
width, height = original_img.size
resolution = f"{width}×{height}"
print(f"\nImmagine: gradient.bmp ({resolution})")
print(f"\n{'F':>3} | {'d':>3} | {'Tempo (ms)':>15}")
print("-"*35)
results = []
for d in d_values:
start_time = time.perf_counter()
_, _ = compress_image(image_path, F, d)
end_time = time.perf_counter()
elapsed_ms = (end_time - start_time) * 1000
print(f"{F:>3} | {d:>3} | {elapsed_ms:>15.2f}")
results.append((F, d, elapsed_ms))
return results
except Exception as e:
print(f"❌ Errore: {e}")
return []
def test_fixed_d_varying_F():
"""
Test con d fissato (d=3) e F variabile (F=4, 10, 16)
Misura i tempi di compressione per analizzare l'effetto del parametro F
"""
print("\n" + "="*90)
print("ANALISI PARAMETRICA: d FISSATO (d=3), F VARIABILE (F=4, 10, 16)")
print("="*90)
d = 3
F_values = [4, 10, 16]
image_path = "images/gradient.bmp"
try:
original_img = Image.open(image_path).convert('L')
width, height = original_img.size
resolution = f"{width}×{height}"
print(f"\nImmagine: gradient.bmp ({resolution})")
print(f"\n{'F':>3} | {'d':>3} | {'Tempo (ms)':>15}")
print("-"*35)
results = []
for F in F_values:
# Verifica che d sia valido per questo F (0 <= d <= 2F-2)
max_d = 2 * F - 2
if d > max_d:
print(f"{F:>3} | {d:>3} | {'N/A (d > 2F-2)':>15}")
continue
start_time = time.perf_counter()
_, _ = compress_image(image_path, F, d)
end_time = time.perf_counter()
elapsed_ms = (end_time - start_time) * 1000
print(f"{F:>3} | {d:>3} | {elapsed_ms:>15.2f}")
results.append((F, d, elapsed_ms))
return results
except Exception as e:
print(f"❌ Errore: {e}")
return []
if __name__ == "__main__":
results = run_compression_tests()
print(f"\n{'=' * 90}")
print(f"✓ Test completati: {len(results)} configurazioni elaborate")
print(f"{'=' * 90}")
# Esegui i test parametrici
results_f_var = test_fixed_F_varying_d()
results_d_var = test_fixed_d_varying_F()
print("\n" + "="*90)
print("RIEPILOGO ANALISI PARAMETRICA")
print("="*90)
if results_f_var:
print("\nAnalisi 1 - F=8 variabile d:")
for F, d, t in results_f_var:
print(f" F={F}, d={d}: {t:.2f} ms")
if results_d_var:
print("\nAnalisi 2 - d=3 variabile F:")
for F, d, t in results_d_var:
print(f" F={F}, d={d}: {t:.2f} ms")
print(f"\n{'=' * 90}")