How do I pass C# Bitmap images as MWArrays into my .NET object created with MATLAB Builder for .NET 2.0?

2 views (last 30 days)
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));

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Jan 2010
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'])

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2006a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!