0

I seek to optimize the performance of my small program, which functionality relies on detecting an image which is most similar to given example. Problem is, the method that I use is really slow and could use a bit of reworking.

I also find that I cannot use Parallel.For to compute the similarity value due to the fact that the function you'll see below is already being called from Parallel.ForEach cycle. Eh.

My similarity method:

    public static double isItSame(Bitmap source, Color[,] example)
    {


        double rez = 0;
        for (int x = 20; x < 130; x += 3)
        {
            for (int y = 10; y < 140; y += 3)
            {

                Color color1 = source.GetPixel(x, y);
                rez += Math.Abs(color1.R - example[x, y].R) + Math.Abs(color1.G - example[x, y].G) + Math.Abs(color1.B - example[x, y].B);
            }
        }


        return rez;
    }

Will greatly appreciate any help to optimize this solution. My own way to optimize it was to just do x+3 instead of x++, and same for y, but it results in poor overall results. Eh.

Cowbless
  • 93
  • 12
  • 2
    GetPixel is too slow and should not be used inside a loop. You may use LockBits instead. See [this question](http://stackoverflow.com/questions/4235731/is-there-a-faster-alternative-to-gdi-getpixel). – Mehrzad Chehraz Nov 19 '16 at 19:47
  • `x += 3`...`y += 3`: do you really want to test every **third** pixel? – Dmitry Bychenko Nov 19 '16 at 19:56
  • Thanks will check it out! – Cowbless Nov 19 '16 at 19:56
  • Dmitry, yes, i know that skipping pixels is not a good idea especially because I will be checking every 9nth pixel if i do `x+3` and `y+3`. But it's the only thing that crossed my mind. – Cowbless Nov 19 '16 at 19:57

0 Answers0