Mathematica 初級・入門コ-ス

Jun Hirabayashi jun@hirax.net

ライブラリ読み込み

<< Graphics`Graphics`

<< Graphics`Graphics3D`

<< Graphics`Polyhedra`

Needs["Statistics`ClusterAnalysis`"]

Needs["Statistics`ContinuousDistributions`"]

Needs["Statistics`MultiDescriptiveStatistics`"]

画像の入出力

画像ファイルを読み込む

image  =  
        Import["C:\Mathematica\mm.png", "PNG"];

Show[image]

画像ファイルの情報を知る

pixels = image[[1, 1]];

pixels[[2]][[2]]

height  =  Length[ pixels ]

width  =  Length[ pixels[[1]] ]

redPixels  =  
        Table[ 
pixels[[ y ]][[ 
                x ]][[1]] , 
{y, 1, 
            height  }, {x, 1, width  }
];

ListDensityPlot[ redPixels  ,  Mesh -> False, 
      AspectRatio -> height/width]

rgbPixels  =  
Table[
          Table[ 
                     pixels[[ y ]][[ 
                  x ]][[i]] , 
                     {y, 1, height  }, {x, 1, 
              width  }
                   ]
, {i,  1, 3}
];

rgbImage  =  
Table[
          ListDensityPlot[  rgbPixels[[i]] ,  Mesh -> False, 
            AspectRatio -> height/width ]
, {i,  1, 
            3}
];

グラフをBMPファイルとして保存

Export[ "C:\\Mathematica\\ImageGraph.BMP", 
        rgbImage[[1]],  "BMP" ];

枠がない画像を作成

rgbImage  =  
Table[
          ListDensityPlot[  rgbPixels[[i]] ,  Mesh -> False, 
            Frame -> False,  AspectRatio -> height/width , 
            ImageSize -> {width, height}]
, {i,  1, 
            3}
];

画像データをBMPファイルとして保存

Export[ "C:\\Mathematica\\Pixels.BMP", 
        rgbImage[[2]],  "BMP" ];

画像データをCSVファイルとして保存

Export[ "C:\\Mathematica\\Pixels.CSV", 
        rgbPixels[[1]],  "CSV" ];

CSVファイルから画像を読み込み

csvPixels  =  
        Import[  "C:\\Mathematica\\Pixels.CSV"  , "CSV" ];

ListDensityPlot[ csvPixels ,  Mesh -> False, 
      AspectRatio -> height/width]

画像をさまざまな出力形式で表示する

birdGraph  =  
        ListPlot3D[ 
rgbPixels[[ 
              1 ]], 
Mesh -> 
            False, 
BoxRatios -> { width, height, 
              20 }
];

アニメ-ション作成

birdGraph  =  
        ListPlot3D[ 
rgbPixels[[1]], 
Mesh -> 
            False, 
BoxRatios -> { width, height, 
              50 }, 
ViewPoint -> { 0, -2, 
              2 }, 
Lighting -> 
            False, 
ImageSize -> {320, 
              240}, 
Axes -> 
            False
];

ani3D = Table[
ListPlot3D[ 
rgbPixels[[1]],
Mesh -> False,
BoxRatios -> { width, height, 
                    30 },
ViewPoint -> { 2* Cos[i],  
                    2 * Sin[i], 2 },
ImageSize -> {320, 240},
ViewCenter -> { 0.5, 0.5, 0},
Axes -> False,
Lighting -> False,
SphericalRegion -> True
]
,{i, 0, 2 π,  2 π / 128 }
];

動画ファイルとして保存

Export["C:\\Mathematica\\rotation.avi",  
        ani3D  ];

画像解析

断面作成

redProfile  =  rgbPixels[[ 1 ]][[100]];

redProfilePlot  =  
      ListPlot[ redProfile ,  
        PlotJoined -> 
          True,  
PlotRange -> {0, 
            255}, 
PlotStyle -> 
          RGBColor[1, 0, 0]  ]

greenProfilePlot  =  
      ListPlot[  rgbPixels[[ 
              2]][[100]] ,  
PlotRange -> {0, 
            255}, PlotJoined -> True,  
        PlotStyle -> RGBColor[0, 1, 0]  ]

blueProfilePlot  =  
      ListPlot[  rgbPixels[[ 
              3]][[100]] ,  
PlotRange -> {0, 
            255}, PlotJoined -> True,  
        PlotStyle -> RGBColor[0, 0, 1]  ]

Show[ {redProfilePlot,  greenProfilePlot,  
        blueProfilePlot} ]

redProfilePlots  =  
        Table[
ListPlot[  rgbPixels[[ 
                  1 ]][[i]] ,  
            PlotJoined -> 
              True,  
PlotRange -> {0, 
                255}, 
PlotStyle -> {Thickness[
                  0.02],  
                RGBColor[1, 0, 
                  0]  } , 
ImageSize -> {320, 
                240}
], 
{i, 1,  
            height }
];

Export["C:\\Mathematica\\profiles.avi",  
        redProfilePlots  ];

断面データをCSVファイルとして保存

\(\(Export["C:\Mathematica\Profile.csv", \ 
{\ \(rgbPixels[\([\ 
                1\ ]\)]\)[\([100]\)], \ \ 
\(rgbPixels[\([\ 
                2\ ]\)]\)[\([100]\)], \ \ 
\(rgbPixels[\([\ 
                3\ ]\)]\)[\([100]\)]}, \ 
"CSV"];\)\)

