static private double[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; System.Array cr = new double[3]; System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); if (exercise == "squats") { matlab.Execute("c = cs_matlab_classifier(CS_kinectData, CS_groundPlane, 'squats');"); } else if (exercise == "arm raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'arm raise'));"); } else if (exercise == "leg raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg raise'));"); } else if (exercise == "leg extension") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg extension'));"); } matlab.GetFullMatrix("c", "base", cr, ci); double[] cr_d = new double[3]; cr_d = (double[])cr; return(cr_d); }
static private void UseEngine(double[,] ar, ref Array cr, ref Array ci) { // Instantiate MATLAB Engine Interface through com Console.WriteLine("Matlab Startup...\r\n"); MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Make imaginary matricies double[] a_d = new double[] { 68.8, 120, 110, 118, 150, 179, 180, 150, 178, 152, 163, 107, 149, 97.8, 90.1, 94.3, 90.9, 89.9, 53.4 }; matlab.PutFullMatrix("a", "base", a_d, new double[19]); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); Console.WriteLine(matFileCD_command); matlab.Execute(matFileCD_command); // matlab.Execute("open testBayes.m"); // matlab.Execute("dbstop in math_on_numbers.m"); Console.WriteLine("Matlab Processing...\r\n"); matlab.Execute("c = transpose(testBayes(a));"); //matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); try { matlab.GetFullMatrix("c", "base", ref cr, ref ci); } catch (Exception) { Console.WriteLine("someErr"); } }
static void Main(string[] args) { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); System.Array pr = new double[4]; pr.SetValue(11, 0); pr.SetValue(12, 1); pr.SetValue(13, 2); pr.SetValue(14, 3); System.Array pi = new double[4]; pi.SetValue(1, 0); pi.SetValue(2, 1); pi.SetValue(3, 2); pi.SetValue(4, 3); matlab.PutFullMatrix("a", "base", pr, pi); System.Array prresult = new double[4]; System.Array piresult = new double[4]; matlab.GetFullMatrix("a", "base", ref prresult, ref piresult); }
// Calling MATLAB function file to perform Gaussian fitting private void GaussianFitting(int n, double[,] arrSamplesR, double[,] arrSamplesI, ref Array arrModes, ref Array arrMUs, ref Array arrSigmaXSigmaY, ref Array junkModes, ref Array junkMUs, ref Array junkSigmaXSigmaY) { // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); //DateTime startTime = DateTime.Now; // Set input matrices matlab.PutFullMatrix("samples", "base", arrSamplesR, arrSamplesI); //DateTime stopTime = DateTime.Now; //TimeSpan duration = stopTime - startTime; //double RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Set input paramters " + RunTime + " seconds!"); double[] NR = new double[1]; double[] NI = new double[1]; NR[0] = n; NI[0] = 0; matlab.PutFullMatrix("N", "base", NR, NI); //startTime = DateTime.Now; // Using Engine Interface, execute ML script file string appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); matlab.Execute("cd " + appPath); matlab.Execute("cd ..\\..\\..\\Scripts"); matlab.Execute("[modes, MUs, SigmaXSigmaY] = IPPAGaussianFitting(samples, N);"); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Execute ML Script " + RunTime + " seconds!"); //startTime = DateTime.Now; // Using Engine Interface, get matrices from the base workspace. matlab.GetFullMatrix("modes", "base", ref arrModes, ref junkModes); matlab.GetFullMatrix("MUs", "base", ref arrMUs, ref junkMUs); matlab.GetFullMatrix("SigmaXSigmaY", "base", ref arrSigmaXSigmaY, ref junkSigmaXSigmaY); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Retrieve matrices " + RunTime + " seconds!"); }
static private string[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; //System.Array cr = new double[3]; //System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); matlab.Execute("initWorkspace();"); if (exercise == "Squat") { matlab.Execute("c = cs_matlab_classifier('squats', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Arm Raise") { matlab.Execute("c = cs_matlab_classifier('armRaise', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Leg Raise") { matlab.Execute("c = cs_matlab_classifier('legRaise', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Hip Abduction") { matlab.Execute("c = cs_matlab_classifier('legExt', CS_kinectData, CS_groundPlane);"); } string[] cr_d = new string[30]; StreamReader FileStreamReader; FileStreamReader = File.OpenText(System.IO.Path.Combine(MyDocs, ProjectLocation) + "\\results.csv"); int i = 0; while (FileStreamReader.Peek() != -1) { string[] words; words = FileStreamReader.ReadLine().Split(','); cr_d[i] = words[0]; cr_d[i + 1] = words[1]; cr_d[i + 2] = words[2]; i = i + 3; } FileStreamReader.Close(); //matlab.GetFullMatrix("c", "base", cr, ci); //double[] cr_d = new double[3]; //cr_d = (double[])cr; return cr_d; }
/* * All input and output arguments must have been allocated * prior to calling these functions * * Input: * ar real part of a * ai imaginary part of a * br real part of b * bi imaginary part of b * * Output: * cr real part of c * ci imaginary part of c * dr real part of d * di imaginary part of d */ static private void UseEngine(Array ar, Array ai, Array br, Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di) { /* * This function calls the math_by_numbers routine inside * MATLAB using the MATLAB Engine's com interface */ // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Using Engine Interface, put the matrix "a" into // the base workspace. // "a" is a complex variable with a real part of ar, // and an imaginary part of ai matlab.PutFullMatrix("a", "base", ar, ai); // Using Engine Interface, put the matrix "b" into // the base workspace. // "b" is a complex variable with a real part of br, // and an imaginary part of bi matlab.PutFullMatrix("b", "base", br, bi); // Using Engine Interface, execute the ML command // contained in quotes. matlab.Execute("cd c:\\demos\\CSharp_MATLAB\\mcode;"); matlab.Execute("open math_on_numbers.m"); matlab.Execute("dbstop in math_on_numbers.m"); matlab.Execute("[c, d] = math_on_numbers(a,b);"); matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); // Using Engine Interface, get the matrix "c" from // the base workspace. // "c" is a complex variable with a real part of cr, // and an imaginary part of ci matlab.GetFullMatrix("c", "base", ref cr, ref ci); // using engine interface, get the matrix "c" from // the base workspace. // "d" is a complex variable with a real part of dr, // and an imaginary part of di matlab.GetFullMatrix("d", "base", ref dr, ref di); }
//// Print out the permutations of the input //static string ShowPermutations<T>(IEnumerable<T> input, int count) //{ // string s = ""; // foreach (IEnumerable<T> permutation in PermuteUtils.Permute<T>(input, count)) // { // foreach (T i in permutation) // { // s += " " + i.ToString(); // } // s += "\n"; // } // return s; //} private static void UseEngine(Array ar, Array ai, Array br, Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di) { /* * This function calls the math_by_numbers routine inside * MATLAB using the MATLAB Engine's com interface */ // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Using Engine Interface, put the matrix "a" into // the base workspace. // "a" is a complex variable with a real part of ar, // and an imaginary part of ai matlab.PutFullMatrix("a", "base", ar, ai); // Using Engine Interface, put the matrix "b" into // the base workspace. // "b" is a complex variable with a real part of br, // and an imaginary part of bi matlab.PutFullMatrix("b", "base", br, bi); // Put test array into base workspace double[,] SR = new double[2, 2]; double[,] SI = new double[2, 2]; SR[0, 0] = 1; SR[0, 1] = 2; SR[1, 0] = 3; SR[1, 1] = 4; SI[0, 0] = 0; SI[0, 1] = 0; SI[1, 0] = 0; SI[1, 1] = 0; System.Array TR = new double[2]; System.Array TI = new double[2]; TR.SetValue(1, 0); TR.SetValue(2, 1); TI.SetValue(0, 0); TI.SetValue(0, 1); // Test List List<double[]> samples = new List<double[]>(); double[] aaa = new double[2]; double[] bbb = new double[2]; aaa[0] = 5; aaa[1] = 6; bbb[0] = 7; bbb[1] = 8; samples.Add(aaa); samples.Add(bbb); double[,] arrSamplesR = new double[samples.Count, 2]; double[,] arrSamplesI = new double[samples.Count, 2]; for (int i = 0; i < samples.Count; i++) { double[] b = samples[i]; arrSamplesR[i, 0] = b[0]; arrSamplesR[i, 1] = b[1]; arrSamplesI[i, 0] = 0; arrSamplesI[i, 1] = 0; } matlab.PutFullMatrix("ss", "base", arrSamplesR, arrSamplesI); // Using Engine Interface, execute the ML command // contained in quotes. matlab.Execute("cd 'C:\\Lanny\\MAMI\\ZPlayGround\\C# Calling MATLAB\\CSharp_MATLAB\\CSharp_MATLAB\\mcode';"); matlab.Execute("open math_on_numbers.m"); matlab.Execute("dbstop in math_on_numbers.m"); matlab.Execute("[c, d] = math_on_numbers(a,b);"); matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); // Using Engine Interface, get the matrix "c" from // the base workspace. // "c" is a complex variable with a real part of cr, // and an imaginary part of ci matlab.GetFullMatrix("c", "base", ref cr, ref ci); // using engine interface, get the matrix "c" from // the base workspace. // "d" is a complex variable with a real part of dr, // and an imaginary part of di matlab.GetFullMatrix("d", "base", ref dr, ref di); System.Array resultsR = new double[2, 2]; System.Array resultsI = new double[2, 2]; matlab.GetFullMatrix("ss", "base", ref resultsR, ref resultsI); Console.WriteLine(resultsR.GetValue(0, 0).ToString() + " " + resultsR.GetValue(0, 1).ToString() + " " + resultsR.GetValue(1, 0).ToString() + " " + resultsR.GetValue(1, 1).ToString()); }
static private void UseEngine(double[,] ar, ref Array cr, ref Array ci) { // Instantiate MATLAB Engine Interface through com Console.WriteLine("Matlab Startup...\r\n"); MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Make imaginary matricies double[] a_d = new double[] { 68.8, 120, 110, 118, 150, 179, 180, 150, 178, 152, 163, 107, 149, 97.8, 90.1, 94.3, 90.9, 89.9, 53.4 }; matlab.PutFullMatrix("a", "base", a_d, new double[19]); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); Console.WriteLine(matFileCD_command); matlab.Execute(matFileCD_command); // matlab.Execute("open testBayes.m"); // matlab.Execute("dbstop in math_on_numbers.m"); Console.WriteLine("Matlab Processing...\r\n"); matlab.Execute("c = transpose(testBayes(a));"); //matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); try { matlab.GetFullMatrix("c", "base", ref cr, ref ci); } catch (Exception) { Console.WriteLine("someErr"); } }
static private double[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; System.Array cr = new double[3]; System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); if (exercise == "squats") { matlab.Execute("c = cs_matlab_classifier(CS_kinectData, CS_groundPlane, 'squats');"); } else if (exercise == "arm raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'arm raise'));"); } else if (exercise == "leg raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg raise'));"); } else if (exercise == "leg extension") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg extension'));"); } matlab.GetFullMatrix("c", "base", cr, ci); double[] cr_d = new double[3]; cr_d = (double[])cr; return cr_d; }