Mathematica で基本的な画像処理を行う方法を教えてください.
このページでは,Mathematica で画像解析を行う方法をご紹介します.ここでは,多くの一般的な操作だけではなく新しく面白い操作・解析も行うことができるListInterpolation関数を使います.
注:ダウンロードする情報量を削減するために,以下のサブセクションではインポートされた元の画像を表示していません.元の画像がご覧になりたい方は,ここをクリックしてください.
グラフィックスファイルのインポート,表示,エキスポート
Mathematica では,ビットマップ,JPEG,GIF,TIFF,プラットフォーム特有の形式の多くを含むほとんどの標準的なグラフィックス形式のファイルを,Importコマンドを使って効率的かつ直観的に読むことができます.多くの場合は,Mathematica が正しいグラフィックス形式を自動的に検出しますが,ユーザが形式を指定することもできます.ファイルパスには2重のバックスラッシュを使わなければならないという点にご注意ください.ファイルをインタラクティブに選びたいときは,「入力」メニューの「ファイルパスの取得」コマンドをお使いください.このコマンドは,標準ファイルダイアログを開き,カーソルの位置に正しくフォーマットされたファイルパスを返します.
![[Graphics:Images/index_gr_1.gif]](Images/index_gr_1.gif)
これでShowを使うとグラフィックスが表示できます.
![[Graphics:Images/index_gr_2.gif]](Images/index_gr_2.gif)
![[Graphics:Images/index_gr_3.gif]](Images/index_gr_3.gif)
![[Graphics:Images/index_gr_4.gif]](Images/index_gr_4.gif)
別の形式でエキスポートすることもできます.使用可能なインポート・エキスポート形式の一覧は,ImportおよびExportのドキュメントでご覧ください.
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
![[Graphics:Images/index_gr_7.gif]](Images/index_gr_7.gif)
![[Graphics:Images/index_gr_8.gif]](Images/index_gr_8.gif)
![[Graphics:Images/index_gr_9.gif]](Images/index_gr_9.gif)
![[Graphics:Images/index_gr_10.gif]](Images/index_gr_10.gif)
トップに戻る
色変換
ColorOutputを使うと,Mathematica の画像のカラーモデルを変更することができます.標準的なカラーモデルは,RGBColor,CMYKColor,GrayLevelの3つです.ColorOutputでは,レンダリンクだけでなく画像表現も変更されます.
![[Graphics:Images/index_gr_11.gif]](Images/index_gr_11.gif)
![[Graphics:Images/index_gr_12.gif]](Images/index_gr_12.gif)
トップに戻る
画像データへのアクセス
Mathematica は数式,リスト,グラフィックスをはじめ,多くの異なる種類のオブジェクトを扱います.各々のオブジェクトは非常に異なっているように見えますが,Mathematica はすべてのオブジェクトを「式」という一様な方法で表現します.例えば,インポートされたグラフィックスファイルは,Graphicsオブジェクトとして保存されます.
式f [x, y, ... ]のオブジェクトf は,式の頭部と言われます.これはHeadを使って抽出することができます.特にMathematica でプログラムを書くときには,式の頭部を抽出して,その式がどのようなものかを調べた方がよい場合がよくあります.
もちろん,インポートされたJPEGファイルとTIFFファイルはともにGraphicsオブジェクトです.
![[Graphics:Images/index_gr_13.gif]](Images/index_gr_13.gif)
![[Graphics:Images/index_gr_14.gif]](Images/index_gr_14.gif)
![[Graphics:Images/index_gr_15.gif]](Images/index_gr_15.gif)
![[Graphics:Images/index_gr_16.gif]](Images/index_gr_16.gif)
ピクセルデータにアクセスするときは,式の頭部をMathematica リストに変換してピクセル値の配列を抽出するのが最も簡単です.下のコマンドで頭部Graphicsを頭部Listに置き換えます.このメカニズムの説明は,このページの目的ではないので省略します.興味のある方は,Replaceのドキュメントをご参照ください.
![[Graphics:Images/index_gr_17.gif]](Images/index_gr_17.gif)
これで作成されたリストには,ピクセル値の配列と画像のサイズ・カラーモデルに関する追加情報が含まれます.Shortを使って,その概略を見てみます.
![[Graphics:Images/index_gr_18.gif]](Images/index_gr_18.gif)
![[Graphics:Images/index_gr_19.gif]](Images/index_gr_19.gif)
ピクセル値は常にデータ構造の最初の要素です.以下のようにピクセル値はMathematica の他のリストと同様に扱えるようになりました.例えば,RGB画像は{r, g, b}の3原色の配列として表現されます.
下の行でピクセル値の配列を抽出します.行列操作についての情報は,Mathematica ブックの1.8.4あるいは「行列の作成および操作方法を教えてください.」をご参照ください.
![[Graphics:Images/index_gr_22.gif]](Images/index_gr_22.gif)
ご覧のように,色値が302x291x3の配列で保存されました.
![[Graphics:Images/index_gr_23.gif]](Images/index_gr_23.gif)
![[Graphics:Images/index_gr_24.gif]](Images/index_gr_24.gif)
以下の例は,画像の赤チャンネルのListDensityPlotです.AllはMathematica 4の新関数で,配列の各次元の要素をすべて取るようMathematica に指示します.
![[Graphics:Images/index_gr_25.gif]](Images/index_gr_25.gif)
![[Graphics:Images/index_gr_26.gif]](Images/index_gr_26.gif)
トップに戻る
簡単な数学
リストに適用できるMathematica 関数はすべて画像データにも適用できます.例えば,以下の2行はグレースケールの画像を反転する2つの異なる方法です.
![[Graphics:Images/index_gr_27.gif]](Images/index_gr_27.gif)
![[Graphics:Images/index_gr_29.gif]](Images/index_gr_29.gif)
![[Graphics:Images/index_gr_30.gif]](Images/index_gr_30.gif)
カラー画像にも同じ方法が使えます.3つの色値を1度にすべて変換するには,Applyを使うのが最も早い方法です.また,RGBColorは0と1の間の値を取ることが想定されているので,pixelvaluesを256で割ります.
![[Graphics:Images/index_gr_31.gif]](Images/index_gr_31.gif)
![[Graphics:Images/index_gr_32.gif]](Images/index_gr_32.gif)
トップに戻る
ヒストグラム
Graphics`Graphics`に含まれるMathematica のヒストグラム関数は小規模のデータにしか使えませんが,BinCountsとListPlotを使うと大規模なものでも簡単に作成することができます.
BinCountsはMathematica に付属のStatistics`DataManipulation`パッケージに含まれています.
まず,パッケージを読み込みます.
![[Graphics: Images/index_gr_33.gif]]( Images/index_gr_33.gif)
次に画像の赤チャンネルにあるピクセル値の数を数えます.
![[Graphics:Images/index_gr_34.gif]](Images/index_gr_34.gif)
![[Graphics:Images/index_gr_35.gif]](Images/index_gr_35.gif)
![[Graphics:Images/index_gr_36.gif]](Images/index_gr_36.gif)
![[Graphics:Images/index_gr_37.gif]](Images/index_gr_37.gif)
トップに戻る
ヒストグラムの操作
例の画像はすでにMathematica リストに変換されているので,そのリストの一部に任意のMathematica 関数を適用することができます.これにより,ヒストグラムの操作が非常に簡単になります.例えば,赤チャンネルの平方根を取ると赤みがかった色調が加わります.RasterArrayの色値は,0から1の間の範囲でなければならないことにご注意ください.コードの#は,純関数です.純関数についての詳細は,Mathematica ブックでご覧ください.
下の画像では,赤チャンネルが70パーセント減っています.
![[Graphics: Images/index_gr_38.gif]]( Images/index_gr_38.gif)
![[Graphics: Images/index_gr_39.gif]]( Images/index_gr_39.gif)
次の画像は,赤チャンネルの色値の平方根を取ったときの効果を示しています.色値の範囲は0と1の間なので,画像は赤味がかっています.
![[Graphics:Images/index_gr_40.gif]](Images/index_gr_40.gif)
![[Graphics:Images/index_gr_41.gif]](Images/index_gr_41.gif)
トップに戻る
画像の一部の選択
スライス
一旦ピクセル情報がMathematica リストに変換されたら,他のMathematica のリストのリストと同様に扱うことができます.下の画像は,266 x 276 x 3のデータセットをz 軸に沿ってスライスすることで抽出された3つのカラーチャンネルです.
リストの一部の選択についての追加情報は,ここをクリックしてご覧ください.
![[Graphics:Images/index_gr_42.gif]](Images/index_gr_42.gif)
![[Graphics:Images/index_gr_43.gif]](Images/index_gr_43.gif)
あるいは,赤チャンネルの200行目までをスライスします.
![[Graphics:Images/index_gr_44.gif]](Images/index_gr_44.gif)
![[Graphics:Images/index_gr_45.gif]](Images/index_gr_45.gif)
このスライス画像をビジュアル化するためには,この行に沿ったピクセルをすべて白に設定します.まず配列を新しい変数にコピーして,元の画像を上書きしないようにします.
![[Graphics:Images/index_gr_46.gif]](Images/index_gr_46.gif)
![[Graphics:Images/index_gr_47.gif]](Images/index_gr_47.gif)
![[Graphics:Images/index_gr_48.gif]](Images/index_gr_48.gif)
![[Graphics:Images/index_gr_49.gif]](Images/index_gr_49.gif)
複数のスライスを1度にプロットする最も簡単な方法は,MultipleListPlot標準パッケージを使うことです.
![[Graphics:Images/index_gr_50.gif]](Images/index_gr_50.gif)
![[Graphics:Images/index_gr_51.gif]](Images/index_gr_51.gif)
![[Graphics:Images/index_gr_52.gif]](Images/index_gr_52.gif)
ListDensityPlotとRasterは二次元配列だけにしか使えないので,フルカラーの画像にマーカーを置くためにはRasterArrayを使わなければなりません.
![[Graphics:Images/index_gr_53.gif]](Images/index_gr_53.gif)
![[Graphics:Images/index_gr_54.gif]](Images/index_gr_54.gif)
![[Graphics:Images/index_gr_55.gif]](Images/index_gr_55.gif)
![[Graphics:Images/index_gr_56.gif]](Images/index_gr_56.gif)
トップに戻る
領域の選択
グラフィックスの領域の選択も簡単にできます.Takeの値を見付けるめには,グラフィックスをクリックし,CONTROLキーを押したままマウスを動かします.ウィンドウの左下隅にポインタの位置が表示されます.複数の点を選ぶためには,選びたい点をすべてクリックしてからコピーし,グラフィックスの選択を解除してペーストします.Mathematica は選んだ点すべてのリストを返します.Mathematica グラフィックスでの座標の選び方についての詳細は,「プロットの座標を抽出する方法を教えてください.」をご参照ください.
以下の行は,51ピクセルx31ピクセルの長方形の画像を選びます.
![[Graphics:Images/index_gr_57.gif]](Images/index_gr_57.gif)
![[Graphics:Images/index_gr_58.gif]](Images/index_gr_58.gif)
![[Graphics:Images/index_gr_59.gif]](Images/index_gr_59.gif)
![[Graphics:Images/index_gr_60.gif]](Images/index_gr_60.gif)
![[Graphics:Images/index_gr_61.gif]](Images/index_gr_61.gif)
次の行で,赤チャンネルを に設定します.
![[Graphics:Images/index_gr_63.gif]](Images/index_gr_63.gif)
![[Graphics:Images/index_gr_64.gif]](Images/index_gr_64.gif)
![[Graphics:Images/index_gr_65.gif]](Images/index_gr_65.gif)
![[Graphics:Images/index_gr_66.gif]](Images/index_gr_66.gif)
結果の画像を元の画像に戻すことも簡単に実行できます.まず,画像を新しい変数にコピーして,元の画像を上書きしないようにします.
![[Graphics:Images/index_gr_67.gif]](Images/index_gr_67.gif)
次にそれぞれの値を変換されたものに置き換えます.
![[Graphics:Images/index_gr_68.gif]](Images/index_gr_68.gif)
![[Graphics:Images/index_gr_69.gif]](Images/index_gr_69.gif)
![[Graphics:Images/index_gr_70.gif]](Images/index_gr_70.gif)
トップに戻る
閾値処理
ある属性に基づいたピクセルが選びたければ,適切な境界値を持つIfを使ってください.例えば,以下の行は,R値が0.5x256より大きいピクセルをすべて選びます.
![[Graphics:Images/index_gr_71.gif]](Images/index_gr_71.gif)
![[Graphics:Images/index_gr_72.gif]](Images/index_gr_72.gif)
リストの一部の選択についての追加情報は,「行列の作成および操作方法を教えてください.」をご覧ください.
トップに戻る
たたみ込みと相関
Mathematica 4の新しい関数,ListConvolveとListCorrelateにより,高度な画像処理タスクの多くが簡単に行えるようになりました.以下のグラフィックスは,画像を2つの3x3カーネルでたたみ込むことにより,Sobelエッジフィルタをその画像に適用したときの効果を示しています.
![[Graphics:Images/index_gr_73.gif]](Images/index_gr_73.gif)
![[Graphics:Images/index_gr_74.gif]](Images/index_gr_74.gif)
トップに戻る
リストの補間とデータ復旧
Mathematica の面白い機能として,任意次元のリストの補間が素早く計算できるというものがあります.この機能により画像を操作したり,データの打切りで失われた情報を復旧したりするための新しい方法が開けます.
The Doghill Mountains
リストの補間の本題に入る前に,面白い操作をしてみましょう.データセットに1次のリストの補間を施し,色値を3Dでプロットします.新しいリアルタイム3Dグラフィックスを使って,グラフィックスを回転させると,画像が浮き上がったりへこんだりして見えます.
![[Graphics:Images/index_gr_75.gif]](Images/index_gr_75.gif)
![[Graphics:Images/index_gr_76.gif]](Images/index_gr_76.gif)
![[Graphics:Images/index_gr_77.gif]](Images/index_gr_77.gif)
![[Graphics:Images/index_gr_78.gif]](Images/index_gr_78.gif)
![[Graphics:Images/interpolation.gif]](Images/interpolation.gif)
データ復旧
「画像の一部を選択する」で選んだ画像の部分に戻りましょう.
![[Graphics:Images/index_gr_80.gif]](Images/index_gr_80.gif)
![[Graphics:Images/index_gr_81.gif]](Images/index_gr_81.gif)
![[Graphics:Images/index_gr_82.gif]](Images/index_gr_82.gif)
![[Graphics:Images/index_gr_83.gif]](Images/index_gr_83.gif)
ご覧の通り,上の画像は51x31のデータポイントだけで構成されているので,解像度はそれほど高くありません.リストを補間することで,より多くの特徴が簡単に復旧できます.
以下のコマンドは,画像の赤チャンネル上でリストの補間を実行し,結果の関数をx 座標とy 座標上の0と1の間でスケールします.
![[Graphics:Images/index_gr_85.gif]](Images/index_gr_85.gif)
![[Graphics:Images/index_gr_86.gif]](Images/index_gr_86.gif)
![[Graphics:Images/index_gr_87.gif]](Images/index_gr_87.gif)
数学的操作
ListInterpolationを使うと,あらゆる種類の画像操作や画像変換が可能です.例えば,微分すると色値が変化します.二次元の微分でもきれいな浮き彫りが施せます.
![[Graphics:Images/index_gr_88.gif]](Images/index_gr_88.gif)
![[Graphics:Images/index_gr_89.gif]](Images/index_gr_89.gif)
トップに戻る
|