Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using Emgu.CV;
  8. using Emgu.CV.CvEnum;
  9. using Emgu.CV.Structure;
  10. using Emgu.CV.UI;
  11. using Emgu.CV.XImgproc;
  12. using System.Collections;
  13.  
  14. namespace Prototype_2
  15. {
  16.     class Program
  17.     {        
  18.         public int matching(Mat input, Mat otherMat)
  19.         {
  20.             Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>();
  21.             Image<Gray, Byte> otherImg = otherMat.ToImage<Gray, Byte>();
  22.             int match = 0;
  23.  
  24.             for (int y = 0; y < inputImg.Rows; y++)
  25.             {
  26.                 for (int x = 0; x < inputImg.Cols; x++)
  27.                 {
  28.                     match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
  29.                 }
  30.             }
  31.             return match;
  32.         }
  33.  
  34.         public long dataBaseMatching(Mat input, Mat[] array)
  35.         {
  36.             Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>();
  37.  
  38.             long bestMatch = 9876543210123;
  39.  
  40.             for (int i = 0; i < array.Length; i++)
  41.             {
  42.                 int match = 0;
  43.                 Image<Gray, Byte> otherImg = array[i].ToImage<Gray, Byte>();
  44.                 for (int y = 0; y < inputImg.Rows; y++)
  45.                 {
  46.                     for (int x = 0; x < inputImg.Cols; x++)
  47.                     {
  48.                         match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
  49.                     }
  50.                 }
  51.                 //compare match values
  52.                 if (match < bestMatch)
  53.                 {
  54.                     bestMatch = match;
  55.                 }
  56.                 //Console.WriteLine(bestMatch);
  57.                 Console.WriteLine(match);
  58.             }
  59.             return bestMatch;
  60.         }
  61.  
  62.         static void LOOCV(Mat[] array)
  63.         {
  64.             for (int j = 0; j < array.Length; j++)
  65.             {
  66.                 Image<Gray, Byte> testImg = array[j].ToImage<Gray, Byte>();
  67.                 long bestMatch = 9876543210123;
  68.                 int picN = 0;
  69.  
  70.                 for (int i = 0; i < array.Length; i++)
  71.                 {
  72.                     if (i != j)
  73.                     {
  74.                         int match = 0;
  75.  
  76.                         Image<Gray, Byte> sampleImg = array[i].ToImage<Gray, Byte>();
  77.  
  78.                         for (int y = 0; y < testImg.Rows; y++)
  79.                         {
  80.                             for (int x = 0; x < testImg.Cols; x++)
  81.                             {
  82.                                 match += Math.Abs(testImg.Data[y, x, 0] - sampleImg.Data[y, x, 0]);
  83.                             }
  84.                         }
  85.                         if (match < bestMatch)
  86.                         {
  87.                             bestMatch = match;
  88.                             picN = i;
  89.                         }
  90.  
  91.  
  92.                         //Console.WriteLine(i+ "pic: " + match + "\n");
  93.                     }
  94.                 }
  95.                 //Console.WriteLine("Best: " + bestMatch +" picN= " +picN);
  96.                 //Console.WriteLine("For image"+j+" \n \n");
  97.                 Console.WriteLine("Image number " + j + " is closest to " + picN);
  98.             }
  99.         }
  100.  
  101.         static void KNearest(Mat input, Mat[] array, int k)
  102.         {            
  103.             if (k % 2 == 0) //Check wether k is uneven(for majority vote)
  104.             {
  105.                 Console.WriteLine("Only uneven numbers.");
  106.             }
  107.             else
  108.             {                
  109.                 Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>(); //Load input image and make a new ArrayList
  110.                 ArrayList myAL = new ArrayList();
  111.                
  112.                 for (int i = 0; i < array.Length; i++) //Go trough database
  113.                 {                    
  114.                     int match = 0; //Reset match and load images from array
  115.                     Image<Gray, Byte> otherImg = array[i].ToImage<Gray, Byte>();
  116.                    
  117.                     for (int y = 0; y < inputImg.Rows; y++) //Go through pixels calculating Manhatan distance
  118.                     {
  119.                         for (int x = 0; x < inputImg.Cols; x++)
  120.                         {
  121.                             match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
  122.                         }
  123.                     }                    
  124.                     myAL.Add(match + " pic " + i); //Add the results to the list
  125.                 }                
  126.                 myAL.Sort(); //Sort the list in ascending order and define 2 counters
  127.                 int g1 = 0, g2 = 0;
  128.                
  129.                 for (int s = 0; s < k; s++)//Go through list getting the first K elements from the list
  130.                 {
  131.                     Console.WriteLine(myAL[s]);                    
  132.                     if (Int32.Parse(myAL[s].ToString().Last().ToString()) < 5) //Vote for which gesture it is
  133.                         g1++;
  134.                     else
  135.                         g2++;
  136.                 }
  137.                
  138.                 if (g1 > g2) //Result from votes
  139.                     Console.WriteLine("Gesture 1");
  140.                 else
  141.                     Console.WriteLine("Gesture 2");
  142.             }
  143.         }
  144.  
  145.         static void LOOCV2(Mat[] array, int k)
  146.         {
  147.             if (k % 2 == 0) //Check wether k is uneven(for majority vote)
  148.             {
  149.                 Console.WriteLine("Only uneven numbers.");
  150.             }
  151.             else
  152.             {
  153.                 int correctness = 0;
  154.                
  155.                 for (int j = 0; j < array.Length; j++) //Go trough database (Outer)
  156.                 {                    
  157.                     Image<Gray, Byte> testImg = array[j].ToImage<Gray, Byte>(); //Load test image and make a new ArrayList
  158.                     ArrayList myAL = new ArrayList();
  159.                    
  160.                     for (int i = 0; i < array.Length; i++) //Go trough database (Inner)
  161.                     {                        
  162.                         if (i != j) //Check for match with Outer
  163.                         {                            
  164.                             int match = 0; //Reset match and load images from array
  165.                             Image<Gray, Byte> sampleImg = array[i].ToImage<Gray, Byte>();
  166.                            
  167.                             for (int y = 0; y < testImg.Rows; y++) //Go through pixels calculating Manhatan distance
  168.                             {
  169.                                 for (int x = 0; x < testImg.Cols; x++)
  170.                                 {
  171.                                     match += Math.Abs(testImg.Data[y, x, 0] - sampleImg.Data[y, x, 0]);
  172.                                 }
  173.                             }                            
  174.                             myAL.Add(match + " pic " + i); //Add the results to the list
  175.                         }
  176.                     }                    
  177.                     myAL.Sort(); //Sort the list in ascending order and define 2 counters
  178.                     int g1 = 0, g2 = 0;
  179.                    
  180.                     for (int s = 0; s < k; s++) //go through list getting the first K elements from the list
  181.                     {                        
  182.                         if (Int32.Parse(myAL[s].ToString().Last().ToString()) < 5) //Vote for which gesture it is
  183.                             g1++;
  184.                         else
  185.                             g2++;
  186.                     }
  187.                    
  188.                     if (g1 > g2)//Result from votes
  189.                     {
  190.                         Console.Write("Image number " + j + " is gesture 1");
  191.                        
  192.                         if (j < 5) //Displaying results for easier reading of the errors
  193.                         {
  194.                             Console.WriteLine(" Correct");
  195.                             correctness++;
  196.                         }
  197.                         else
  198.                             Console.WriteLine(" Wrong");
  199.                     }
  200.                     else
  201.                     {
  202.                         Console.Write("Image number " + j + " is gesture 2");
  203.                         if (j >= 5)
  204.                         {
  205.                             Console.WriteLine(" Correct");
  206.                             correctness++;
  207.                         }
  208.                         else
  209.                             Console.WriteLine(" Wrong");
  210.                     }
  211.                 }                
  212.                 Console.WriteLine("Corect " + correctness + "/" + array.Length + " times!");//Display the accuracy
  213.             }
  214.         }
  215.  
  216.         static void Main(string[] args)
  217.         {            
  218.             Mat input = CvInvoke.Imread("C://Users//Miro//Desktop//matching//input.jpg", LoadImageType.Grayscale); // LoadImageType.Grayscale
  219.             Mat[] dataBase = new Mat[10];
  220.  
  221.             for (int i = 0; i < dataBase.Length; i++)
  222.             {
  223.                 dataBase[i] = CvInvoke.Imread("C://Users//Miro//Desktop//matching//" + i + ".jpg", LoadImageType.Grayscale);
  224.             }
  225.  
  226.             #region "Version 1.0"
  227.             //Mat rightMatch = CvInvoke.Imread("C://Users//Miro//Desktop//matching//1.jpg", LoadImageType.Grayscale);
  228.             //Mat wrongMatch = CvInvoke.Imread("C://Users//Miro//Desktop//matching//2.jpg", LoadImageType.Grayscale);
  229.  
  230.             //long myMatch;
  231.             //Program ci = new Program();
  232.             //myMatch = ci.dataBaseMatching(input, dataBase, dataBase.Length);
  233.  
  234.             //int myMatch1;
  235.             //int myMatch2;
  236.             //myMatch1 = ci.matching(input, images[1], ci.rightMatchResult);
  237.             //myMatch2 = ci.matching(input, images[2], ci.wrongMatchResult);
  238.             //myMatch1 = ci.matching(input, images[1]);
  239.             //myMatch2 = ci.matching(input, images[2]);
  240.  
  241.             //myMatch = ci.matching(input, images[0]);
  242.             //Console.WriteLine("difference is " + myMatch);
  243.  
  244.             //Console.WriteLine("difference between rightMatch and input is " + myMatch1);
  245.             //Console.WriteLine("difference between wrongMatch and input is " + myMatch2);
  246.  
  247.             //Leave one out cross-validation(LOOCV) method
  248.             //LOOCV(dataBase);
  249.             #endregion
  250.             #region "Version 1.1"
  251.             //K nearest
  252.             KNearest(input, dataBase, 3);
  253.  
  254.             //LOOCV2 method for Knearest
  255.             //LOOCV2(dataBase, 1);
  256.             #endregion
  257.             Console.ReadLine();
  258.         }
  259.     }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement