public double GetDistance(GistData sample1, GistData sample2, double alpha) { var distance = 0.0; // assume sample1 and sample2 are valid i.e. same length //for (var i = 0; i < sample1.Count; i++) { var tempD = sample1.distance - sample2.distance; var tempH = GetEditDistance(sample1.metadata, sample2.metadata); distance += alpha * tempD * tempD + (1 - alpha) * tempH; Console.WriteLine(sample1.filename + "-" + sample2.filename + " tempD: " + tempD + " tempH: " + tempH + " distance :" + distance); } return(distance); }
private void FSI_Button_Click(object sender, RoutedEventArgs e) { Gist gist = new Gist(); var refgistdata = new GistData(); //System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "Locate the file to compare"; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == System.Windows.Forms.DialogResult.OK) // Test result. { string refImage = openFileDialog1.FileName; Image i = new Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = new Uri(refImage, UriKind.Relative); src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); i.Source = src; i.Stretch = Stretch.Uniform; //int q = src.PixelHeight; // Image loads here sp0.Children.Clear(); // clear previous image sp0.Children.Add(i); gist.defaultOutputDirectory = defaultOutputDirectory; System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; gist.LMgist(refImage, true); refgistdata = gist.refImgGistData; globalrefgistdata = refgistdata; //save it for KNN Console.WriteLine("Done reading reference"); //Now compare refImage with precomputed gists FolderBrowserDialog infbd = new FolderBrowserDialog(); infbd.Description = "Select the directory that contains precomputed Image gists"; DialogResult result1 = infbd.ShowDialog(); if (result1 == System.Windows.Forms.DialogResult.OK) // Test result1. { string[] files = Directory.GetFiles(infbd.SelectedPath); List <GistData> gistdataforSort = new List <GistData>(); foreach (string path in files) { if (File.Exists(path)) { // This path is a file var gistdata = gist.JsonRead(path); var distance = gist.FindDistance(refgistdata, gistdata); var gistdataWithoutValue = new GistData(distance, gistdata.filename, gistdata.metadata); gistdataforSort.Add(gistdataWithoutValue); using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:/result.txt", true)) { file.WriteLine("{0} {1} {2}", distance, gistdata.filename, gistdata.metadata); } Console.WriteLine("D= {0} File= {1}", gist.FindDistance(refgistdata, gistdata), gistdata.filename); } else { Console.WriteLine("{0} is not a valid file or directory.", path); } } Console.WriteLine("Done FSI"); globalgistdatalist = gistdataforSort.OrderBy(o => o.distance).ToList(); //Now load the files in the stack panel for (int j = 0; j < globalgistdatalist.Count; j++) { if (j > 0) { if (globalgistdatalist[j].filename == globalgistdatalist[j - 1].filename && globalgistdatalist[j].distance == globalgistdatalist[j - 1].distance) { continue; } } Image img = new Image(); BitmapImage source = new BitmapImage(); source.BeginInit(); source.UriSource = new Uri(globalgistdatalist[j].filename, UriKind.Relative); source.CacheOption = BitmapCacheOption.OnLoad; source.DecodePixelWidth = 200; source.DecodePixelHeight = 200; source.EndInit(); img.Source = source; img.Stretch = Stretch.UniformToFill; int qq = source.PixelHeight; // Image loads here System.Windows.Controls.ToolTip tooltip = new System.Windows.Controls.ToolTip(); tooltip.Content = globalgistdatalist[j].metadata; img.ToolTip = tooltip; //sp1.Children.Add(img); //lv1.Items.Add(img); //imgrid.Source = source; //System.Windows.Controls.CheckBox checkbox = new System.Windows.Controls.CheckBox(); //checkbox.HorizontalAlignment = HorizontalAlignment.Left; //checkbox.VerticalAlignment = VerticalAlignment.Bottom; System.Windows.Controls.RadioButton radio1like = new System.Windows.Controls.RadioButton(); System.Windows.Controls.RadioButton radio2dislike = new System.Windows.Controls.RadioButton(); radio1like.VerticalAlignment = VerticalAlignment.Bottom; radio1like.Content = "Yes"; radio2dislike.VerticalAlignment = VerticalAlignment.Bottom; radio2dislike.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; radio2dislike.Content = "No"; var grid = new Grid(); grid.Children.Add(img); grid.Children.Add(radio1like); grid.Children.Add(radio2dislike); lv1.Items.Add(grid); //if (checkbox.IsChecked == true) //{ // globalgistdatalist[j].IsSimilar = 1; // imgrid.Source = source; //} //else // globalgistdatalist[j].IsSimilar = -1; if (radio1like.IsChecked == true) { globalgistdatalist[j].IsSimilar = 1; } else if (radio2dislike.IsChecked == true) { globalgistdatalist[j].IsSimilar = -1; } Console.WriteLine("FSI: " + globalgistdatalist[j].filename + " " + globalgistdatalist[j].IsSimilar + " distance :" + globalgistdatalist[j].distance); } } System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } }