Python OpenCV3で疑似カラー処理(モノクロ画像からカラー画像へ?)
疑似カラー処理とは、画像内のRGB値の階調を変換する処理のこと。
やり方などもあっているかわからないため、ご指導いただければ幸いです。
[amazonjs asin="4061538225″ locale="JP" title="OpenCVによる画像処理入門 (KS情報科学専門書)"]
前提
 
元画像
ガンマ変換などと比較しやすいために、同じこの画像(sora2.jpg)を使う。
以前の例だとsource/imageディレクトリ配下に設置する。
ルックアップテーブル
ルックアップテーブルは、RGBそれぞれに関数を作成してみる。
以下のような関数を3つ作成した。
[python title="ルックアップテーブル用の関数"]
def get_lookuptable_r(x):
"""疑似カラー処理のR成分用"""
if x < 128:
return 0
elif x < 192:
return 4 * x – 512
else:
return 256
def get_lookuptable_g(x):
"""疑似カラー処理のG成分用"""
if x < 64:
return 4 * x
elif x < 192:
return 256
else:
return -4 * x + 1024
def get_lookuptable_b(x):
"""疑似カラー処理のB成分用"""
if x < 64:
return 256
elif x < 128:
return -4 * x + 512
else:
return 0
[/python]
赤がR用、緑がG用、青がB用。
iPythonのnotebookとnotebookを使ったグラフの描画を利用して関数を描画した。
上で作った3つの関数は描画用に改造した。
[python title="iPythonで実行したソース"]
%matplotlib inline
import numpy as np
from matplotlib import pyplot
def get_lookuptable_r(ary):
"""疑似カラー処理のR成分用"""
answer = []
for x in ary:
if x < 128:
answer.append(0)
elif x < 192:
answer.append( 4 * x – 512)
else:
answer.append( 256 )
return answer
def get_lookuptable_g(ary):
"""疑似カラー処理のG成分用"""
answer = []
for x in ary:
if x < 64:
answer.append( 4 * x )
elif x < 192:
answer.append( 256 )
else:
answer.append( -4 * x + 1024 )
return answer
def get_lookuptable_b(ary):
"""疑似カラー処理のB成分用"""
answer = []
for x in ary:
if x < 64:
answer.append( 256 )
elif x < 128:
answer.append( -4 * x + 512 )
else:
answer.append( 0 )
return answer
x = np.arange(0, 256)
# B用表示
y = get_lookuptable_b(x)
pyplot.plot(x, y)
# G用表示
y = get_lookuptable_g(x)
pyplot.plot(x, y)
# R用表示
y = get_lookuptable_r(x)
pyplot.plot(x, y)
[/python]
擬似カラー処理
sourceディレクトリ配下にpseudo_color_processing.pyを作成する。
RGBそれぞれのルックアップテーブルを作成し、OpenCV3のLUTメソッドの引数でルックアップテーブルを使って変換したものをマージする。
[python title="pseudo_color_processing.pyの内容"]
# -*- coding: utf-8 -*-
import cv2
import numpy as np
def get_lookuptable_r(x):
"""疑似カラー処理のR成分用"""
if x < 128:
return 0
elif x < 192:
return 4 * x – 512
else:
return 256
def get_lookuptable_g(x):
"""疑似カラー処理のG成分用"""
if x < 64:
return 4 * x
elif x < 192:
return 256
else:
return -4 * x + 1024
def get_lookuptable_b(x):
"""疑似カラー処理のB成分用"""
if x < 64:
return 256
elif x < 128:
return -4 * x + 512
else:
return 0
if __name__ == '__main__’:
# ルックアップテーブルの生成
"""
array([[0],
[0],
[0],
[0],
[0],
[0],
[0],
・・・・・・・・
[0], dtype=’uint8′)
のようなリストが生成される。
"""
look_up_table_r = np.ones((256, 1), dtype = 'uint8’ ) * 0
look_up_table_g = np.ones((256, 1), dtype = 'uint8’ ) * 0
look_up_table_b = np.ones((256, 1), dtype = 'uint8’ ) * 0
for i in range(256):
各RGBごとのルックアップテーブルを作成
look_up_table_r[i][0] = get_lookuptable_r(i)
look_up_table_g[i][0] = get_lookuptable_g(i)
look_up_table_b[i][0] = get_lookuptable_b(i)
# 画像の読み込み
img_src = cv2.imread("./image/sora2.jpg", 1)
# 複数色のチャンネルを分割して配列で取得
# img_bgr[0] に青, img_bgr[1]に緑,img_bgr[2]に赤が入る。
# 読み込んだ画像のグレースケール化
img_gry = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
# 擬似カラー化
img_pcp_r = cv2.LUT(img_gry, look_up_table_r)
img_pcp_g = cv2.LUT(img_gry, look_up_table_g)
img_pcp_b = cv2.LUT(img_gry, look_up_table_b)
# 各擬似カラー化した画像をマージ
img_mrg = cv2.merge((img_pcp_b,img_pcp_g,img_pcp_r))
# 表示
cv2.imshow("Show Pseudo Color Processing Image", img_mrg)
cv2.waitKey(0)
cv2.destroyAllWindows()
[/python]
実行してみる。
[shell]
python pseudo_color_processing.py
[/shell]
何この魔界。
やり方間違ってる気がするなぁ。。。
ディスカッション
コメント一覧
まだ、コメントがありません