colorcorrect: 色恒常性に基づいた自動色補正ライブラリ †
colorcorrectは褪色画像や、白色光源以外の光源化で撮影された画像、ホワイトバランスが狂っている画像等の色を補正するpythonライブラリです。
人間の色知覚における色恒常性(Color constancy)の複数の仮説モデルの実装になります。
基本的に、パラメータなどをいじること無く色の補正が可能です。
古い色あせた写真をスキャンして元の色を復元するであったり、画像認識の際の前処理に利用して認識性能を上げる等の利用が考えられます。CVPRの論文で実際に利用されています(Benenson et al, 2013)。
ToDo †
Perceptual color correction through variational techniques.の実装
デモ †
事前準備 †
本ライブラリは、以下のライブラリに依存しています。
- Python2.6 or later
- numpy1.4 or later
- PIL
windows版のバイナリはまだありません。
インストール †
easy_installやpipでインストールが可能です
pypiから直接ダウンロードして、インストールすることもできます。
実装アルゴリズム †
多くの手法は、色恒常性仮説の一つ、灰色仮説(Gray World Assumption)に基づいています。灰色仮説とは、白色光原下において視野内のすべての色、画像の場合は、全ピクセルのRGB値の平均を取ると灰色になるという仮説のこと。つまり、特殊な光源下の画像や、褪色して色が失われてしまった画像のRGB値の平均値は灰色とは乖離しているため、それのズレを補正して上げることで元の色を推定するという手法になります。もちろん、白色光原下においても平均が灰色にならない画像については正しく補正することが出来ないため、それを修正するような複数の手法が提案されていることになります。
詳細は参考文献を参照
- 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[2]
- colorcorrect.algorithm.retinex
- usage: image
- retinex with adjust[2](Gray WorldとRetinexの混合手法)
- colorcorrect.algorithm.retinex_with_adjust
- usage: image
- standard deviation weighted grey world[3]
- colorcorrect.algorithm.standard_deviation_weighted_grey_world
- usage: image,subblock width(default:20), subblock height(default:20)
- standard deviation and luminance weighted gray world[3]
- colorcorrect.algorithm.standard_deviation_and_luminance_weighted_gray_world
- usage: image,subblock width(default:20), subblock height(default:20)
- luminance weighted gray world[3]
- colorcorrect.algorithm.luminance_weighted_gray_world
- usage: image,subblock width(default:20), subblock height(default:20)
- automatic color equalization[4]
- colorcorrect.algorithm.automatic_color_equalization
- usage: image,slope(default:10),limit(default:1000), samples(default:500)
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は大体の画像で非常に良い補正画像を出力しますが、計算時間がかかります。gray worldアルゴリズムとstretchなどを組み合わせると、他のアルゴリズムと組み合わせると高速でそこそこの結果が得られます。単純な褪色などはgray world系のアルゴリズムが良いですが、青や赤系の変色などはstretchなどが良い結果を出します。上手く組み合わせると良いでしょう。
参考文献 †
- 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.
- E.Y. Lam, "Combining gray world and retinex theory for automatic white balance in digital photography.",in Proc ISCE, 2005.
- 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.