public void BhattacharyyaTest2() { double[] x = { 2, 0, 0 }; double[] y = { 1, 0, 0 }; double[,] covX = { { 2, 3, 0 }, { 3, 0, 0 }, { 0, 0, 0 } }; double[,] covY = { { 2, 1, 0 }, { 1, 0, 0 }, { 0, 0, 0 } }; // Run actual test double expected = 0.1438410362258904; double actual = new Bhattacharyya().Distance(x, covX, y, covY); Assert.AreEqual(expected, actual, 1e-6); Assert.IsFalse(Double.IsNaN(actual)); }
public void IndexFiles(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { List <BhattacharyyaRecord> listOfRecords = new List <BhattacharyyaRecord>(); Double[,] normalizedHistogram = new double[16, 16]; int totalFileCount = imageFiles.Length; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Image img = Image.FromFile(fi.FullName)) { normalizedHistogram = Bhattacharyya.CalculateNormalizedHistogram(img); } BhattacharyyaRecord record = new BhattacharyyaRecord { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, NormalizedHistogram = MultiToSingle(normalizedHistogram) }; listOfRecords.Add(record); IndexBgWorker.ReportProgress(i); } BinaryAlgoRepository <List <BhattacharyyaRecord> > repo = new BinaryAlgoRepository <List <BhattacharyyaRecord> >(); repo.Save(listOfRecords); }
public void BhattacharyyaTest1() { double[] histogram1 = { 0.1, 0.5, 0.4 }; double[] histogram2 = { 0.7, 0.2, 0.1 }; double expected = 0.468184902444219; double actual = new Bhattacharyya().Distance(histogram1, histogram2); Assert.AreEqual(expected, actual, 1e-10); Assert.IsFalse(Double.IsNaN(actual)); }
public void BhattacharyyaTest4() { double[,] X = { { 0.20, 0.52 }, { 1.52, 2.53 }, { 7.21, 0.92 }, }; double expected = 0.0; double actual = new Bhattacharyya().Distance(X, X); Assert.AreEqual(expected, actual, 1e-10); Assert.IsFalse(Double.IsNaN(actual)); }
private void btnImageCompare_Click(object sender, EventArgs e) { string orgImagePath = lblImage1Path.Text; string dupImagePath = lblImage2Path.Text; bool isFirstImageEmpty = string.IsNullOrWhiteSpace(orgImagePath); bool isSecondImageEmpty = string.IsNullOrWhiteSpace(dupImagePath); if (isFirstImageEmpty || isSecondImageEmpty) { MessageBox.Show("Please select images before compare"); } bool ispHashSelected = cbCompareAlgorithm.Text == "pHash"; bool isBhattacharyyaSelected = cbCompareAlgorithm.Text == "Bhattacharyya"; if (ispHashSelected) { var percentage = ImageHashing.Similarity(orgImagePath, dupImagePath); percentage = Math.Round(percentage, 3); txtPerDiff.Text = percentage.ToString(); } else if (isBhattacharyyaSelected) { Image img1 = Image.FromFile(orgImagePath); Image img2 = Image.FromFile(dupImagePath); var percentage = Bhattacharyya.BhattacharyyaDifference(img1, img2); percentage = percentage * 100; percentage = Math.Round(percentage, 3); txtPerDiff.Text = percentage.ToString(); } else { int imageAlgoIndex = cbCompareAlgorithm.SelectedIndex; ErrorMetric imageAlgo = (ErrorMetric)imageAlgoIndex; MagickImage orgImage = new MagickImage(orgImagePath); MagickImage dupImage = new MagickImage(dupImagePath); var percentage = orgImage.Compare(dupImage, imageAlgo); txtPerDiff.Text = percentage.ToString(); orgImage.Dispose(); dupImage.Dispose(); } }
public void IndexFilesAsync(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { ConcurrentBag <BhattacharyyaRecord> listOfRecords = new ConcurrentBag <BhattacharyyaRecord>(); Double[,] normalizedHistogram = new double[16, 16]; int totalFileCount = imageFiles.Length; int i = 0; long nextSequence; //In the class scope: Object lockMe = new Object(); Parallel.ForEach(imageFiles, currentImageFile => { var fi = currentImageFile; using (Image img = Image.FromFile(fi.FullName)) { normalizedHistogram = Bhattacharyya.CalculateNormalizedHistogram(img); } lock (lockMe) { nextSequence = i++; } BhattacharyyaRecord record = new BhattacharyyaRecord { Id = nextSequence, ImageName = fi.Name, ImagePath = fi.FullName, NormalizedHistogram = MultiToSingle(normalizedHistogram) }; listOfRecords.Add(record); IndexBgWorker.ReportProgress(i); }); BinaryAlgoRepository <List <BhattacharyyaRecord> > repo = new BinaryAlgoRepository <List <BhattacharyyaRecord> >(); repo.Save(listOfRecords.ToList()); }
public void BhattacharyyaTest() { double[,] X = { { 0.20, 0.52 }, { 1.52, 2.53 }, { 7.21, 0.92 }, }; double[,] Y = { { 9.42, 5.21 }, { 1.12, 3.14 }, { 5.21, 2.12 }, }; double expected = 0.45095821066601938; double actual = new Bhattacharyya().Distance(X, Y); Assert.AreEqual(expected, actual, 1e-10); Assert.IsFalse(Double.IsNaN(actual)); }
private void ComputeDistances(FlowProfile profile1, FlowProfile profile2) { var measuresTable = new DataTable(); measuresTable.Columns.Add("Target", typeof(string)); measuresTable.Columns.Add("Count 1", typeof(double)); measuresTable.Columns.Add("Count 2", typeof(double)); measuresTable.Columns.Add("Distance", typeof(double)); foreach (var m1 in profile1) { if (profile2.TryGetValue(m1.Key, out var m2)) { var b = new Bhattacharyya(); var dist = b.Distance(m1.Value.Samples.ToArray(), m2.Samples.ToArray()); measuresTable.Rows.Add(m1.Key, m1.Value.Samples.Count, m2.Samples.Count, dist); } } Console.WriteLine("Distances:"); ConsoleTableBuilder.From(measuresTable) .WithFormat(ConsoleTableBuilderFormat.MarkDown) .ExportAndWriteLine(); }
private void btnImageCompare_Click(object sender, EventArgs e) { string pictureDirectory = DirectoryHelper.PictureDirectory; if (string.IsNullOrWhiteSpace(pictureDirectory)) { MessageBox.Show("Can't find picture directory, this form can't work!"); return; } string OrginalFile = Path.Combine(pictureDirectory, "Original.jpg"); MagickImage orgImage = null; MagickImage dupImage = null; DirectoryInfo di = new DirectoryInfo(pictureDirectory); Dictionary <string, double> imageCompareData = new Dictionary <string, double>(); foreach (FileInfo fi in di.EnumerateFiles()) { string filePath = fi.FullName; string fileName = fi.Name; if (filePath == OrginalFile) { continue; } bool ispHashSelected = cbCompareAlgorithm.Text == "pHash"; bool isBhattacharyyaSelected = cbCompareAlgorithm.Text == "Bhattacharyya"; if (ispHashSelected) { var percentage = ImageHashing.Similarity(OrginalFile, filePath); percentage = Math.Round(percentage, 3); imageCompareData.Add(fileName, percentage); } else if (isBhattacharyyaSelected) { Image img1 = Image.FromFile(OrginalFile); Image img2 = Image.FromFile(filePath); var percentage = Bhattacharyya.BhattacharyyaDifference(img1, img2); percentage = (1 - percentage) * 100; percentage = Math.Round(percentage, 3); imageCompareData.Add(fileName, percentage); } else { orgImage = new MagickImage(OrginalFile); int imageAlgoIndex = cbCompareAlgorithm.SelectedIndex; ErrorMetric imageAlgo = (ErrorMetric)imageAlgoIndex; dupImage = new MagickImage(filePath); var percentage = orgImage.Compare(dupImage, imageAlgo); percentage = Math.Round(percentage, 3); imageCompareData.Add(fileName, percentage); } } var bl = new DictionaryBindingList <string, double>(imageCompareData); dataGridView1.DataSource = bl; if (orgImage != null) { orgImage.Dispose(); } if (dupImage != null) { dupImage.Dispose(); } }