はじめに
ここでは、skimage restoration unwrap_phaseによりラッピングした画像をアンラッピングする例について説明する。
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from matplotlib import pyplot as plt | |
from skimage import img_as_float, color, exposure | |
from skimage.restoration import unwrap_phase | |
from mpl_toolkits.axes_grid1 import ImageGrid | |
img = plt.imread('kaioumaru.jpg') | |
# Load an image as a floating-point grayscale | |
image = color.rgb2gray(img_as_float(img)) | |
# Scale the image to [0, 4*pi] | |
image = exposure.rescale_intensity(image, out_range=(0, 4 * np.pi)) | |
# Create a phase-wrapped image in the interval [-pi, pi) | |
image_wrapped = np.angle(np.exp(1j * image)) | |
# Perform phase unwrapping | |
image_unwrapped = unwrap_phase(image_wrapped) | |
fig = plt.figure(figsize=(6,6)) | |
grid = ImageGrid(fig, 111, | |
nrows_ncols=(2, 2), | |
axes_pad=0.3, | |
cbar_mode="each", | |
cbar_pad=0.03) | |
im0 = grid[0].imshow(image, cmap='gray', vmin=0, vmax=4 * np.pi) | |
grid[0].set_title('Original') | |
grid.cbar_axes[0].colorbar(im0) | |
im1 = grid[1].imshow(image_wrapped,cmap='gray', vmin=-np.pi, vmax=np.pi) | |
grid[1].set_title('Wrapped phase') | |
grid.cbar_axes[1].colorbar(im1) | |
im2 = grid[2].imshow(image_unwrapped, cmap='gray') | |
grid[2].set_title('After phase unwrapping') | |
grid.cbar_axes[2].colorbar(im2) | |
im3 = grid[3].imshow(image_unwrapped - image, cmap='gray') | |
grid[3].set_title('Unwrapped minus original') | |
grid.cbar_axes[3].colorbar(im3) | |
for i in range(4): | |
grid[i].axis("off") | |
plt.savefig('wrap_kaioumaru.jpg',dpi=150) | |
plt.show() |

解説
モジュールのインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np
from matplotlib import pyplot as plt
from skimage import img_as_float, color, exposure
from skimage.restoration import unwrap_phase
from mpl_toolkits.axes_grid1 import ImageGrid
画像データの読み込みとグレースケール化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img = plt.imread('kaioumaru.jpg')
# Load an image as a floating-point grayscale
image = color.rgb2gray(img_as_float(img))
ギムノカリキュウム・海王丸の画像を読み込む。
データ形式を0-1のfloat型にした後にrgb2grayによりグレースケールにする。

画像のデータ範囲をリスケール
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a phase-wrapped image in the interval [-pi, pi)
image_wrapped = np.angle(np.exp(1j * image))
exposure.rescale_intensity()によりデータの範囲を(0, 4 * np.pi)にリスケールする。
rescale_intensityについては下記で解説した。

[scikit-image] 8. 各種均等化法によるコントラストの補正
skimage.exposureのrescale_intensity, equalize_histとequalize_adapthistによる画像のコントラストの補正
位相ラッピング像の生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image_wrapped = np.angle(np.exp(1j * image))
1j * imageにより画像を複素数にする。それの指数をnp.exp()でとることでオイラーの公式が適用できて、np.angle()で偏角をとることで(-π, π)でラッピングされたデータを得ることができる。
位相アンラッピング像の生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image_unwrapped = unwrap_phase(image_wrapped)
画像の表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = plt.figure(figsize=(6,6))
grid = ImageGrid(fig, 111,
nrows_ncols=(2, 2),
axes_pad=0.3,
cbar_mode="each",
cbar_pad=0.03)
im0 = grid[0].imshow(image, cmap='gray', vmin=0, vmax=4 * np.pi)
grid[0].set_title('Original')
grid.cbar_axes[0].colorbar(im0)
im1 = grid[1].imshow(image_wrapped,cmap='gray', vmin=-np.pi, vmax=np.pi)
grid[1].set_title('Wrapped phase')
grid.cbar_axes[1].colorbar(im1)
im2 = grid[2].imshow(image_unwrapped, cmap='gray')
grid[2].set_title('After phase unwrapping')
grid.cbar_axes[2].colorbar(im2)
im3 = grid[3].imshow(image_unwrapped - image, cmap='gray')
grid[3].set_title('Unwrapped minus original')
grid.cbar_axes[3].colorbar(im3)
for i in range(4):
grid[i].axis("off")
plt.savefig('wrap_kaioumaru.jpg',dpi=150)
plt.show()
Imagegrid により画像を表示した。
cbar_mode=”each”などを記述しないとカラーバーは表示されない。
cbar_pad=0.03は画像とカラーバーの間隔となる。
Imagegridでカラーバーを表示するには、grid.cbar_axes[3].colorbar(im3)のようにする。
コードをダウンロード(.pyファイル) コードをダウンロード(.ipynbファイル)参考
Phase Unwrapping — skimage 0.25.2 documentation

オイラーの公式 - Wikipedia
位相アンラッピング(Phase unwrapping)
1D Phase Unwappingをサクッと行う 1.元データの作成 まず元データを作成。適当なデータを作る。 In : from numpy imp...
コメント