2024-04-25 19:49:36 发布
网友
我想创建一个程序,比较两个图像。它必须包含在2个文件夹中,每个文件夹中都有一对图片,并且一个文件夹中的每个图片在另一个文件夹中有一对(具有相同的文件名)例如:folder1有3个图片:[a.png,b.png,c.png],folder2有3个图片:[a.png,b.png,c.png]。我想把folder1/a和folder2/a比较一下,我不知道从哪里开始。在
你可能不想比较图像和图像之间的差异
最简单的方法是使用PIL比较两个图像。PIL有一个histogram函数,你可以用它来得到两幅图像的直方图,然后得到RMS(均方根)
histogram
import ImageChops import math, operator def rmsdiff(im1, im2): "Calculate the root-mean-square difference between two images" h = ImageChops.difference(im1, im2).histogram() # calculate rms return math.sqrt(reduce(operator.add, map(lambda h, i: h*(i**2), h, range(256)) ) / (float(im1.size[0]) * im1.size[1]))
从http://effbot.org/zone/pil-comparing-images.htm复制
一旦你有了一个返回rmsdiff的函数,你就可以在另一个遍历所有文件的函数中使用它
这是一个C解决方案,它使用二进制文件比较来比较文件。然而,图像可能包含不同的元信息,即使图片看起来相同。因此,首先将图像作为图像而不是仅作为文件打开进行比较是合适的。在
public void CompareImages() { var dir1 = new DirectoryInfo(@"C:\path1"); FileInfo[] files1 = dir1.GetFiles("*.png"); var dir2 = new DirectoryInfo(@"C:\path2"); FileInfo[] files2 = dir2.GetFiles("*.png"); Dictionary<string, FileInfo> files2Dict = files2.ToDictionary(f => f.Name); foreach (FileInfo f1 in files1) { FileInfo f2; bool equal = true; if (files2Dict.TryGetValue(f1.Name, out f2) && f1.Length == f2.Length) { byte[] image1 = GetFileBytes(f1); byte[] image2 = GetFileBytes(f2); for (int i = 0; i < image1.Length; i++) { if (image1[i] != image2[i]) { equal = false; break; } } } else { equal = false; } Console.WriteLine(f1.Name + ": " + (equal ? "Images are equal" : "Images are NOT equal")); } } private static byte[] GetFileBytes(FileInfo f) { using (FileStream stream = f.OpenRead()) { byte[] buffer = new byte[f.Length]; stream.Read(buffer, 0, (int)f.Length); return buffer; } }
C,Python还是C? 看看:
How to compare Image objects with C# .NET?
Compare two images the python/linux way
祝你好运
你可能不想比较图像和图像之间的差异
最简单的方法是使用PIL比较两个图像。PIL有一个
histogram
函数,你可以用它来得到两幅图像的直方图,然后得到RMS(均方根)从http://effbot.org/zone/pil-comparing-images.htm复制
一旦你有了一个返回rmsdiff的函数,你就可以在另一个遍历所有文件的函数中使用它
^{pr2}$这是一个C解决方案,它使用二进制文件比较来比较文件。然而,图像可能包含不同的元信息,即使图片看起来相同。因此,首先将图像作为图像而不是仅作为文件打开进行比较是合适的。在
C,Python还是C? 看看:
How to compare Image objects with C# .NET?
Compare two images the python/linux way
祝你好运
相关问题 更多 >
编程相关推荐