画像処理
下のジンジャーエールの瓶の画像を加工する方法を試して見ましょう。
まずこれを、自分のフォルダの中に取り込みます。コピ−&ペーストでOKです。ここでは、Hの情報技術の中に、入れたものとします。
図示してみましょう。
ginger = Import["h:\情報技術\ginger.jpg", "JPEG"] // Show;
これを、グレースケール(白黒)の行列に変換してやります。カラーでも構わないのですが、データ量が多いと、マシンが止まる可能性があるためです。
makeGraylevel[{r_, g_, b_}] := (0.3r + 0.59g +
0.11b)/255;
grayscaleginger = Transpose[Map[makeGraylevel, ginger[[1, 1]],
{2}]];
この図を、ListInterpolationの関数を使って、2次元の関数に変換します。
gingerFunction = ListInterpolation[grayscaleginger]
これを、PlotPoints→200、Mesh→Falseに設定して描いてみます。
DensityPlot[gingerFunction[x, y], {x, 1, 138}, {y,
1, 365},
PlotPoints -> 200,Mesh -> False, AspectRatio ->
Automatic];
サンプリングの幅を等間隔にせず、画像を変形させることも可能です。
Off[InterpolatingFunction::dmval]
DensityPlot[gingerFunction[x^2, y^2], {x, 1, 138^0.5}, {y, 1,
365^(1/2)},
PlotPoints -> 200, Mesh -> False, AspectRatio -> Automatic,
Frame -> None];
フィルタリングをして、モザイクを掛けてみましょう。
b1 = 10;
DensityPlot[gingerFunction[2 b1 Floor[x/b1] - x + b1, 2 b1
Floor[y/b1] - y + b1],
{x, 20, 138}, {y, 20, 365}, PlotPoints -> 200, Mesh ->
False,
AspectRatio -> Automatic, Frame -> None, PlotRange -> {0,
1}];
次の形式を取ると、画像の中心を、原点位置に移動させることが可能です。
gingerFunction =
ListInterpolation[grayscaleginger, {{-1, 1}, {-2.5, 2.5}}];
DensityPlot[gingerFunction[x, y], {x, -1, 1}, {y, -2.5, 2.5},
PlotPoints -> 200, Mesh -> False, AspectRatio ->
Automatic];
中心付近が拡大した形にしてみましょう。
DensityPlot[gingerFunction[Sign[x]x^2,
Sign[y]y^2], {x, -1, 1},
{y, -2.5^0.5, 2.5^0.5}, PlotPoints -> 200, Mesh -> False,
AspectRatio -> Automatic, Frame -> None];
次に、中心付近に光源が集中したようにしてみましょう。
DensityPlot[E^(-(x^2 + y^2))gingerFunction[x, y],
{x, -1, 1}, {y, -2.5, 2.5},手入力は大丈夫ですか
PlotPoints -> 200, Mesh -> False, AspectRatio -> Automatic,
Frame -> None,PlotRange -> {0, 1}];
以前やったような、行列の変換を使って、図を回転させることも可能です。
gingerFunction2[{x_, y_}] := gingerFunction[x,
y];
rotationMatrix[t_] := {{Cos[t], Sin[t]}, {-Sin[t], Cos[t]}};
DensityPlot[gingerFunction2[rotationMatrix[1].{x, y}], {x, -1, 1},
{y, -1, 1},コピペに頼ってませんか?
PlotPoints -> 200, Mesh -> False, AspectRatio -> Automatic,
Frame -> None, PlotRange -> {0, 1}];
自分が取り込んだ絵を同様の手法で変形して見ましょう。