如何比较图像上的颜色并裁剪差异?

为了在 Selenium 下进行视觉测试,我进行了图像比较测试(仅 2 张图像)。我使用文件大小来查看是否存在差异。但是,没有什么能告诉我哪里有这种差异,我希望能够显示图像上显示的差异。


我在考虑按颜色而不是尺寸进行比较。这对我来说似乎很复杂,特别是因为我希望图像输出显示差异(使用指定区域的裁剪)或通过提取受此差异影响的像素。您认为可以在 C# 中使用 selenium 来实现吗?目前,我试过尺寸。


public static void TestComapre()

{

    string imgPath1 = <//PATHNAME >

    string imgPath2 = <//PATHNAME >


    const int size = 1000;

    var len = new FileInfo(imgPath1).Length;

    if (len != new FileInfo(imgPath2).Length)


    var s1 = File.OpenRead(imgPath1);

    var s2 = File.OpenRead(imgPath2);


    var buf1 = new byte[size];

    var buf2 = new byte[size];


    for (int i = 0; i < len / size; i++)

    {

        s1.Read(buf1, 0, size);

        s2.Read(buf2, 0, size);

        if (CompareBuffers(buf1, buf2) == false)


            Assert.Fail();

    }

    Assert.True(true);

}


小怪兽爱吃肉
浏览 80回答 1
1回答

不负相思意

我在 C# 中有一个定制的图像比较器。它比较 2 个图像,忽略洋红色像素(您可以使用洋红色作为比较时要忽略的区域的掩码)并在新图像中将不同像素标记为蓝色///////////////////// 变量 /////////////////////////&nbsp; &nbsp; private string pathReferenceImg;&nbsp; &nbsp; private string pathTestImg;&nbsp; &nbsp; private FileInfo fReferenceFile;&nbsp; &nbsp; private FileInfo fTestFile;&nbsp; &nbsp; private Bitmap referenceImage;&nbsp; &nbsp; private Bitmap testImage;&nbsp; &nbsp; private int areaToCompareWidth;&nbsp; &nbsp; private int areaToCompareHeight;&nbsp; &nbsp; public int xMinAreaToCompare = 0;&nbsp; &nbsp; public int yMinAreaToCompare = 0;&nbsp; &nbsp; public int pixelDifferenceQuantity = 0;&nbsp; &nbsp; public List<Point> differentPixelsList = new List<Point>();&nbsp; &nbsp; private int[] rgbArrayTestImgWithReferenceImgPink;&nbsp; &nbsp; private int tolerance = 15;&nbsp; &nbsp; public bool result = false;////////////////////// 代码 //////////////////////////&nbsp; &nbsp; public void compareFiles(string pathReferenceImg, string pathTestImg)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; fReferenceFile = new FileInfo(pathReferenceImg);&nbsp; &nbsp; &nbsp; &nbsp; fTestFile = new FileInfo(pathTestImg);&nbsp; &nbsp; &nbsp; &nbsp; referenceImage = new Bitmap(pathReferenceImg);&nbsp; &nbsp; &nbsp; &nbsp; testImage = new Bitmap(pathTestImg);&nbsp; &nbsp; &nbsp; &nbsp; areaToCompareWidth = referenceImage.Width;&nbsp; &nbsp; &nbsp; &nbsp; areaToCompareHeight = referenceImage.Height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (xMinAreaToCompare < areaToCompareWidth)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color colorRef = referenceImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color colorTest = testImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Magenta = 255R,255B,0G&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (colorRef.ToArgb() != Color.Magenta.ToArgb())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (colorRef != colorTest)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pixelDifferenceQuantity++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; differentPixelsList.Add(new Point(xMinAreaToCompare, yMinAreaToCompare));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yMinAreaToCompare ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (yMinAreaToCompare == areaToCompareHeight)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xMinAreaToCompare ++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yMinAreaToCompare = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pixelDifferenceQuantity >= tolerance)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap resultImage = new Bitmap(testImage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Point pixel in differentPixelsList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultImage.SetPixel(pixel.X, pixel.Y, Color.Blue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultImage.Save(pathTestImg.Replace("TestFolder", "ResultFolder"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }希望能帮助到你。
打开App,查看更多内容
随时随地看视频慕课网APP