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(); } }
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(); } }