In [1]:
from skimage import io

画像表示のためにPyplotをインポート

In [2]:
from matplotlib import pyplot as plt
In [3]:
img_rgb=io.imread('QVGA_COLOR.jpg')
In [4]:
img_rgb.shape # 縦240pix , 320pix ,(R,G,B)
Out[4]:
(240, 320, 3)
In [5]:
plt.imshow(img_rgb)
Out[5]:
<matplotlib.image.AxesImage at 0x224f1733550>

(0,0)画素の値を表示

In [6]:
print(img_rgb[0:1,0:2,:]) # R:254, G:0, B:0
[[[254   0   0]
  [254   0   0]]]
In [7]:
plt.imshow(img_rgb[0:1,0:1,:])
Out[7]:
<matplotlib.image.AxesImage at 0x224f1854668>

(0,63-64)画素

63 64
0 Red Green
In [8]:
print(img_rgb[0:1,63:65,:]) # (0,63)(0,64)画素
[[[254   0   0]
  [  0 255   1]]]
In [9]:
plt.imshow(img_rgb[0:1,63:65,:])
Out[9]:
<matplotlib.image.AxesImage at 0x224f18c30b8>

(0,127-128)画素

127 128
0 Green Blue
In [10]:
print(img_rgb[0:1,127:129,:]) # (0,127)(0,128)画素
[[[  0 255   1]
  [  0   0 254]]]
In [11]:
plt.imshow(img_rgb[0:1,127:129,:])
Out[11]:
<matplotlib.image.AxesImage at 0x224f191f5c0>

numpyで画像データをいじってみる

画像を切り出し

  • 64-127行 x 64-255列の領域を切り出しで表示
In [12]:
plt.imshow(img_rgb[64:128, 64:256])
Out[12]:
<matplotlib.image.AxesImage at 0x224f197eac8>

画像を縦横回転して表示

  • 水平と垂直を入れ替えて表示
  • transposeの引数で軸の順番を指定 0:水平,1:垂直,2:RGB → 1:垂直,0:水平,2:RGB に変更
In [13]:
plt.imshow(img_rgb.transpose(1,0,2))
Out[13]:
<matplotlib.image.AxesImage at 0x224f19dfac8>

画像を透過

透過率αチャンネル付の画像データを作る

In [14]:
import numpy as np
  • 不透明度を表現するαチャネルを追加
  • αチャネル用のデータを定義 初期値:255(透過率0%)
  • img_color.shape[:2] : (320,240,3)のindex[0-1]を取り出す
In [15]:
imag_a=np.full(img_rgb.shape[:2],255) # fullで324×240分のデータを初期値255で定義
In [16]:
imag_a.shape
Out[16]:
(240, 320)
In [17]:
img_rgba=np.dstack((img_rgb,imag_a))
In [18]:
# 1行目を見てみよう。各画素に4番目の要素が追加されている。
img_rgba[0,:,:]
Out[18]:
array([[254,   0,   0, 255],
       [254,   0,   0, 255],
       [254,   0,   0, 255],
       ...,
       [  0,   0,   0, 255],
       [  0,   0,   0, 255],
       [  0,   0,   0, 255]])
In [19]:
plt.imshow(img_rgba) # 初期値は透過率0%なので、普通に表示される。
Out[19]:
<matplotlib.image.AxesImage at 0x224f1a44dd8>

透過率を50%に変更して表示してみる

  • もとの値は維持したいのでテンポラリにコピー
  • 【注意】変数のつもりで'img_tmp=img_rgba'はNG!!
  • 透過率を50%に変更して表示する
In [20]:
img_tmp=img_rgba.copy()
img_tmp[:,:,-1]=128
plt.imshow(img_tmp)
Out[20]:
<matplotlib.image.AxesImage at 0x224f1d2a588>

特定の画素を選択して透過率を変更してみる。

  • R成分がある画素は透過率0%。それ以外は画素の透過率を半分にする
where関数を使ってR成分が0の画素のインデックスを表示してみる。
  • 各軸毎に該当するインデックスが配で出力される
  • 各行の赤・白の画素以外はR成分は0なので、(0,64) (0,65) (0,66)・・・が抽出され、(0,0) (0,63)は抽出されていない。
In [21]:
print(np.where(img_rgba[:,:,0] == 0 ))
(array([  0,   0,   0, ..., 239, 239, 239], dtype=int64), array([ 64,  65,  66, ..., 317, 318, 319], dtype=int64))
  • αチャネルの値を変更(index -1は最終indexなのでαチャネル)。whereでRの値が0の要素を特定し、透過率を半分の128にする。
In [22]:
img_rgba2 = img_rgba.copy()
img_rgba2[:,:,-1] = np.where(img_rgba2[:,:,0] == 0, img_rgba2[:,:,-1]/2, img_rgba2[:,:,-1])
In [23]:
img_rgba2[:,:,-1]
Out[23]:
array([[255, 255, 255, ..., 127, 127, 127],
       [255, 255, 255, ..., 127, 127, 127],
       [255, 255, 255, ..., 127, 127, 127],
       ...,
       [255, 255, 255, ..., 127, 127, 127],
       [255, 255, 255, ..., 127, 127, 127],
       [255, 255, 255, ..., 127, 127, 127]])
In [24]:
# numpyのslice
# RGBA各CHに分けたい場合はimg_r, img_g, img_b, img_a=np.dsplit(img_rgba,4) としてもいい。
img_a = img_rgba2[:,:,-1]
In [25]:
print(img_a)
[[255 255 255 ... 127 127 127]
 [255 255 255 ... 127 127 127]
 [255 255 255 ... 127 127 127]
 ...
 [255 255 255 ... 127 127 127]
 [255 255 255 ... 127 127 127]
 [255 255 255 ... 127 127 127]]
In [26]:
plt.imshow(img_a, cmap='gray')
Out[26]:
<matplotlib.image.AxesImage at 0x224f1d88780>

↑ 255と128なので、白とグレーが表示されるはずなのに、2値化されて白と黒になっている。

In [27]:
plt.imshow(img_a, cmap='gray',vmin=0, vmax=255)
Out[27]:
<matplotlib.image.AxesImage at 0x224f2eecb38>
In [28]:
plt.subplot(1,2,1,title="Color") # 1行2列の1番目に画像を配置
plt.imshow(img_rgba)
plt.subplot(1,2,2,title="Color Transparent") # 1行2列の2番目に透過画像を配置
plt.imshow(img_rgba2)
plt.show()