Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using Emgu.CV;
- using Emgu.CV.CvEnum;
- using Emgu.CV.Structure;
- using Emgu.CV.UI;
- using Emgu.CV.XImgproc;
- using System.Collections;
- namespace Prototype_2
- {
- class Program
- {
- public int matching(Mat input, Mat otherMat)
- {
- Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>();
- Image<Gray, Byte> otherImg = otherMat.ToImage<Gray, Byte>();
- int match = 0;
- for (int y = 0; y < inputImg.Rows; y++)
- {
- for (int x = 0; x < inputImg.Cols; x++)
- {
- match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
- }
- }
- return match;
- }
- public long dataBaseMatching(Mat input, Mat[] array)
- {
- Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>();
- long bestMatch = 9876543210123;
- for (int i = 0; i < array.Length; i++)
- {
- int match = 0;
- Image<Gray, Byte> otherImg = array[i].ToImage<Gray, Byte>();
- for (int y = 0; y < inputImg.Rows; y++)
- {
- for (int x = 0; x < inputImg.Cols; x++)
- {
- match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
- }
- }
- //compare match values
- if (match < bestMatch)
- {
- bestMatch = match;
- }
- //Console.WriteLine(bestMatch);
- Console.WriteLine(match);
- }
- return bestMatch;
- }
- static void LOOCV(Mat[] array)
- {
- for (int j = 0; j < array.Length; j++)
- {
- Image<Gray, Byte> testImg = array[j].ToImage<Gray, Byte>();
- long bestMatch = 9876543210123;
- int picN = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (i != j)
- {
- int match = 0;
- Image<Gray, Byte> sampleImg = array[i].ToImage<Gray, Byte>();
- for (int y = 0; y < testImg.Rows; y++)
- {
- for (int x = 0; x < testImg.Cols; x++)
- {
- match += Math.Abs(testImg.Data[y, x, 0] - sampleImg.Data[y, x, 0]);
- }
- }
- if (match < bestMatch)
- {
- bestMatch = match;
- picN = i;
- }
- //Console.WriteLine(i+ "pic: " + match + "\n");
- }
- }
- //Console.WriteLine("Best: " + bestMatch +" picN= " +picN);
- //Console.WriteLine("For image"+j+" \n \n");
- Console.WriteLine("Image number " + j + " is closest to " + picN);
- }
- }
- static void KNearest(Mat input, Mat[] array, int k)
- {
- if (k % 2 == 0) //Check wether k is uneven(for majority vote)
- {
- Console.WriteLine("Only uneven numbers.");
- }
- else
- {
- Image<Gray, Byte> inputImg = input.ToImage<Gray, Byte>(); //Load input image and make a new ArrayList
- ArrayList myAL = new ArrayList();
- for (int i = 0; i < array.Length; i++) //Go trough database
- {
- int match = 0; //Reset match and load images from array
- Image<Gray, Byte> otherImg = array[i].ToImage<Gray, Byte>();
- for (int y = 0; y < inputImg.Rows; y++) //Go through pixels calculating Manhatan distance
- {
- for (int x = 0; x < inputImg.Cols; x++)
- {
- match += Math.Abs(inputImg.Data[y, x, 0] - otherImg.Data[y, x, 0]);
- }
- }
- myAL.Add(match + " pic " + i); //Add the results to the list
- }
- myAL.Sort(); //Sort the list in ascending order and define 2 counters
- int g1 = 0, g2 = 0;
- for (int s = 0; s < k; s++)//Go through list getting the first K elements from the list
- {
- Console.WriteLine(myAL[s]);
- if (Int32.Parse(myAL[s].ToString().Last().ToString()) < 5) //Vote for which gesture it is
- g1++;
- else
- g2++;
- }
- if (g1 > g2) //Result from votes
- Console.WriteLine("Gesture 1");
- else
- Console.WriteLine("Gesture 2");
- }
- }
- static void LOOCV2(Mat[] array, int k)
- {
- if (k % 2 == 0) //Check wether k is uneven(for majority vote)
- {
- Console.WriteLine("Only uneven numbers.");
- }
- else
- {
- int correctness = 0;
- for (int j = 0; j < array.Length; j++) //Go trough database (Outer)
- {
- Image<Gray, Byte> testImg = array[j].ToImage<Gray, Byte>(); //Load test image and make a new ArrayList
- ArrayList myAL = new ArrayList();
- for (int i = 0; i < array.Length; i++) //Go trough database (Inner)
- {
- if (i != j) //Check for match with Outer
- {
- int match = 0; //Reset match and load images from array
- Image<Gray, Byte> sampleImg = array[i].ToImage<Gray, Byte>();
- for (int y = 0; y < testImg.Rows; y++) //Go through pixels calculating Manhatan distance
- {
- for (int x = 0; x < testImg.Cols; x++)
- {
- match += Math.Abs(testImg.Data[y, x, 0] - sampleImg.Data[y, x, 0]);
- }
- }
- myAL.Add(match + " pic " + i); //Add the results to the list
- }
- }
- myAL.Sort(); //Sort the list in ascending order and define 2 counters
- int g1 = 0, g2 = 0;
- for (int s = 0; s < k; s++) //go through list getting the first K elements from the list
- {
- if (Int32.Parse(myAL[s].ToString().Last().ToString()) < 5) //Vote for which gesture it is
- g1++;
- else
- g2++;
- }
- if (g1 > g2)//Result from votes
- {
- Console.Write("Image number " + j + " is gesture 1");
- if (j < 5) //Displaying results for easier reading of the errors
- {
- Console.WriteLine(" Correct");
- correctness++;
- }
- else
- Console.WriteLine(" Wrong");
- }
- else
- {
- Console.Write("Image number " + j + " is gesture 2");
- if (j >= 5)
- {
- Console.WriteLine(" Correct");
- correctness++;
- }
- else
- Console.WriteLine(" Wrong");
- }
- }
- Console.WriteLine("Corect " + correctness + "/" + array.Length + " times!");//Display the accuracy
- }
- }
- static void Main(string[] args)
- {
- Mat input = CvInvoke.Imread("C://Users//Miro//Desktop//matching//input.jpg", LoadImageType.Grayscale); // LoadImageType.Grayscale
- Mat[] dataBase = new Mat[10];
- for (int i = 0; i < dataBase.Length; i++)
- {
- dataBase[i] = CvInvoke.Imread("C://Users//Miro//Desktop//matching//" + i + ".jpg", LoadImageType.Grayscale);
- }
- #region "Version 1.0"
- //Mat rightMatch = CvInvoke.Imread("C://Users//Miro//Desktop//matching//1.jpg", LoadImageType.Grayscale);
- //Mat wrongMatch = CvInvoke.Imread("C://Users//Miro//Desktop//matching//2.jpg", LoadImageType.Grayscale);
- //long myMatch;
- //Program ci = new Program();
- //myMatch = ci.dataBaseMatching(input, dataBase, dataBase.Length);
- //int myMatch1;
- //int myMatch2;
- //myMatch1 = ci.matching(input, images[1], ci.rightMatchResult);
- //myMatch2 = ci.matching(input, images[2], ci.wrongMatchResult);
- //myMatch1 = ci.matching(input, images[1]);
- //myMatch2 = ci.matching(input, images[2]);
- //myMatch = ci.matching(input, images[0]);
- //Console.WriteLine("difference is " + myMatch);
- //Console.WriteLine("difference between rightMatch and input is " + myMatch1);
- //Console.WriteLine("difference between wrongMatch and input is " + myMatch2);
- //Leave one out cross-validation(LOOCV) method
- //LOOCV(dataBase);
- #endregion
- #region "Version 1.1"
- //K nearest
- KNearest(input, dataBase, 3);
- //LOOCV2 method for Knearest
- //LOOCV2(dataBase, 1);
- #endregion
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement