0

i have the following code which compares two images and outputs how many pixels are different. I'm trying to figure out how to make it output the x and y coordinates of the pixels. Anyone have an idea?

        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");  
        }

these are the images, and the small black square should be found in the middleish of the big image: enter image description here enter image description here

user2078674
  • 165
  • 2
  • 2
  • 14
  • Not really related to your question, but if you're concerned about performance, there are better options (see answer 1 pointers section): http://stackoverflow.com/questions/12170894/drawing-image-with-additive-blending – Mike Trusov May 06 '13 at 03:23
  • Why are you turning colors into strings? String comparisons are slow. Conversion of colors into strings is slow. This code is full of smells. – Ben Voigt May 06 '13 at 03:26

1 Answers1

0

How about having a List that stores all the wrong pixels?

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());
}
Ryan
  • 1,797
  • 12
  • 19
  • Right, except that he wants to save the coordinates, not the color. Use `new Point(i, j)` to add to a list of Points. – SimpleVar May 06 '13 at 03:13
  • Thanks for helping but this just outputted x=1 through to 20.. not the coordinates but i+1, +1, +1 until it gets to 20... – user2078674 May 06 '13 at 03:41
  • Sorry all. I think im completely wrong and this code doesnt work all.. :( (my code) I'll have to start again.. ive no idea why it isnt finding the small image from inside the big image. – user2078674 May 06 '13 at 04:18