How do I pass C# Bitmap images as MWArrays into my .NET object created with MATLAB Builder for .NET 2.0?
2 次查看(过去 30 天)
显示 更早的评论
I have the following MATLAB code that performs edge detection on a matrix of image data:
function iprocess(A)
BW=edge(A, 'sobel');
imshow(BW);
I invoke this function in MATLAB as follows:
A = imread('C:\lena.bmp');
iprocess(A)
I am generating a .NET component from iprocess.m using MATLAB Builder for .NET and want to know how to pass a Bitmap image created in C# to this function. This is how I create a Bitmap image in my C# application:
int width = 256;
int height = 256;
Image image = Image.FromFile("C:\\lena.bmp");
Bitmap bitmap = new Bitmap(image, new Size(width, height));
采纳的回答
MathWorks Support Team
2010-1-22
The Bitmap data must be converted into one of the supported datatypes that are derived from MWArray. The first step is to create a native C# double array containing the image data:
//Get image dimensions
int width = bitmap.Width;
int height = bitmap.Height;
//Declare the double array of grayscale values to be read from "bitmap"
double[,] bnew = new double[width, height];
//Loop to read the data from the Bitmap image into the double array
int i, j;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
Color pixelColor = bitmap.GetPixel(i, j);
double b = pixelColor.GetBrightness(); //the Brightness component
//Note that rows in C# correspond to columns in MWarray
bnew.SetValue(b,i, j);
}
}
And pass it into the .NET object after typecasting it to an MWNumericArray:
//Instantiate the .NET object
iprocessclass ip= new iprocessclass();
//typecast the double[] data into a MWNumericArray and pass it into the .NET method
ip.iprocess((MWNumericArray) bnew);
For more information about the MWArray API refer to the MATLAB documentation:
web([docroot,'/toolbox/dotnetbuilder/MWArrayAPI/HTML/index.html'])
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!