*colorcorrect: 色恒常性に基づいた自動色補正ライブラリ [#i61fe0cf] |&ref(http://gyazo.argmax.jp/data/2f4cd6529b88f386d533320682b5a126.png,center,360x240);| colorcorrectは褪色画像や、白色光源以外の光源化で撮影された画像、ホワイトバランスが狂っている画像等の色を補正するpythonライブラリです。~ 人間の色知覚における色恒常性(Color constancy)の複数の仮説モデルの実装になります。~ 基本的に、パラメータなどをいじること無く色の補正が可能です。 古い色あせた写真をスキャンして元の色を復元するであったり、画像認識の際の前処理に利用して認識性能を上げる等の利用が考えられます。CVPRの論文で実際に利用されています(Benenson et al, 2013)。 -リポジトリ --https://bitbucket.org/aihara/colorcorrect ** ToDo [#e0b7a968] Perceptual color correction through variational techniques.の実装 ** デモ [#v0225add] - http://colorcorrect.argmax.jp |&ref(http://gyazo.argmax.jp/data/fe89d5c25aee79eab3caa6d01d135305.png,center,360x240);| ** 事前準備 [#g236d617] 本ライブラリは、以下のライブラリに依存しています。 -Python2.6 or later -numpy1.4 or later -PIL windows版のバイナリはまだありません。 ** インストール [#p233fcb9] easy_installやpipでインストールが可能です -easy_install easy_install colorcorrect -pip pip install colorcorrect pypiから直接ダウンロードして、インストールすることもできます。 -http://pypi.python.org/pypi/colorcorrect **実装アルゴリズム [#oa62d768] 多くの手法は、色恒常性仮説の一つ、灰色仮説(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のパラメータを増やすと、サンプル数が多くなり精度が上がりますが線形に遅くなります。 **使い方 [#ubfce139] 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)))) **デモ [#a001f4a5] ***サンプル1 [#f9b7a01a] wikimedia commonsの以下の画像(Nitzan Danzig, 1982)を用いて色補正の比較を行います。 -http://commons.wikimedia.org/wiki/File:Yamit_evacuation_2.jpg?uselang=ja パラメータは全てデフォルトのものを用いています。 |~original|~gray world|~max white|~stretch|~gray world + stretch |~retinex| |&ref(http://gyazo.argmax.jp/data/694d353971d899641fc72584452122ef.png,150x220);|&ref(http://gyazo.argmax.jp/data/cab5cdb9898c0404a2b6ab11d6a533d9.png,150x220);|&ref(http://gyazo.argmax.jp/data/c4300174850cca7879d58464a443da98.png,150x220);|&ref(http://gyazo.argmax.jp/data/fe0d7ac6966c22d33148c5bd4ff9cf96.png,150x220);|&ref(http://gyazo.argmax.jp/data/3f6019fb9f73561d2e2529993b77bdc9.png,150x220);|&ref(http://gyazo.argmax.jp/data/c4300174850cca7879d58464a443da98.png,150x220);| |~ |~retinex with adjast|~standard deviation&br;weighted grey world|~standard deviation &br; and luminance &br; weighted gray world|~luminance &br; weighted gray world|~automatic &br; color equalization| ||&ref(http://gyazo.argmax.jp/data/d5886ca3f3c4769e97a39d1c9f9ca414.png,150x220);|&ref(http://gyazo.argmax.jp/data/2895eed38f658a6c8be08ebe9c367090.png,150x220);|&ref(http://gyazo.argmax.jp/data/fa423cf371c912d6e4563183f3461184.png,150x220);|&ref(http://gyazo.argmax.jp/data/97fc312fe8c2fe01275538a34d409693.png,150x220);|&ref(http://gyazo.argmax.jp/data/3a72636425dac3e9cea56979801dc83e.png,150x220);| automatic color equalizationは大体の画像で非常に良い補正画像を出力しますが、計算時間がかかります。gray worldアルゴリズムとstretchなどを組み合わせると、他のアルゴリズムと組み合わせると高速でそこそこの結果が得られます。単純な褪色などはgray world系のアルゴリズムが良いですが、青や赤系の変色などはstretchなどが良い結果を出します。上手く組み合わせると良いでしょう。 **参考文献 [#f768f419] +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. &hatena_bookmark_anywhere();