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

问题描述 投票:0回答:1

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

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

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);
}
c# image bitmap
1个回答
0
投票

我在C#中有一个定制的图像比较器。

它比较2个图像,忽略品红色像素(比较时可以使用洋红色作为MASK区域忽略),并在新图像中将不同像素标记为蓝色

////////////////////// VARIABLES //////////////////////////

    private string pathReferenceImg;
    private string pathTestImg;
    private FileInfo fReferenceFile;
    private FileInfo fTestFile;
    private Bitmap referenceImage;
    private Bitmap testImage;
    private int areaToCompareWidth;
    private int areaToCompareHeight;
    public int xMinAreaToCompare = 0;
    public int yMinAreaToCompare = 0;
    public int pixelDifferenceQuantity = 0;
    public List<Point> differentPixelsList = new List<Point>();
    private int[] rgbArrayTestImgWithReferenceImgPink;
    private int tolerance = 15;
    public bool result = false;

//////////////////////代码//////////////////////////

    public void compareFiles(string pathReferenceImg, string pathTestImg)
    {
        fReferenceFile = new FileInfo(pathReferenceImg);
        fTestFile = new FileInfo(pathTestImg);
        referenceImage = new Bitmap(pathReferenceImg);
        testImage = new Bitmap(pathTestImg);
        areaToCompareWidth = referenceImage.Width;
        areaToCompareHeight = referenceImage.Height;
            while (xMinAreaToCompare < areaToCompareWidth)
            {
                Color colorRef = referenceImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
                Color colorTest = testImage.GetPixel(xMinAreaToCompare, yMinAreaToCompare);
                //Magenta = 255R,255B,0G
                if (colorRef.ToArgb() != Color.Magenta.ToArgb())
                {
                    if (colorRef != colorTest)
                    {
                        pixelDifferenceQuantity++;
                        differentPixelsList.Add(new Point(xMinAreaToCompare, yMinAreaToCompare));
                    }
                }

                yMinAreaToCompare ++;
                if (yMinAreaToCompare == areaToCompareHeight)
                {
                    xMinAreaToCompare ++;
                    yMinAreaToCompare = 1;
                }
            }
            if (pixelDifferenceQuantity >= tolerance)
            {
                Bitmap resultImage = new Bitmap(testImage);
                foreach (Point pixel in differentPixelsList)
                {
                    resultImage.SetPixel(pixel.X, pixel.Y, Color.Blue);
                }
                resultImage.Save(pathTestImg.Replace("TestFolder", "ResultFolder"));
            }
            else
            {
                result = true;
            }
    }

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.