ListPlot[ profiles ,  PlotJoined -> True ];

ヒストグラム作成

hs  =  Table[i, {i, 0, 255,  8}];

redHistogram = Histogram[ Flatten[ rgbPixels[[ 1 ]]  ], 
HistogramRange -> {0, 255},
HistogramCategories -> hs ,
BarStyle -> RGBColor[ 1, 0, 0 ] 
]

greenHistogram = Histogram[ Flatten[ rgbPixels[[ 2 ]]  ], 
HistogramRange -> {0, 255},
HistogramCategories -> hs ,
BarStyle -> RGBColor[ 0, 1, 0 ] 
]

blueHistogram = Histogram[ Flatten[ rgbPixels[[ 3 ]]  ], 
HistogramRange -> {0, 255},
HistogramCategories -> hs ,
BarStyle -> RGBColor[ 0, 0, 1 ] 
]

Show[  {  redHistogram,  greenHistogram,  
        blueHistogram }  ]

RGB色空間分布

scatterData  =  
        Table[
Cuboid[ { 
rgbPixels[[ 1 ]][[y]][[x]] / 
                10,  
rgbPixels[[ 
                        2 ]][[y]][[x]] / 
                10,  
rgbPixels[[ 
                        3 ]][[y]][[x]] / 
                10} ]
, {y, 1, height , 50 }, {x, 1, 
            width , 50  }
];

Show[ Graphics3D[ scatterData ]  , 
      BoxRatios -> Automatic]

ani3D = Table[
Show[ Graphics3D[ scatterData ]  ,
BoxRatios -> Automatic,
ViewPoint -> { 2* Cos[i],  
                    2 * Sin[i], 2 },
ImageSize -> {320, 240},
ViewCenter -> {128/100,  
                    128/ 10, 128 / 10},
Axes -> False,
Lighting -> False,
SphericalRegion -> True
]
,{i, 0, 2 π,  2 π / 128 }
];

Export["C:\\Mathematica\\scatter.avi",  ani3D  ];

クラスタリングによる減色

dataSparse  =  7;

SampleData  =  
        Table[
{y, 
            x, 
rgbPixels[[ 
                    1 ]][[y]][[x]] ,  
rgbPixels[[ 
                    2 ]][[y]][[x]], 
rgbPixels[[ 
                    3 ]][[y]][[x]]} 
, {y, 
            1, height , dataSparse }, {x, 1, width , 
            dataSparse  }
];

SampleData  =  Flatten[ SampleData , 1];

cl  =  
        FindClusters[  Drop[SampleData, None, {1, 2}] -> SampleData , 
          10 ];

Length[cl]

g  =  
        Table[
tmp  =  
            cl[[i]]; 
Table[
Graphics3D[{
RGBColor[tmp[[1]][[3]]/255, tmp[[1]][[4]]/
                    255, tmp[[1]][[5]]/
                    255], 
Cuboid[{
tmp[[j]][[2]]/dataSparse, 
tmp[[j]][[1]]/
                      dataSparse, 
0
}   ]  }]
, {j, 1, 
              Length[tmp]}
]
, {i, 1, 
            Length[cl]}
];

Show[ g , Lighting -> False, 
      ViewPoint -> { 0, 0, 2 }]

画像加工

screen  =  
        Table[
(Sin[ 
                    y / height *20 * 2 π  ] Cos[ 
                    x / width *20 * 2 π ]  +  1 ) / 
            2, 
{y, 1, height  }, {x, 1, 
            width  }
];

ListDensityPlot[ screen   ,  Mesh -> False, 
      AspectRatio -> height/width, Frame -> False,  
      ImageSize -> {width,  height }]

screenData  =  
        Table[
If[ screen [[y]][[x]]  >  rgbPixels[[ 1 ]][[y]][[x]] / 255 , 1, 
            0]
, {y, 1, height  }, {x, 1, 
            width  }
];

RF[x_]  :=  RGBColor[x, 1, 1]

RS  =  
      ListDensityPlot[ screenData   ,  Mesh -> False, 
        AspectRatio -> height/width, Frame -> False,  
        ImageSize -> {width,  height } , 
        ColorFunction -> RF ]

GF[x_]  :=  RGBColor[1, x, 1]

screenData  =  
        Table[
If[ screen [[y]][[x]]  >  rgbPixels[[ 2]][[y]][[x]] / 255 , 1, 
            0]
, {y, 1, height  }, {x, 1, 
            width  }
];

GS  =  
      ListDensityPlot[ screenData   ,  Mesh -> False, 
        AspectRatio -> height/width, Frame -> False,  
        ImageSize -> {width,  height } , 
        ColorFunction -> GF ]

BF[x_]  :=  RGBColor[1, 1, x]

screenData  =  
        Table[
If[ screen [[y]][[x]]  >  rgbPixels[[ 3 ]][[y]][[x]] / 255 , 1, 
            0]
, {y, 1, height  }, {x, 1, 
            width  }
];

BS  =  
      ListDensityPlot[ screenData   ,  Mesh -> False, 
        AspectRatio -> height/width, Frame -> False,  
        ImageSize -> {width,  height } , 
        ColorFunction -> BF ]