0

我有以下代码比较两个图像并输出有多少像素不同。我试图弄清楚如何让它输出像素的 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");  
        }

这些是图像,应该在大图像的中间找到黑色的小方块: 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

0

拥有一个存储所有错误像素的列表怎么样?

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());
}
于 2013-05-06T03:01:10.810 回答