colorcorrect: 色恒常性アルゴリズムの実装 †
colorcorrectは褪色画像や、白色光源以外の光源化で撮影された画像、ホワイトバランスが狂っている画像等の色を補正するpythonライブラリです。
人間の色知覚における色恒常性(Color constancy)の複数の仮説モデルの実装になります。
基本的に、パラメータなどをいじること無く色の補正が可能です。
古い色あせた写真をスキャンして元の色を復元するであったり、画像認識の際の前処理に利用して認識性能を上げる等の利用が考えられます。
事前準備 †
本ライブラリは、以下のライブラリに依存しています。
- Python2.6 or later
- numpy1.4 or later
- PIL
インストール †
easy_installやpipでインストールが可能です
pypiから直接ダウンロードして、インストールすることもできます。
実装アルゴリズム †
詳細は参考文献を参照
- gray world[1]
- colorcorrect.algorithm.gray_world
- usage: image
- max white[1]
- colorcorrect.algorithm.max_white
- usage: image
- stretch[1]
- colorcorrect.algorithm.stretch
- usage: image
- retinex[1]
- colorcorrect.algorithm.retinex
- usage: image
- retinex with adjust[1]
- colorcorrect.algorithm.retinex_with_adjust
- usage: image
- standard deviation weighted grey world[2]
- colorcorrect.algorithm.standard_deviation_weighted_grey_world
- usage: image,subblock width(default:20), subblock height(default:20)
- standard deviation and luminance weighted gray world[2]
- colorcorrect.algorithm.standard_deviation_and_luminance_weighted_gray_world
- usage: image,subblock width(default:20), subblock height(default:20)
- luminance weighted gray world[2]
- colorcorrect.algorithm.luminance_weighted_gray_world
- usage: image,subblock width(default:20), subblock height(default:20)
- automatic color equalization[3]
- colorcorrect.algorithm.automatic_color_equalization
- usage: image,slope(default:10),limit(default:1000)
automatic color equalizationは、フルに計算すると非常に重いため、ランダムサンプリングをして計算量を下げるという適当なあんちょこをしています。limitのパラメータを増やすと、サンプル数が多くなり精度が上がりますが線形に遅くなります。
使い方 †
PILのイメージオブジェクトをfrom_pil関数でcolorcorrectで扱うデータ構造に変換して用います。
>>> import Image
>>> import colorcorrect.algorithm as cca
>>> from colorcorrect.util import from_pil, to_pil
>>> img = Image.open('/path/to/image')
>>> to_pil(cca.stretch(from_pil(img))).show()
to_pil関数で、PILのImageオブジェクトに戻すことができます。また、複数のアルゴリズムを、以下のようにチェインして用いることもできます。
>>>cca.stretch(cca.gray_world(from_pil(img))))
デモ †
サンプル1 †
wikimedia commonsの以下の画像(Nitzan Danzig, 1982)を用いて色補正の比較を行います。
パラメータは全てデフォルトのものを用いています。
original | gray world | max white | stretch | gray world + stretch | retinex |
---|
 |  |  |  |  |  |
| retinex with adjast | standard deviation weighted grey world | standard deviation and luminance weighted gray world | luminance weighted gray world | automatic color equalization |
---|
|  |  |  |  |  |
automatic color equalizationは大体の画像で非常に良い補正画像を出力しますが、非常に計算時間がかかります。stretchアルゴリズムは、単純な割に良い出力を帰す場合が多いです。gray worldアルゴリズムと組み合わせるなど、他のアルゴリズムと組み合わせると高速でそこそこの結果が得られます。
参考文献 †
- D. Nikitenko, M. Wirth and K. Trudel, "Applicability Of White-Balancing Algorithms to Restoring Faded Colour Slides: An Empirical Evaluation.", Journal of Multimedia, vol. 3, no. 5, 2008.
- HK. Lam, OC. Au and CW. Wong, "Automatic white balancing using luminance component and standard deviation of RGB components.", in Proc. ICASSP, 2004.
- A. Rizzi, C. Gatta and D. Marini, "A new algorithm for unsupervised global and local color correction.", Pattern Recognition Letters, vol. 24, no. 11, 2003.