I have been trying to create a program that scans documents to ensure that they have not been already pre-scanned into the system, there'll be a folder where all the previously scanned documents will reside, however I'm having difficult finding a way to make it identify the differences properly between documents, the only thing that it can accurately do is tell whether they're 100% the same or not, and that might even be subject to error in-case the lighting conditions were to differ, so far i have only tested it with 2 identical images of the same document, so they're basically just copies of each other, but each time i scan a different document or picture, all the white that is in similar places is counted towards the overall similarity, which is something i don't want, how could i make it exclude all the white pixels and only compare the black pixels ? color isn't a main problem because all the documents will be in black and white.. here is the code that i have at the moment.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ImageCompare {
class TestingClass2
{
public Double Compare(Image img1_, Image img2_)
{
//total number of pixels
int pixelNb = img1_.Width * img1_.Height;
Double percent = 100;
Bitmap resized_img2_ = ResizeBitmap((Bitmap)img2_, img1_.Width, img1_.Height);
for (int i = 0; i < img1_.Width; i++)
{
for (int j = 0; j < img1_.Height; j++)
{
percent -= ColorCompare(((Bitmap)img1_).GetPixel(i, j),
((Bitmap)resized_img2_).GetPixel(i, j)) / pixelNb;
}
}
return percent;
}
public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage((Image)result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
return result;
}
public Double ColorCompare(Color c1, Color c2)
{
return Double.Parse((Math.Abs(c1.B - c2.B) + Math.Abs(c1.R - c2.R) + Math.Abs(c1.G - c2.G)).ToString()) * 100 / (3 * 255);
}
public void showresult(Bitmap img1, Bitmap img2)
{
String degreeofSimilarity = "";
Double simcheck;
simcheck = Compare(img1, img2);
String var = "";
if (simcheck == 100)
{
var = "Identical";
}
else if (simcheck >= 95)
{
var = "Almost Identical";
}else if (simcheck <= 85)
{
var = "Very Similar";
}else if (simcheck <= 50)
{
var = "Slightly Similar";
}else if (simcheck <= 30)
{
var = "Not Similar";
}else
{
var = "UNKNOWN ERROR";
}
degreeofSimilarity = var;
MessageBox.Show("Comparison Result is: " + simcheck + " // - The images are " + degreeofSimilarity);
}
}
}
Looking forward to hearing your helpful answers, if you're confused about any part please feel free to ask me down below, aside from that, the code above is only the comparison part.