ロハでHDRパノラマ (20): RAW一発からのHDR・階調データの確認

以前、RAW一発からのHDRHDRと呼んでいいのか、みたいなことを書いた。要は、元画像に含まれている階調情報が8 bit以上であれば、HDRと呼んでいいのではないか、ということだった。というわけで、RAW撮影した画像の階調データがどんなものか確かめておくことにする。

同じようなことを考える人はいるもので、こちらの方がプログラムを書いてくださってる。


実は、8 bit階調の画像ファイルなら簡単にヒストグラムを得る方法がある。imagemagickを使えばよい。例えば、

$ convert IMG_6983.JPG histogram:Histogram_CameraJPG.png


横幅256ピクセルヒストグラムが得られる。同様に、16 bit整数階調の画像でヒストグラムを作成しようとすると、いつまで経っても終わらない。恐らく、横幅65536ピクセルヒストグラム画像を生成しているのだろう。いつか終わるのかもしれないが、ワシは待てない。で、あちこち探しているうちに上のプログラムを見つけた。ダウンロードしてコンパイルすると、エラーが出るので適当に誤魔化しておく。winsock.hは要らないはずだし、stricmpはメッセージ出力部分だけなので、それぞれ、バサッとコメントアウトしておく。

$ gcc -lm -o rawhistogram rawhistogram.c
rawhistogram.c: In function ‘LoadRawPGM’:
rawhistogram.c:178: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘long unsigned int *’
rawhistogram.c:178: warning: format ‘%d’ expects type ‘int *’, but argument 4 has type ‘long unsigned int *’
rawhistogram.c:178: warning: format ‘%d’ expects type ‘int *’, but argument 5 has type ‘long unsigned int *’
rawhistogram.c:178: warning: format ‘%d’ expects type ‘int *’, but argument 6 has type ‘long unsigned int *’
rawhistogram.c:186: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
rawhistogram.c:186: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long unsigned int’


ワーニングは出るが通る。試してみる。

$ ./rawhistogram

RAW histogram creator v.0.93 (20070802) by Peter Ruevski (ruevs@hotmail.com)

Usage:
./rawhistogram [-?][-help] file
	-?, -help	This message
	file		PGM input file created with dcraw.

This program will build histograms of RAW files processed into 16 bit RAW PGM
files by using dcraw (http://www.cybercom.net/~dcoffin/dcraw/). For example
if you have a Canon RAW file called IMG_0005.CR2 first use dcraw:

	dcraw -D -4 -t 0 -o 0 -v IMG_0005.CR2

this will create IMG_0005.pgm. Then run this program:

	rawhistogram IMG_0005.pgm

The result will be a (text) file called IMG_0005.csv that contains the RAW
histogram data. This file can easily be opened in a spreadsheet program to plot
the RGGB histograms.


$ dcraw -D -4 -t 0 -o 0 -v IMG_6983.CR2 
Loading Canon PowerShot S95 image from IMG_6983.CR2 ...
Building histograms...
Writing data to IMG_6983.pgm ...


$ ./rawhistogram IMG_6983.pgm 
Processing 3684 by 2760 image - 10167840 pixels.
|--------------------------------------------------------------------|
......................................................................OK


$ ls -l IMG_6983.csv
-rw-r--r-- 1 foobar foobar 901450 2011-04-22 16:56 IMG_6983.csv


カラーマトリックスやホワイトバランスを勘定に入れる前の、生のデータをまず出力し、そいつをプログラムに喰わせている。結果、CSVファイルが得られる。中身はこんな。

$ head IMG_6983.csv 
  val,          R,         G1,         G2,          B
    0,          0,          0,          0,          0
    1,          0,          0,          0,          0
    2,          0,          0,          0,          0
    3,          0,          0,          0,          0
    4,          0,          0,          0,          0
    5,          0,          0,          0,          0
    6,          0,          0,          0,          0
    7,          0,          0,          0,          0
    8,          0,          0,          0,          0


$ tail IMG_6983.csv 
16379,          0,          0,          0,          0
16380,          0,          0,          0,          0
16381,          0,          0,          0,          0
16382,          0,          0,          0,          0
16383,          0,          0,          0,          0
  Sum,    2541960,    2541960,    2541960,    2541960
  Min,        132,        141,        140,        129
  Max,       4095,       4095,       4095,       4095
 Mean, 511.685375, 777.217595, 779.503009, 421.436007
StDev, 439.589819, 746.489274, 747.605587, 435.966405


元のプログラムを書いた方は、30Dユーザなので、14 bit階調 (0:16383)を前提にしている様子。ご丁寧に、最大最小なども算出してくれている。これによると、ワシのカメラS95は、

  • 各チャネルの最小値がどれも0でないことから、黒レベルに若干のオフセットがあり、
  • 各チャネルの最大値が4095であることから、12 bitの階調データ


であることが分かる。つまり、S95のRAW一発からHDR合成しても、そこそこの結果が得られそうな感じ。ちなみに、ヒストグラムはこんな感じだった。

hist.r 直 rawhistogram.c 直 rawhistogram.c.orig 直

関連する記事

HDRとは編集

  • High Dynamic Range、明度差を広く取った映像のこと。本来的な意味ではコンピュータ画面上で表示可能な8bitの色深度を越えた画像ということになるが、実際には8bitの中で明度を調整した画像を指すことが多い。 通常、映像は明るいところと暗いところを同時に.. 続きを読む
  • このキーワードを含むブログを見る