-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_visualizer.py
307 lines (247 loc) · 9.99 KB
/
image_visualizer.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.exposure import histogram
from image_scaler import load_image
def plot_rgb_histogram(image_path, bins=256, figure_size=(10, 6)):
"""
Plot RGB histogram for an input image using skimage.
Parameters:
image_path (str): Path to the image file
bins (int): Number of bins for the histogram
figure_size (tuple): Size of the output figure (width, height)
"""
# Read image using skimage
img = io.imread(image_path)
# Create figure and axis
plt.figure(figsize=figure_size)
# Plot histograms for each channel
colors = ('red', 'green', 'blue')
for i, color in enumerate(colors):
# Calculate histogram using skimage's histogram function
hist, bin_centers = histogram(img[:,:,i], nbins=bins, normalize=True)
# Apply light smoothing using moving average
smoothed_hist = np.convolve(hist, np.ones(5)/5, mode='valid')
smoothed_bins = bin_centers[2:-2] # Adjust bins to match smoothed histogram length
plt.plot(smoothed_bins, smoothed_hist, color=color, alpha=0.7,
linewidth=2, label=color.upper())
# Customize plot
plt.xlabel('Pixel Intensity')
plt.ylabel('Normalized Frequency')
plt.title('RGB Color Histogram')
plt.legend()
plt.grid(True, alpha=0.2)
# Remove top and right spines
plt.gca().spines['top'].set_visible(False)
plt.gca().spines['right'].set_visible(False)
plt.show()
def plot_rgb_histograms_comparison(image1_path, image2_path, bins=256, figure_size=(15, 5)):
"""
Plot RGB histograms for two images side by side for comparison.
Parameters:
image1_path (str): Path to the first image
image2_path (str): Path to the second image
bins (int): Number of bins for the histogram
figure_size (tuple): Size of the output figure (width, height)
"""
# Read both images
img1 = io.imread(image1_path)
img2 = io.imread(image2_path)
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figure_size)
# Plot histograms for each image
for ax, img, title in zip([ax1, ax2], [img1, img2], ['Image 1', 'Image 2']):
colors = ('red', 'green', 'blue')
for i, color in enumerate(colors):
hist, bin_centers = histogram(img[:,:,i], nbins=bins, normalize=True)
# Apply smoothing
smoothed_hist = np.convolve(hist, np.ones(5)/5, mode='valid')
smoothed_bins = bin_centers[2:-2]
ax.plot(smoothed_bins, smoothed_hist, color=color, alpha=0.7,
linewidth=2, label=color.upper())
ax.set_xlabel('Pixel Intensity')
ax.set_ylabel('Normalized Frequency')
ax.set_title(f'RGB Histogram - {title}')
ax.legend()
ax.grid(True, alpha=0.2)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.show()
def plot_rgb_histograms_comparison(image1_path, image2_path, image3_path, image4_path, bins=256, figure_size=(20, 8)):
# Read all images
img1 = io.imread(image1_path)
img2 = io.imread(image2_path)
img3 = io.imread(image3_path)
img4 = io.imread(image4_path)
# Create subplots - 2 rows for image and histogram
fig, ((ax_img1, ax_img2, ax_img3, ax_img4),
(ax_hist1, ax_hist2, ax_hist3, ax_hist4)) = plt.subplots(2, 4, figsize=figure_size)
# Plot images and histograms
for ax_img, ax_hist, img, title in zip(
[ax_img1, ax_img2, ax_img3, ax_img4],
[ax_hist1, ax_hist2, ax_hist3, ax_hist4],
[img1, img2, img3, img4],
['Original', 'Histogram', 'Linear', 'Var']):
# Show image
ax_img.imshow(img)
ax_img.set_title(f'Image - {title}')
ax_img.axis('off') # Hide axes for image
# Plot histogram
colors = ('red', 'green', 'blue')
for i, color in enumerate(colors):
hist, bin_centers = histogram(img[:,:,i], nbins=bins, normalize=True)
# Apply smoothing
smoothed_hist = np.convolve(hist, np.ones(15)/15, mode='valid')
smoothed_bins = bin_centers[7:-7]
ax_hist.plot(smoothed_bins, smoothed_hist, color=color, alpha=0.7,
linewidth=2, label=color.upper())
ax_hist.set_xlabel('Pixel Intensity')
ax_hist.set_ylabel('Normalized Frequency')
ax_hist.set_title(f'RGB Histogram - {title}')
ax_hist.legend()
ax_hist.grid(True, alpha=0.2)
ax_hist.spines['top'].set_visible(False)
ax_hist.spines['right'].set_visible(False)
plt.tight_layout()
plt.show()
from skimage.exposure import histogram
from skimage.util import compare_images
def plot_image_difference(image1_path, image2_path, figure_size=(15, 10)):
"""
Visualize the difference between two images, showing:
1. Original images
2. Difference image
3. RGB histograms of the difference
Parameters:
image1_path (str): Path to the first image
image2_path (str): Path to the second image
figure_size (tuple): Size of the output figure (width, height)
"""
# Read images
img1 = io.imread(image1_path)
img2 = io.imread(image2_path)
# Ensure images have same shape
if img1.shape != img2.shape:
raise ValueError("Images must have the same dimensions")
# Calculate difference
diff = compare_images(img1, img2, method='diff')
# Create figure with subplots
fig = plt.figure(figsize=figure_size)
gs = fig.add_gridspec(2, 3)
# Plot original images and difference
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[0, 2])
ax4 = fig.add_subplot(gs[1, :])
# Display images
ax1.imshow(img1)
ax1.set_title('Image 1')
ax1.axis('off')
ax2.imshow(img2)
ax2.set_title('Image 2')
ax2.axis('off')
# Display difference image
diff_img = ax3.imshow(diff, cmap='RdBu_r')
ax3.set_title('Difference')
ax3.axis('off')
plt.colorbar(diff_img, ax=ax3)
# Plot histogram of differences for each channel
colors = ('red', 'green', 'blue')
for i, color in enumerate(colors):
# Calculate histogram of difference
hist, bin_centers = histogram(diff[:,:,i], nbins=256, normalize=True)
# Apply smoothing
smoothed_hist = np.convolve(hist, np.ones(5)/5, mode='valid')
smoothed_bins = bin_centers[2:-2]
ax4.plot(smoothed_bins, smoothed_hist, color=color, alpha=0.7,
linewidth=2, label=color.upper())
# Customize histogram plot
ax4.set_xlabel('Difference Intensity')
ax4.set_ylabel('Normalized Frequency')
ax4.set_title('RGB Histogram of Differences')
ax4.legend()
ax4.grid(True, alpha=0.2)
ax4.spines['top'].set_visible(False)
ax4.spines['right'].set_visible(False)
plt.tight_layout()
plt.show()
def analyze_image_differences(image1_path, image2_path):
"""
Analyze and print statistical measures of differences between two images.
Parameters:
image1_path (str): Path to the first image
image2_path (str): Path to the second image
"""
# Read images
img1 = io.imread(image1_path)
img2 = io.imread(image2_path)
# Calculate difference
diff = compare_images(img1, img2, method='diff')
# Calculate statistics for each channel
stats = {}
for i, channel in enumerate(['Red', 'Green', 'Blue']):
channel_diff = diff[:,:,i]
stats[channel] = {
'mean': np.mean(channel_diff),
'std': np.std(channel_diff),
'max': np.max(channel_diff),
'min': np.min(channel_diff),
'median': np.median(channel_diff)
}
# Print statistics
print("Image Difference Statistics:")
print("-" * 50)
for channel, measures in stats.items():
print(f"\n{channel} Channel:")
for measure, value in measures.items():
print(f"{measure.capitalize():>10}: {value:.4f}")
def analyze_gaussian_input(gaussian_image):
plt.figure(figsize=(15, 5))
# Plot histograms for each channel
channels = ['Red', 'Green', 'Blue']
for i in range(3):
plt.subplot(1, 3, i+1)
plt.hist(gaussian_image[:,:,i].ravel(), bins=50, density=True)
plt.title(f'{channels[i]} Channel Distribution')
plt.xlabel('Value')
plt.ylabel('Density')
# Print statistics
print("Gaussian Image Statistics:")
print("Mean:", gaussian_image.mean(axis=(0,1)))
print("Std:", gaussian_image.std(axis=(0,1)))
print("Min:", gaussian_image.min(axis=(0,1)))
print("Max:", gaussian_image.max(axis=(0,1)))
plt.tight_layout()
plt.show()
def plot_2d_distributions(gaussian_image):
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
# R-G distribution
axes[0].scatter(gaussian_image[:,:,0].ravel(),
gaussian_image[:,:,1].ravel(),
alpha=0.1, s=1)
axes[0].set_title('R-G Distribution')
axes[0].set_xlabel('Red')
axes[0].set_ylabel('Green')
# R-B distribution
axes[1].scatter(gaussian_image[:,:,0].ravel(),
gaussian_image[:,:,2].ravel(),
alpha=0.1, s=1)
axes[1].set_title('R-B Distribution')
axes[1].set_xlabel('Red')
axes[1].set_ylabel('Blue')
# G-B distribution
axes[2].scatter(gaussian_image[:,:,1].ravel(),
gaussian_image[:,:,2].ravel(),
alpha=0.1, s=1)
axes[2].set_title('G-B Distribution')
axes[2].set_xlabel('Green')
axes[2].set_ylabel('Blue')
plt.tight_layout()
plt.show()
# Example usage
def main():
name = "granite"
plot_rgb_histograms_comparison(f'./result/{name}_source.png', f'./result/{name}_blended.png', f'./result/{name}_linear.png', f'./result/{name}_var.png')
if __name__ == "__main__":
main()