I want to write a simple c# windows forms application in .NET 6/7 where I can choose a source folder containing image files and a target folder containing image files. The main functionality is to copy all image files from the source folder to the target folder, BUT ONLY if there is NO image file in the target folder that matches the source file.
But what I want is not to compare just by checking if the file size is the same, but comparing the actual images if they match a certain percentage.
So if I resize a picture to 50% of its size, the algorithm should still recognize the 100% version as matching the 50% version.
I just wonder if there is a way to implement this in a resource-saving way.
I already did an implementation myself by using Emgu.CV
NuGet package (current version 4.6.0.5131) but it is way too slow if your target folder contains e.g. 5000 image files and your source folder too.
private bool TargetFolderContains(Image<Gray, Byte> sourceImage)
{
var contains = false;
Parallel.ForEach(_targetFiles, targetFile => //_targetFiles is string[] containing each filepath
{
Image<Gray, Byte> targetImage = new Image<Gray, Byte>(targetFile);
var resizedTargetImage = targetImage.Resize(sourceImage.Width, sourceImage.Height, Emgu.CV.CvEnum.Inter.Linear); //same size because of exception if not same size
var resultImage = sourceImage.AbsDiff(resizedTargetImage);
double diff = CvInvoke.CountNonZero(resultImage);
diff = (diff / (resizedTargetImage.Width * resizedTargetImage.Height)) * 100; // gets the percentage
if (diff < 95)
{
contains = true;
}
});
return contains;
}
So u can imagine how slow this is when u have 5000 files in source and target folder. The resultImage is actually an image you can save...
I just wonder if there is an elegant way to solve this in c#.
I guess python would be the best choice for things like that, but I'm not into python and would be glad if there is a c# solution :P