Interface2017年05月号新画像処理101を読む

モジュールのインポート

import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

技1 色の変換

1-1 大胆に色を変える

img_src = np.ones((600,500,3)) * 255

#左上、右下の座標、カラー、線の太さを指定
cv2.rectangle(img_src,(100,25),(300,150),(255,0,0),-1)
cv2.rectangle(img_src,(100,25+200),(300,150+200),(0,255,0),-1)
cv2.rectangle(img_src,(100,25+400),(300,150+400),(0,0,255),-1)

#cv2.splitはRGB画像を3つに分離
img_bgr = cv2.split(img_src)
#blue->green,green->red,red->blue
#cv2.mergeはマージする
img_dst = cv2.merge((img_bgr[2],img_bgr[0],img_bgr[1]))
cv2.imshow("img_src",img_src)
cv2.imshow("img_dst",img_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

f:id:bitop:20180113095127p:plain

f:id:bitop:20180113095154p:plain