public static void MakeSquarePowerOfTwo(this BoxData data, int stride) { if ((data.Rightmost - data.LeftMost) % stride == 0) { return; } if ((data.Lowest - data.Highest) % stride == 0) { return; } int amount = (data.Rightmost - data.LeftMost) / stride; int number = stride * (amount + 1); number = number - (data.Rightmost - data.LeftMost); for (int i = 0; i < number; i++) { if (data.LeftMost - 1 > 0) { data.LeftMost--; } else if (data.Rightmost + 1 < 512) { data.Rightmost++; } } amount = (data.Lowest - data.Highest) / stride; number = stride * (amount + 1); number = number - (data.Lowest - data.Highest); for (int i = 0; i < number; i++) { if (data.Highest - 1 > 0) { data.Highest--; } else if (data.Lowest + 1 < 512) { data.Lowest++; } } }
/// <summary> /// /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <param name="size"></param> private Tuple <Bitmap, bool> FindDifferences(Image first, Image second, int size) { Bitmap C = new Bitmap(first); Bitmap R = new Bitmap(second); BoxData tbd = new BoxData(Boxdata); List <List <double> > diffs = Differences(C, R); Bitmap output = new Bitmap(size, size); unsafe { BitmapData bitmapData = output.LockBits(new Rectangle(0, 0, output.Width, output.Height), ImageLockMode.ReadWrite, output.PixelFormat); int bytesPerPixel = Image.GetPixelFormatSize(output.PixelFormat) / 8; int heightInPixels = bitmapData.Height; int widthInBytes = bitmapData.Width * bytesPerPixel; byte *ptrFirstPixel = (byte *)bitmapData.Scan0; Boxdata = new BoxData(); Parallel.For(0, heightInPixels, y => { byte *currentLine = ptrFirstPixel + y * bitmapData.Stride; for (int x = 0; x < widthInBytes; x = x + bytesPerPixel) { int value = (int)Math.Round(diffs[x / 4][y]); value = value > 10 ? 255 : 0; //diffs[x/4][y] = value; currentLine[x + 3] = 255; currentLine[x + 1] = (byte)value; currentLine[x + 2] = (byte)value; currentLine[x] = (byte)value; if (value != 255) { continue; } if (x / 4 < Boxdata.LeftMost) { Boxdata.LeftMost = x / 4; } else if (x / 4 > Boxdata.Rightmost) { Boxdata.Rightmost = x / 4; } if (y < Boxdata.Highest) { Boxdata.Highest = y; } else if (y > Boxdata.Lowest) { Boxdata.Lowest = y; } } }); Boxdata.MakeSquarePowerOfTwo(16); output.UnlockBits(bitmapData); } bool found = IsolateFaces(first, diffs); return(new Tuple <Bitmap, bool>(output, found)); }