はじめに
画像を2×2,4×4や8×8に分割して表示する方法について説明する。
コード
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分割
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid
image = plt.imread('moonworld.jpg')
image.shape
#(800, 800, 3)
def image_div(image, div):
image_list = []
image_size = image.shape[0]
im_size = int(image_size/div)
for i in range(div):
for l in range(div):
im = image[im_size*i:im_size*(i+1)+1,im_size*l:im_size*(l+1)+1]
image_list.append(im)
fig = plt.figure(figsize=(8, 8))
grid = ImageGrid(fig, 111,
nrows_ncols=(div, div),
axes_pad=0.05,
share_all=True,
)
for i in range(len(image_list)):
grid[i].imshow(image_list[i])
grid[i].set_axis_off()
image_div(image,2)

解説
モジュールのインポート
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
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
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
image = plt.imread('moonworld.jpg')

エピテランサ 月世界の発芽後4ヶ月の画像を用いる。画像の大きさは800×800となっている。
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.shape
#(800, 800, 3)
画像を分割し、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
image_list = []
image_size = image.shape[0]
im_size = int(image_size/div)
for i in range(div):
for l in range(div):
im = image[im_size*i:im_size*(i+1)+1,im_size*l:im_size*(l+1)+1]
image_list.append(im)
image_listという空のリストを作成して、それに分割した画像を加えていく方法で画像の分割を行う。
divは分割数であり、2とした場合には2×2=4個に分割される。
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
fig = plt.figure(figsize=(8, 8))
grid = ImageGrid(fig, 111,
nrows_ncols=(div, div),
axes_pad=0.05,
share_all=True,
)
for i in range(len(image_list)):
grid[i].imshow(image_list[i])
grid[i].set_axis_off()
axes_padにより画像間の間隔を調整できる。
画像の表示
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_div(image,2)
4×4に分割した画像の表示
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_div(image,4)

8×8に分割した画像の表示
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_div(image,8)

参考

numpyで画像の分割と連結 - Qiita
Deep Learning等で大きな画像をいくつかに分割したり、分割した画像を結合させたりすることがよくあるが、まとまった記事がなさそうだったのでメモ。動作環境Ubuntu 16.04Pyth…

[matplotlib] 19. mpl_toolkits.axes_grid1 の使い方(画像の表示)
axes_grid1のImageGridを用いて複数の画像を表示する方法
コメント