/// <summary> /// Main constructor for windows form /// </summary> public MainWindow() { InitializeComponent(); //Add properties for openFileDialog openFileDialog.DefaultExt = ".tif"; openFileDialog.Title = "Open Image"; openFileDialog.Filter = "JPEG files (*.jpg)| *.jpg|TIFF files (*.tif)| *.tif|" + " All files | *.*"; //Close button Yes and No, which user click for assert recognition labelCorrect.Visible = false; buttonCorrectYes.Visible = false; buttonCorrectYes.Visible = false; this.BackColor = Color.White; //Read ready SVM and bagOfContourFragments multiSVM = BinarySave.ReadBinary(true); bagOfContourFragments = BinarySave.ReadBinary(); }
/// <summary> /// This methods only for admin, and this recompute bagOfContourFragments and svm /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonCompute_Click(object sender, EventArgs e) { //Accord.Math.Random.Generator.Seed = 1; DirectoryInfo path = new DirectoryInfo(Path.Combine(Application.StartupPath, "Resources/Res")); ///Create dictionary for train images originalTrainImages = new Dictionary <int, Bitmap>(); int j = 0; int k = 0; foreach (DirectoryInfo classFolder in path.EnumerateDirectories()) { ///Add name of folder string name = classFolder.Name; ///Upload all files in aarray FileInfo[] files = GetFilesByExtensions(classFolder, ".jpg", ".tif").ToArray(); //Shuffle objects in array Vector.Shuffle(files); //For each image complite some easy operations for (int i = 0; i < files.Length; i++) { //Uploat only train images //70% if ((i / (double)files.Length) < 0.7) { //Add file FileInfo file = files[i]; //Create image from file Bitmap image = (Bitmap)Bitmap.FromFile(file.FullName); //Use detector CannyEdgeDetector filterCanny = new CannyEdgeDetector(); //Apply changes filterCanny.ApplyInPlace(image); //Add some information of image string shortName = file.Name; int imageKey = j; //Add image to dictionary originalTrainImages.Add(j, image); //Save correct key of class for image outputsResult[j] = k; j++; } } //Change key of folder k++; } //Create teacher for svm, using Histogram Intersection var teacher = new MulticlassSupportVectorLearning <HistogramIntersection>() { //Add leaner params Learner = (param) => new SequentialMinimalOptimization <HistogramIntersection>() { //Create kernel with optimal params Kernel = new HistogramIntersection(0.25, 1), } }; //Create KMeans algr var kmodes = new KModes <byte>(numberOfContour, new Hamming()); //Create detector var detector = new FastRetinaKeypointDetector(); //Create bagOfContourFragments bagOfContourFragments = new BagOfVisualWords(numberOfContour); //Learned bagOfContourFragments bagOfContourFragments.Learn(originalTrainImages.Values.ToArray()); //For each iamge add inputs info for (int i = 0; i < originalTrainImages.Count; i++) { Bitmap image = originalTrainImages[i] as Bitmap; inputsInfo[i] = (bagOfContourFragments as ITransform <Bitmap, double[]>).Transform(image); } //Save condition of bagOfContourFragments BinarySave.WriteBinary(bagOfContourFragments); //Teach svm multiSVM = teacher.Learn(inputsInfo, outputsResult); //Save condition of svm BinarySave.WriteBinary(multiSVM); }