注意
转到结尾 下载完整的示例代码。或通过 Binder 在浏览器中运行此示例
视觉图像比较#
在执行图像处理任务(例如曝光处理、滤波和恢复)时,图像比较特别有用。
此示例展示了如何使用各种方法轻松比较两个图像。
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from skimage import data, transform, exposure
from skimage.util import compare_images
img1 = data.coins()
img1_equalized = exposure.equalize_hist(img1)
img2 = transform.rotate(img1, 2)
comp_equalized = compare_images(img1, img1_equalized, method='checkerboard')
diff_rotated = compare_images(img1, img2, method='diff')
blend_rotated = compare_images(img1, img2, method='blend')
棋盘格#
checkerboard
方法交替使用来自第一张和第二张图像的瓦片。
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
方法计算两个图像之间的绝对差值。
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
是两个图像平均值的结果。
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()
脚本的总运行时间:(0 分钟 2.670 秒)