我有以下代码比较两个图像并输出有多少像素不同。我试图找出如何输出像素的x和y坐标。有人有想法吗?
public int count2;
public Boolean flag;
private void button1_Click(object sender, EventArgs e)
{
string img1_ref, img2_ref;
//this image is 10x10
Bitmap img1 = Properties.Resources.black;
//this image is a screenshot
Bitmap img2 = Properties.Resources.bigimg;
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
img1_ref = img1.GetPixel(i, j).ToString();
img2_ref = img2.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
{
count2++;
flag = false;
break;
}
}
}
if (flag == false)
MessageBox.Show(count2 + " wrong pixels found");
}
这些是图像,小的黑色正方形应该在大图像的中间位置找到:
答案 0 :(得分:0)
如何让List存储所有错误的像素?
List<Point> wrongPixels = new List<Point>();
[...]
if (img1_ref != img2_ref){
wrongPixels.Add(new Point(i,j));
[...]
}
[...]
for (int i=0, max=wrongPixels.Count;i<max;i++){
MessageBox.Show("Wrong pixel at: " + wrongPixels[i].ToString());
}