-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSIM_2dirs.py
More file actions
48 lines (37 loc) · 1.64 KB
/
Copy pathSSIM_2dirs.py
File metadata and controls
48 lines (37 loc) · 1.64 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
import os
from skimage.metrics import structural_similarity as ssim
import cv2
import numpy as np
def compute_ssim_for_dirs(dir1, dir2, output_file="ssim_results.txt"):
files1 = sorted(os.listdir(dir1))
files2 = sorted(os.listdir(dir2))
if len(files1) != len(files2):
print("Warning: The two directories do not have the same number of files!")
with open(output_file, "w") as f:
total_ssim = 0
count = 0
for file1, file2 in zip(files1, files2):
path1 = os.path.join(dir1, file1)
path2 = os.path.join(dir2, file2)
img1 = cv2.imread(path1, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(path2, cv2.IMREAD_GRAYSCALE)
if img1 is None or img2 is None:
print(f"Skipping {file1} and {file2}: Could not load one of the images.")
continue
if img1.shape != img2.shape:
print(f"Skipping {file1} and {file2}: Images have different dimensions.")
continue
ssim_score, _ = ssim(img1, img2, full=True)
f.write(f"{file1} - {file2}: {ssim_score:.4f}\n")
print('ssmin score: ', ssim_score)
total_ssim += ssim_score
count += 1
if count > 0:
avg_ssim = total_ssim / count
f.write(f"\nAverage SSIM: {avg_ssim:.4f}\n")
print(f"Average SSIM: {avg_ssim:.4f}")
else:
print("No valid image pairs to compute SSIM.")
dir1 = "/Users/sigvard/Desktop/FFHQ"
dir2 = "/Users/sigvard/Desktop/Results FFHQ/Colorization FFHQ /Scale 0.1"
compute_ssim_for_dirs(dir1, dir2, output_file="ssim_results.txt")