[Snippets]Passing a Bitmap from C# to a MatLab dll

此篇著重在C#傳遞灰階的bitmap影像資料給matlab dll的程式碼,至於如何編譯與使用matlab dll請參考本站文章build .net assembly dll for c# use by MATLAB r2011amatlab dll usage on Visual Studio 2011

C#

```

Bitmap bitmap = gray.ToBitmap();

int width = bitmap.Width;

int height = bitmap.Height;

double[,]  = new double[height, width];

for (int i = 0; i < width; i++) {

for (int j = 0; j &lt; height; j++) {

    Color pixelColor = bitmap.GetPixel(i, j);

    double b = (pixelColor.R + pixelColor.G + pixelColor.B) / 3; //the Brightness component

    bnew.SetValue(b, j, i);

}

}

e.goGetRecognizedClass((MWNumericArray)bnew);    // 假設有個goGetRecognizedClass的matlab函式


以上的Code Snippets適用在二維影像,若是三維影像 (color image),只要將bnew變成三維 -&gt; for迴圈改成三層 -&gt; 把GetPixel得到的R、G、B值分別丟進bnew而非取平均即可。