Python OpenCV3でコントラストを強調(濃くする)

Python OpenCV3でコントラストを低減(薄くする) の逆。

[amazonjs asin="4061538225″ locale="JP" title="OpenCVによる画像処理入門 (KS情報科学専門書)"]

前提

ディレクトリ構成などはこことかこことか。

&nbsp

元画像

この画像(akai_tsuki.jpg)を使う。
以前の例だとsource/imageディレクトリ配下に設置する。

akai_tsuki

 

ルックアップテーブル

ルックアップテーブルは、RGBそれぞれに関数を作成してみる。
以下のような関数を3つ作成した。

[python title="ルックアップテーブルの作成"]
min_table = 100
max_table = 192
diff_table = max_table – min_table
look_up_table = np.arange(256, dtype = 'uint8’ )

for i in range(0, min_table):

look_up_table[i] = 0

for i in range(min_table, max_table):

look_up_table[i] = 255 * (i – min_table) / diff_table

for i in range(max_table, 255):

look_up_table[i] = 255

[/python]

 

コントラストを強調

sourceディレクトリ配下にhigh_contrast.pyを作成する。

[python title="high_contrastの内容"]
# -*- coding: utf-8 -*-

import cv2
import numpy as np

if __name__ == '__main__’:

# ルックアップテーブルの生成
min_table = 100
max_table = 192
diff_table = max_table – min_table
look_up_table = np.arange(256, dtype = 'uint8’ )

for i in range(0, min_table):

look_up_table[i] = 0

for i in range(min_table, max_table):

look_up_table[i] = 255 * (i – min_table) / diff_table

for i in range(max_table, 255):

look_up_table[i] = 255

# 画像の読み込み
img_src = cv2.imread("./image/akai_tsuki.jpg", 1)

# コントラストを低減
img_contrast = cv2.LUT(img_src, look_up_table)

# 表示
cv2.imshow("Show LOW CONTRAST Image", img_contrast)
cv2.waitKey(0)
cv2.destroyAllWindows()

[/python]

実行してみる。

[shell]
python high_contrast.py
[/shell]

スクリーンショット 2015-07-01 22.31.58

濃くなっている。