from skimage import io
from matplotlib import pyplot as plt
img_rgb=io.imread('QVGA_COLOR.jpg')
img_rgb.shape # 縦240pix , 320pix ,(R,G,B)
plt.imshow(img_rgb)
print(img_rgb[0:1,0:2,:]) # R:254, G:0, B:0
plt.imshow(img_rgb[0:1,0:1,:])
63 | 64 | |
---|---|---|
0 | Red | Green |
print(img_rgb[0:1,63:65,:]) # (0,63)(0,64)画素
plt.imshow(img_rgb[0:1,63:65,:])
127 | 128 | |
---|---|---|
0 | Green | Blue |
print(img_rgb[0:1,127:129,:]) # (0,127)(0,128)画素
plt.imshow(img_rgb[0:1,127:129,:])
plt.imshow(img_rgb[64:128, 64:256])
plt.imshow(img_rgb.transpose(1,0,2))
import numpy as np
imag_a=np.full(img_rgb.shape[:2],255) # fullで324×240分のデータを初期値255で定義
imag_a.shape
img_rgba=np.dstack((img_rgb,imag_a))
# 1行目を見てみよう。各画素に4番目の要素が追加されている。
img_rgba[0,:,:]
plt.imshow(img_rgba) # 初期値は透過率0%なので、普通に表示される。
img_tmp=img_rgba.copy()
img_tmp[:,:,-1]=128
plt.imshow(img_tmp)
print(np.where(img_rgba[:,:,0] == 0 ))
img_rgba2 = img_rgba.copy()
img_rgba2[:,:,-1] = np.where(img_rgba2[:,:,0] == 0, img_rgba2[:,:,-1]/2, img_rgba2[:,:,-1])
img_rgba2[:,:,-1]
# numpyのslice
# RGBA各CHに分けたい場合はimg_r, img_g, img_b, img_a=np.dsplit(img_rgba,4) としてもいい。
img_a = img_rgba2[:,:,-1]
print(img_a)
plt.imshow(img_a, cmap='gray')
plt.imshow(img_a, cmap='gray',vmin=0, vmax=255)
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()