File: plot_image_comparison.py

package info (click to toggle)
skimage 0.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,720 kB
  • sloc: python: 60,007; cpp: 2,592; ansic: 1,591; xml: 1,342; javascript: 1,267; makefile: 168; sh: 20
file content (107 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download | duplicates (2)
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
"""
=======================
Visual image comparison
=======================

Image comparison is particularly useful when performing image processing tasks
such as exposure manipulations, filtering, and restoration.

This example shows how to easily compare two images with various approaches.

"""

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

import skimage as ski


img1 = ski.data.coins()
img1_equalized = ski.exposure.equalize_hist(img1)
img2 = ski.transform.rotate(img1, 2)


comp_equalized = ski.util.compare_images(img1, img1_equalized, method='checkerboard')
diff_rotated = ski.util.compare_images(img1, img2, method='diff')
blend_rotated = ski.util.compare_images(img1, img2, method='blend')


######################################################################
# Checkerboard
# ============
#
# The `checkerboard` method alternates tiles from the first and the second
# images.

fig = plt.figure(figsize=(8, 9))

gs = GridSpec(3, 2)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1:, :])

ax0.imshow(img1, cmap='gray')
ax0.set_title('Original')
ax1.imshow(img1_equalized, cmap='gray')
ax1.set_title('Equalized')
ax2.imshow(comp_equalized, cmap='gray')
ax2.set_title('Checkerboard comparison')

for a in (ax0, ax1, ax2):
    a.set_axis_off()

fig.tight_layout()


######################################################################
# Diff
# ====
#
# The `diff` method computes the absolute difference between the two images.

fig = plt.figure(figsize=(8, 9))

gs = GridSpec(3, 2)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1:, :])

ax0.imshow(img1, cmap='gray')
ax0.set_title('Original')
ax1.imshow(img2, cmap='gray')
ax1.set_title('Rotated')
ax2.imshow(diff_rotated, cmap='gray')
ax2.set_title('Diff comparison')

for a in (ax0, ax1, ax2):
    a.set_axis_off()

fig.tight_layout()


######################################################################
# Blend
# =====
#
# `blend` is the result of the average of the two images.

fig = plt.figure(figsize=(8, 9))

gs = GridSpec(3, 2)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1:, :])

ax0.imshow(img1, cmap='gray')
ax0.set_title('Original')
ax1.imshow(img2, cmap='gray')
ax1.set_title('Rotated')
ax2.imshow(blend_rotated, cmap='gray')
ax2.set_title('Blend comparison')

for a in (ax0, ax1, ax2):
    a.set_axis_off()

fig.tight_layout()

plt.show()