はじめに
skimage.transform のpyramid_gaussian, rescaleを用いて、画像の一部分を低解像度(ぼかし加工)化する方法について説明する。
コード
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
import matplotlib.pyplot as plt
from skimage.transform import pyramid_gaussian
from skimage.transform import rescale
from skimage.util import img_as_ubyte
image = plt.imread('komachi.jpg')
pyramid = tuple(pyramid_gaussian(image, downscale=2, multichannel=True))
image_rescaled_5 = rescale(pyramid[5], 2**5, order=1,anti_aliasing=False)
image_rescaled_5 = img_as_ubyte(image_rescaled_5)
im5 = image.copy()
im5[100:800,300:730,:] = image_rescaled_5[100:800,300:730,:]
fig, axes = plt.subplots(1, 3, figsize=(10,3.4))
ax = axes.flatten()
ims = [image, image_rescaled_5,im5]
title = ['Original','Low dpi', 'Mix']
for i in range(3):
ax[i].imshow(ims[i])
ax[i].set_title(title[i])
ax[i].axis('off')
plt.savefig('komacchi_bokashimix.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
import matplotlib.pyplot as plt
from skimage.transform import pyramid_gaussian
from skimage.transform import rescale
from skimage.util import img_as_ubyte
画像の読み込み
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 = plt.imread('komachi.jpg')
パロディア属の紅小町を用いる。画像のサイズは1024 x 1024となっている。

低解像度画像の生成
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
pyramid = tuple(pyramid_gaussian(image, downscale=2, multichannel=True))
pyramid_gaussianにより、低解像度の画像を生成する。pyramid_gaussianについては下記で解説した。

[scikit-image] 25. 一定倍率で縮小された連続画像を生成(transform.pyramid_gaussian)
ここでは、skimage.transformのpyramid_gaussianによる一定倍率で縮小された連続画像の生成方法について説明する。
低解像度化した画像を表示すると下記のようになる。
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, axes = plt.subplots(2, 5, figsize=(16,7))
ax = axes.flatten()
for i in range(len(pyramid)-1):
ax[i].imshow(pyramid[i])
x,y,z = pyramid[i].shape
ax[i].set_title(''+str(x)+'*'+str(y)+'', fontsize=15)
plt.savefig('komacchi_pyramid.jpg',dpi=50)
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
image_rescaled_5 = rescale(pyramid[5], 2**5, order=1,anti_aliasing=False)
image_rescaled_5 = img_as_ubyte(image_rescaled_5)
rescaleで画像を元画像の大きさまで大きくする。ここではpyramid[5](上図左下の32 x 32の画像)を 2の5乗(32)倍に拡大する。order=1とすることで各画素がスプライン補完され、ぼかしがはいったような画像が得られる。
rescaleした画像はfloat形式(0〜1)となるので、img_as_ubyteで0〜255の値に戻す。
画像の一部の低解像度化
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
im5 = image.copy()
im5[100:800,300:730,:] = image_rescaled_5[100:800,300:730,:]
im5[100:800,300:730,:]の部分を低解像度な画像であるimage_rescaled_5[100:800,300:730,:]に置き換える。
結果の表示
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, axes = plt.subplots(1, 3, figsize=(10,3.4))
ax = axes.flatten()
ims = [image, image_rescaled_5,im5]
title = ['Original','Low dpi', 'Mix']
for i in range(3):
ax[i].imshow(ims[i])
ax[i].set_title(title[i])
ax[i].axis('off')
plt.savefig('komacchi_bokashimix.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
image_rescaled_4 = rescale(pyramid[4], 2**4, order=1,anti_aliasing=False)
image_rescaled_4 = img_as_ubyte(image_rescaled_4)
image_rescaled_6 = rescale(pyramid[6], 2**6, order=1,anti_aliasing=False)
image_rescaled_6 = img_as_ubyte(image_rescaled_6)
im4 = image.copy()
im4[100:800,300:730,:] = image_rescaled_4[100:800,300:730,:]
im6 = image.copy()
im6[100:800,300:730,:] = image_rescaled_6[100:800,300:730,:]
fig, axes = plt.subplots(2, 2, figsize=(8,8))
ax = axes.flatten()
ims = [image, im4,im5,im6]
title = ['Original','弱', '中', '強']
for i in range(4):
ax[i].imshow(ims[i])
ax[i].set_title(title[i])
ax[i].axis('off')
plt.savefig('komacchi_bokashis.jpg',dpi=120)
plt.show()

参考
403 Forbidden
skimage.transform — skimage 0.26.0rc0.dev0 documentation
skimage.transform — skimage 0.26.0rc0.dev0 documentation
skimage.util — skimage 0.26.0rc0.dev0 documentation
コメント