如何将 Image 对象与 C# .NET 进行比较? [英] How to compare Image objects with C# .NET?
本文介绍了如何将 Image 对象与 C# .NET 进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以用 C# 比较两个 Image
对象吗?例如,检查它们是否相等,甚至更好地检查它们的像素有多相似?
Can we compare two Image
objects with C#? For example, check whether they are equal, or even better check how similar are their pixels?
如果可能,怎么做?
推荐答案
您可以使用一组名为 TestApi,这是一个帮助单元测试的开源库.其中一种 API 称为 Visual Verification API,并且它完全符合您的需要 - 它可以比较两个图像并告诉您它们是否相等:
You can use a set of tools called TestApi, which is an open-source library to aid unit testing. One of such API is called Visual Verification API, and it does exactly what you need - it can compare two images and tell you if they are equal:
// 1. Capture the actual pixels from a given window
Snapshot actual = Snapshot.FromRectangle(new Rectangle(0, 0, 100, 100));
// 2. Load the reference/master data from a previously saved file
Snapshot expected = Snapshot.FromFile("Expected.png"));
// 3. Compare the actual image with the master image
// This operation creates a difference image. Any regions which are identical in
// the actual and master images appear as black. Areas with significant
// differences are shown in other colors.
Snapshot difference = actual.CompareTo(expected);
// 4. Configure the snapshot verifier - It expects a black image with zero tolerances
SnapshotVerifier v = new SnapshotColorVerifier(Color.Black, new ColorDifference());
// 5. Evaluate the difference image
if (v.Verify(difference) == VerificationResult.Fail)
{
// Log failure, and save the diff file for investigation
actual.ToFile("Actual.png", ImageFormat.Png);
difference.ToFile("Difference.png", ImageFormat.Png);
}
这篇关于如何将 Image 对象与 C# .NET 进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文