亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何比較圖像上的顏色并裁剪差異?

如何比較圖像上的顏色并裁剪差異?

C#
小怪獸愛吃肉 2022-12-31 13:43:59
為了在 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);}
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

我在 C# 中有一個定制的圖像比較器。


它比較 2 個圖像,忽略洋紅色像素(您可以使用洋紅色作為比較時要忽略的區域的掩碼)并在新圖像中將不同像素標記為藍色


///////////////////// 變量 /////////////////////////


    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;

            }

    }

希望能幫助到你。


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 100 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號