Sharpie
Sharpie

Reputation: 1575

c# mat2gray (matlab) function

Anybody know of a image processing library for c# that has a function that works like the mat2gray function in matlab?

Thanks.

Upvotes: 2

Views: 1420

Answers (3)

WebMonster
WebMonster

Reputation: 3031

You could print the exact implementation of MATLAB functions using the type keyword (apart from built-in functions).

type mat2gray

Upvotes: 0

Matt
Matt

Reputation: 2379

Another possibility, if you have access to the MATLAB Builder NE toolbox, is to use deploytool to create an .NET interface to mat2gray (or any other MATLAB functionality you'd like to call from C#). Then you can just wrap up the arguments as MWArray objects, call the .NET wrapper for the MATLAB function, and unwrap the MWArray[] results that are returned.

Upvotes: 0

svrist
svrist

Reputation: 7110

Something like:

public Bitmap mat2gray(int[,] mat,double? amin = null, double? amax = null){

  var sizex = mat.GetLength(0);
  var sizey = mat.GetLength(1);
  if (!amin.HasValue)
    amin = 0;
  if (!amax.HasValue)
    amax = 1;
  var ret = new Bitmap(sizex,sizey);
   for (int i=0; i< sizex;i++){
    for (int j=0; j< sizey;j++){
      int A = (int)((Math.Round(mat[i,j]-amin.Value)*(255.0/amax.Value))%amax.Value);
      ret.SetPixel(i,j,Color.FromArgb(A,A,A));
    }
}

But the amin/amax stuff needs some finetuning

Upvotes: 3

Related Questions