[英]How to compare colors on image and crop the diff?
為了在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#中有一個定制的圖像比較器。
它比較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;
}
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.