-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdct1.py
More file actions
146 lines (110 loc) · 4.04 KB
/
Copy pathdct1.py
File metadata and controls
146 lines (110 loc) · 4.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
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pywt
# metode dct
def lossless_image_comparison(image1_path, image2_path):
img1 = Image.open(image1_path).convert('RGB')
img2 = Image.open(image2_path).convert('RGB')
if img1.size != img2.size:
img2 = img2.resize(img1.size)
arr1 = np.array(img1)
arr2 = np.array(img2)
matches = np.sum(arr1 == arr2)
total = arr1.size
similarity = matches / total
return similarity
# metode dct
def dct_image_comparison(image1_path, image2_path, block_size=8):
img1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(image2_path, cv2.IMREAD_GRAYSCALE)
# Resize image2 agar sama ukuran dengan image1 jika diperlukan
if img1.shape != img2.shape:
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
# DCT
def compute_dct(image):
h, w = image.shape
dct_blocks = np.zeros((h, w))
for i in range(0, h, block_size):
for j in range(0, w, block_size):
block = image[i:i+block_size, j:j+block_size]
if block.shape[0] == block_size and block.shape[1] == block_size:
dct_blocks[i:i+block_size, j:j+block_size] = cv2.dct(block.astype(np.float32))
return dct_blocks
dct1 = compute_dct(img1)
dct2 = compute_dct(img2)
# MDCT
mse = np.mean((dct1 - dct2) ** 2)
if mse == 0:
return 1.0
max_pixel = 255.0
similarity = 1 - (mse / (max_pixel ** 2))
return similarity
# DWT
def dwt_image_comparison(image1_path, image2_path, wavelet='haar', level=1):
img1 = cv2.imread(image1_path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(image2_path, cv2.IMREAD_GRAYSCALE)
# Resize image2 agar sama ukuran dengan image1 jika diperlukan
if img1.shape != img2.shape:
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
# Menghitung DWT
def compute_dwt(image):
coeffs = pywt.wavedec2(image, wavelet, level=level)
return coeffs
coeffs1 = compute_dwt(img1)
coeffs2 = compute_dwt(img2)
# DWT
similarity = 0
count = 0
cA1 = coeffs1[0]
cA2 = coeffs2[0]
mse = np.mean((cA1 - cA2) ** 2)
max_pixel = np.maximum(np.max(cA1), np.max(cA2))
if max_pixel > 0:
similarity += 1 - (mse / (max_pixel ** 2))
count += 1
# Band detail
for i in range(1, len(coeffs1)):
(cH1, cV1, cD1) = coeffs1[i]
(cH2, cV2, cD2) = coeffs2[i]
for band1, band2 in [(cH1, cH2), (cV1, cV2), (cD1, cD2)]:
mse = np.mean((band1 - band2) ** 2)
max_pixel = np.maximum(np.max(band1), np.max(band2))
if max_pixel > 0:
similarity += 1 - (mse / (max_pixel ** 2))
count += 1
if count > 0:
return similarity / count
return 0.0
def tampilkan_gambar(image1_path, image2_path, similarity_scores):
img1 = Image.open(image1_path)
img2 = Image.open(image2_path)
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(img1)
axes[0].set_title("Gambar 1")
axes[0].axis('off')
axes[1].imshow(img2)
axes[1].set_title("Gambar 2")
axes[1].axis('off')
title = (
f"Similarity Scores:\n"
f"Pixel-by-Pixel: {similarity_scores['pixel']:.4f}\n"
f"DCT: {similarity_scores['dct']:.4f}\n"
f"DWT: {similarity_scores['dwt']:.4f}"
)
plt.suptitle(title, fontsize=12)
plt.show()
if __name__ == "__main__":
image1 = "image/image1.jpg"
image2 = "image/image2.jpg"
sim_scores = {
'pixel': lossless_image_comparison(image1, image2),
'dct': dct_image_comparison(image1, image2),
'dwt': dwt_image_comparison(image1, image2)
}
print("Similarity Scores:")
print(f"Pixel-by-Pixel: {sim_scores['pixel']:.4f}")
print(f"DCT: {sim_scores['dct']:.4f}")
print(f"DWT: {sim_scores['dwt']:.4f}")
tampilkan_gambar(image1, image2, sim_scores)