遮罩正規化互相關#

在此範例中,我們使用遮罩正規化互相關來識別兩個包含無效資料的相似影像之間的相對位移。

在這種情況下,無法在計算互相關之前簡單地遮罩影像,因為遮罩會影響計算。必須從互相關中移除遮罩的影響,如 [1] 中所述。

在此範例中,我們註冊兩個影像之間的平移。然而,其中一個影像約有 25% 的像素已損壞。

import numpy as np
import matplotlib.pyplot as plt

from skimage import data, draw
from skimage.registration import phase_cross_correlation
from scipy import ndimage as ndi

定義影像中無效的區域。無效像素的機率為 25%。這可能是由於偵測器故障,或不受平移影響的邊緣(例如,視窗中移動的物體)。有關更多範例,請參閱參考論文

image = data.camera()
shift = (-22, 13)

rng = np.random.default_rng()
corrupted_pixels = rng.choice([False, True], size=image.shape, p=[0.25, 0.75])

# The shift corresponds to the pixel offset relative to the reference image
offset_image = ndi.shift(image, shift)
offset_image *= corrupted_pixels
print(f'Known offset (row, col): {shift}')

# Determine what the mask is based on which pixels are invalid
# In this case, we know what the mask should be since we corrupted
# the pixels ourselves
mask = corrupted_pixels

detected_shift, _, _ = phase_cross_correlation(image, offset_image, reference_mask=mask)

print(f'Detected pixel offset (row, col): {-detected_shift}')

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharex=True, sharey=True, figsize=(8, 3))

ax1.imshow(image, cmap='gray')
ax1.set_axis_off()
ax1.set_title('Reference image')

ax2.imshow(offset_image.real, cmap='gray')
ax2.set_axis_off()
ax2.set_title('Corrupted, offset image')

ax3.imshow(mask, cmap='gray')
ax3.set_axis_off()
ax3.set_title('Masked pixels')


plt.show()
Reference image, Corrupted, offset image, Masked pixels
Known offset (row, col): (-22, 13)
Detected pixel offset (row, col): [-22.  13.]

實心遮罩是另一個說明範例。在這種情況下,我們對影像和偏移影像的視圖有限。這些影像的遮罩不必相同。phase_cross_correlation 函數會正確識別應該比較影像的哪一部分。

image = data.camera()
shift = (-22, 13)

rr1, cc1 = draw.ellipse(259, 248, r_radius=125, c_radius=100, shape=image.shape)

rr2, cc2 = draw.ellipse(300, 200, r_radius=110, c_radius=180, shape=image.shape)

mask1 = np.zeros_like(image, dtype=bool)
mask2 = np.zeros_like(image, dtype=bool)
mask1[rr1, cc1] = True
mask2[rr2, cc2] = True

offset_image = ndi.shift(image, shift)
image *= mask1
offset_image *= mask2

print(f'Known offset (row, col): {shift}')

detected_shift, _, _ = phase_cross_correlation(
    image, offset_image, reference_mask=mask1, moving_mask=mask2
)

print(f'Detected pixel offset (row, col): {-detected_shift}')

fig = plt.figure(figsize=(8, 3))
ax1 = plt.subplot(1, 2, 1)
ax2 = plt.subplot(1, 2, 2, sharex=ax1, sharey=ax1)

ax1.imshow(image, cmap='gray')
ax1.set_axis_off()
ax1.set_title('Reference image')

ax2.imshow(offset_image.real, cmap='gray')
ax2.set_axis_off()
ax2.set_title('Masked, offset image')

plt.show()
Reference image, Masked, offset image
Known offset (row, col): (-22, 13)
Detected pixel offset (row, col): [-22.  13.]

腳本的總執行時間: (0 分鐘 1.105 秒)

由 Sphinx-Gallery 產生的圖庫