2012-10-30 7 views
8

白黒画像(またはpdf)ファイルがあり、画像の水平プロファイルのヒストグラムを取得する必要があります。つまり、画像の各列について、列内のピクセルのグレースケール値の合計が必要です。イメージがXピクセルでYピクセルの場合、0(完全に黒い列の場合)と255 * Y(完全に白い列の場合)の間のX番号になります。画像の「黒インクレベル」のヒストグラム

this comicComic

の2番目のパネルを参照してください私はこのようなヒストグラムを希望するが、各ビンは、そのx座標(ピクセル)の画像内のすべての「ブラックインク」を表すことになります。

大学院生としては、Linuxのコマンドライン、FOSSプログラム(ImageMagick、gnuplot、Perl、g ++など)のみを使用することで制約があります。 GIMPのようなものは、GUIにアクセスすることができないため、ターミナル経由でコマンドを実行できる場合にのみ役立ちます。視覚的な出力ファイルは、後で役に立ちますが、必要ではありません。

私はこの情報をどのように抽出できるか知っていますか? 「画像プロファイル」を検索するだけで、カラープロファイルに関する情報が得られます。

+1

Python(およびImageモジュール)はありますか? – andyras

+0

無料であれば入手できます。私は何年も前にPythonを学習しようとしましたが、文法のどれもが何の問題もなくなりませんでした。これは私の最初のプロジェクトです( 'python'を入力すると、私は' >>> 'プロンプトになります)。 – user1717828

+0

OK、クール。あなたはどのOSを使用していますか? – andyras

答えて

12

私の好きな無料のユーティリティのうちの2つ、pythonとgnuplotを使用して、2つの動作で答えを出します。

私が教えてくれる大学院生として、無料で使おうと思えば、使い方を学ぶことができる最も汎用性の高いツールの1つです。

はここ(黒は白のための0から255まで)のグレースケール値をカウント、最初の部分をするPythonスクリプトです:

#!/usr/bin/python 

import Image   # basic image processing/manipulation, just what we want 

im = Image.open('img.png')  # open the image file as a python image object 
with open('data.dat', 'w') as f: # open the data file to be written 
    for i in range(im.size[0]): # loop over columns 
     counter = sum(im.getpixel((i,j)) for j in range(im.size[1])) 
     f.write(str(i)+'\t'+str(counter)+'\n') # write to data file 

衝撃的無痛!あなたは(私はimg.pngとして保存XKCDコミック、この場合には)プロットしたい画像と同じディレクトリに保存し、これらのスクリプトを実行するには

#!/usr/bin/gnuplot 

set terminal pngcairo size 925,900 
set output 'plot.png' 
#set terminal pdfcairo 
#set output 'plot.pdf' 
set multiplot 

## first plot 
set origin 0,0.025    # this plot will be on the bottom 
set size 1,0.75     # and fill 3/4 of the whole canvas 

set title "Black count in XKCD 'Self-Description'" 
set xlabel 'Column' 
set ylabel "Black\ncount" norotate offset screen 0.0125 

set lmargin at screen 0.15  # make plot area correct size 
set rmargin at screen 0.95  # width = 740 px = (0.95-0.15)*925 px 

set border 0     # these settings are just to make the data 
set grid      # stand out and not overlap with the tics, etc. 
set tics nomirror 
set xtics scale 0.5 out 
set ytics scale 0 

set xr [0:740]     # x range such that there is one spike/pixel 

## uncomment if gnuplot version >= 4.6.0 
## this will autoset the x and y ranges 
#stats 'data.dat' 
#set xr [STATS_min_x:STATS_max_x+1] 
#set yr [STATS_min_y:STATS_may_y] 

plot 'data.dat' with impulse notitle lc 'black' 

## second plot 
set origin 0,0.75    # this plot will be on top 
set size 1,0.25     # and fill 1/4 of the canvas 

unset ylabel; unset xlabel  # clean up a bit... 
unset border; unset grid; unset tics; unset title 

set size ratio -1    # ensures image proper ratio 
plot 'img.png' binary filetype=png with rgbimage 

unset multiplot   # important to unset multiplot! 

:今すぐヒストグラム*を作るGNUPLOT持っています。それらを実行可能にする。 (のpython +画像モジュール+ gnuplotがすべてインストールされている場合)はbashで、これが次に

$ chmod 755 grayscalecount.py plot.plt 

である、あなたは、私が得るのgnuplot 4.4.3でのUbuntu 11.10を実行している、私のコンピュータ上で

$ ./grayscalecount.py 
$ ./plot.plt 

を実行することができます最後に、このクールなプロット:

enter image description here

**サイドノート*:作ることができるのgnuplot異なるヒストグラムプロットが多いです。私はこのスタイルがデータをうまく披露したと思ったが、gnuplot histogramsのデータを整形することができます。

pythonをプロットする方法はたくさんありますが、gnuplot(matplotlib、pygnuplot、gnuplot-py)を使ってプロットすることもできますが、私はそれらのものと同じではありません。 Gnuplotはプロットのために素晴らしいスクリプトであり、Python、Bash、C++などでうまく動作させる方法はたくさんあります。

+1

あなた、サーは、StackOverflowコミュニティのモデルです。私はあなたのコメント/ヒントを以前の[Paulの良い仕事](http://stackoverflow.com/questions/4188104/pil-create-one-dimensional-histogram-of-image-color-lightness#4199541)と 'plot "dataFile"を使用して1と同様の何かを得た。あなたのヒントとあなたの明快なステップバイステップの統合チュートリアルをありがとう。今後のGoogle社員をあなたの答えに導くのに十分な一般的な質問だと思います。 – user1717828

+0

あなたは大歓迎です、そして、stackoverflowへようこそ!私はその答えを思いついて楽しい時間を過ごしました。 – andyras

+1

非常に良い答え(+1)。私はあなたのPythonコードをPEP-8に準拠させるように書式を設定しました。コンテキストマネージャを使って、ファイルが正しくフラッシュされるようにしました。(あなたの古い方法は、プログラムを終了したときに、あなたはファイルを閉じなかった)。範囲内のj(im.size [1]))に対して 'sum(im.getpixel((i、j))'を呼び出すために、 'sum'を使用することをお勧めします。 'im 'がインデックス付けのようにnumpyをサポートしている場合、' sum(im [i、:]) 'も動作します(しかし' Image'を使用しないので、 – mgilson

関連する問題