private static string compareImagesToDefn(string dir, string pattern, string defFileName) { // "-r" = recursively search subdirectories for files that match pattern List <string> filenames = getFiles(dir, pattern); Console.WriteLine("Compare images to: " + defFileName); Image defImage = new Image(32, 32); defImage.LoadImage(defFileName); string output = ""; foreach (string fileName in filenames) { Console.WriteLine(fileName); Image testImage = new Image(); testImage.LoadImage(fileName); Metrics.ImageDistance id = new Metrics.ImageDistance(defImage, testImage); double distance = id.distance(Metrics.ImageDistance.HAUSDORFF); // output += defFileName.Replace(@"C:\Users\sketchers\Documents\Trunk\Data\TrainingResults\Context","") + ", "; // output += fileName.Replace(@"C:\Users\sketchers\Documents\Trunk\Data\Context Symbols\TEST","") + ", "; output += defFileName + ", "; output += fileName + ", "; output += distance + ", \n"; } return(output); }
/// <summary> /// Returns the correct CongealParameters that will yield a best-aligned shape. /// Aligns by brute-forcing through 90-degree rotations /// </summary> /// <param name="toalign">The image to align. Will not be modified (i.e., aligned) by this function</param> /// <returns>The correct number of degrees to rotate</returns> public double align(SymbolRec.Image.Image toalign) { SymbolRec.Image.Image rotated; Metrics.ImageDistance d = new Metrics.ImageDistance(_trained, toalign); int rotbest = 0; double dbest = d.ModifiedHausdorff; for (int rotation = 90; rotation < 360; rotation += 90) { rotated = toalign.rotate(DtR(rotation), toalign.WeightedCenter[0], toalign.WeightedCenter[1]); d = new Metrics.ImageDistance(_trained, rotated); double dnew = d.ModifiedHausdorff; if (dnew < dbest) { dbest = dnew; rotbest = rotation; } } return(rotbest); }
private static string compareImagesByTestFile(string dir, string pattern, List <string> defFileNames) { // "-r" = recursively search subdirectories for files that match pattern List <string> testFiles = getFiles(dir, pattern); //Set up column headings string output = "Testfile , "; foreach (string df in defFileNames) { output += df + ", "; } output += "Best Guess, minDist, \n"; foreach (string tf in testFiles) { Console.WriteLine(tf); Image testImage = new Image(); testImage.LoadImage(tf); output += tf + ", "; double minDistance = Double.MaxValue; string bestGuess = "NONE"; foreach (string df in defFileNames) { Image defImage = new Image(32, 32); defImage.LoadImage(df); Metrics.ImageDistance id = new Metrics.ImageDistance(defImage, testImage); double distance = id.distance(Metrics.ImageDistance.HAUSDORFF); if (distance < minDistance) { minDistance = distance; bestGuess = df; } output += distance + ", "; } output += bestGuess + ", " + minDistance + ", \n"; } return(output); }
/// <summary> /// Finds the amount to move each column by translating all of the columns and finding the best one. /// </summary> /// <param name="A">Image to match to</param> /// <param name="cols">Number of columns to best translation</param> /// <param name="dist">Best distance</param> public void findBestTranslation(DefinitionImage A, out int cols, out double bestDist) { double dist; bestDist = double.PositiveInfinity; cols = -1; Image translated = new Image(this); Metrics.ImageDistance id; //Go over the potential column translations int i, len = _width; for (i = 0; i < len; ++i) { //Store the translation in translated TranslateMatrix(i, ref matrix, ref translated.matrix); //dist = id.distance(Metrics.ImageDistance.DIRECTEDMODIFIEDHAUSDORFF_AB); //dist = id.distance(Metrics.ImageDistance.TANIMOTO) + id.distance(Metrics.ImageDistance.YULE); //dist = id.distance(Metrics.ImageDistance.VERTICAL); //dist = id.distance(Metrics.ImageDistance.DIRECTEDHAUSDORFF_AB); //dist = id.distance(Metrics.ImageDistance.OVERLAPPING_AB); //dist = id.distance(Metrics.ImageDistance.HORIZONTAL); //id = new Metrics.ImageDistance(translated, A.Polar); //dist = id.distance(Metrics.ImageDistance.TANIMOTO) * id.distance(Metrics.ImageDistance.YULE); id = new Metrics.ImageDistance(translated, A.PolarTransform); dist = id.distance(Metrics.ImageDistance.DIRECTEDMODIFIEDHAUSDORFF_AB); if (dist < bestDist) { bestDist = dist; cols = i; } } }
/// <summary> /// Align a shape by brute-forcing through 90-degree rotations /// and seeing which one yields the best results /// </summary> /// <param name="toalign">The shape to align (will not be mutated)</param> /// <returns>The aligned shape</returns> public Sketch.Shape align(Sketch.Shape toalign) { Sketch.Shape aligned = toalign.Clone(); SymbolRec.Image.Image i = new SymbolRec.Image.Image(_trained.Width, _trained.Height, new SymbolRec.Substrokes(aligned.SubstrokesL)); Metrics.ImageDistance d = new Metrics.ImageDistance(_trained, i); int rotbest = 0; double dbest = d.ModifiedHausdorff; for (int rotation = 90; rotation < 360; rotation += 90) { aligned.rotate(DtR(rotation)); i = new SymbolRec.Image.Image(_trained.Width, _trained.Height, new SymbolRec.Substrokes(toalign.SubstrokesL)); d = new Metrics.ImageDistance(_trained, i); double dnew = d.ModifiedHausdorff; if (dnew < dbest) { dbest = dnew; rotbest = rotation; } aligned.rotate(-DtR(rotation)); } aligned.rotate(DtR(rotbest)); return(aligned); }