First of all you have to open a Console Application project in VS2010. To execute this code you have to have installed Matlab in your PC. Also, you have to include the :
using System.Reflection;
and finally save on your desktop (or whereever you like) a JPEG image and load it from there (you'll need the whole path of your image).
The code below in C# is leading in the same results as executing in the Matlab Command line the following:
I = rgb2gray(imread('C:\users\cex\desktop\1.jpg'));imshow(I);O = edge(I,'sobel');imshow(O);
Processed JPEG |
Original JPEG |
So, the code in C#, in the Console Application will be:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace matlab1
{
class Program
{
static void Main(string[] args)
{
//Get the type info
Type matlabtype;
matlabtype = Type.GetTypeFromProgID("matlab.application");
//Create an instance of MATLAB
object matlab;
matlab = Activator.CreateInstance(matlabtype);
//Prepare input as an object
object[] arrayInput = new Object[] { "I = rgb2gray(imread('C:/users/cex/desktop/1.jpg'));imshow(I);O = edge(I,'sobel');imshow(O);" };
//Call MATLAB method
matlabtype.InvokeMember("Execute", BindingFlags.InvokeMethod, null, matlab, arrayInput);
Console.ReadKey();
}
}
}
debugging this piece of code we will take open the matlab command window and the processed figure as shown below:
No comments:
Post a Comment