public void Analyze() { if(Difference == null) Difference = new NeighborDifference[Image.Height, Image.Width]; LockBitmap lockBitmap = new LockBitmap(Image); lockBitmap.LockBits(); for (int y = 0; y < lockBitmap.Height; y++) { for (int x = 0; x < lockBitmap.Width; x++) { int up = y - 1; int down = y + 1; int left = x - 1; int right = x + 1; Color pixel = lockBitmap.GetPixel(x, y); NeighborDifference neighborDifference = new NeighborDifference(); bool isCharData = IsPixelCharData(x, y); neighborDifference.IsPartOfChar = isCharData; if (up < 0 /*|| IsPixelCharData(x, up) != isCharData*/) { neighborDifference.HasUp = false; } else { neighborDifference.HasUp = IsPixelCharData(x, up) == isCharData; Color upPixel = lockBitmap.GetPixel(x, up); neighborDifference.Up = GetPixelDistance(pixel, upPixel); } if (down >= lockBitmap.Height/*lockBitmap.Height || IsPixelCharData(x, down) != isCharData*/) { neighborDifference.HasDown = false; } else { neighborDifference.HasDown = IsPixelCharData(x, down) == isCharData; Color downPixel = lockBitmap.GetPixel(x, down); neighborDifference.Down = GetPixelDistance(pixel, downPixel); } if (left < 0 /*|| IsPixelCharData(left, y) != isCharData*/) { neighborDifference.HasLeft = false; } else { neighborDifference.HasLeft = IsPixelCharData(left, y) == isCharData; Color leftPixel = lockBitmap.GetPixel(left, y); neighborDifference.Left = GetPixelDistance(pixel, leftPixel); } if (right >= lockBitmap.Width/* || IsPixelCharData(right, y) != isCharData*/) { neighborDifference.HasRight = false; } else { neighborDifference.HasRight = IsPixelCharData(right, y) == isCharData; Color rightPixel = lockBitmap.GetPixel(right, y); neighborDifference.Right = GetPixelDistance(pixel, rightPixel); } Difference[y, x] = neighborDifference; } } lockBitmap.UnlockBits(); }
public NeighborDifference[,] GetNeighborDifferences(Bitmap bitmap) { LockBitmap lockBitmap = new LockBitmap(bitmap); lockBitmap.LockBits(); NeighborDifference[,] retDifference = new NeighborDifference[bitmap.Height, bitmap.Width]; Color[,] colors = new Color[lockBitmap.Height, lockBitmap.Width]; for (int y = 0; y < lockBitmap.Height; y++) { for (int x = 0; x < lockBitmap.Width; x++) { colors[y, x] = lockBitmap.GetPixel(x, y); } } for (int y = 0; y < lockBitmap.Height; y++) { for (int x = 0; x < lockBitmap.Width; x++) { int up = y - 1; int down = y + 1; int left = x - 1; int right = x + 1; Color pixel = colors[y, x]; NeighborDifference neighborDifference = new NeighborDifference(); if (up < 0) { neighborDifference.HasUp = false; } else if (up > 0) { neighborDifference.Up = retDifference[up, x].Down; } else { Color upPixel = colors[up, x]; neighborDifference.Up = GetPixelDistance(pixel, upPixel); } if (down >= lockBitmap.Height) { neighborDifference.HasDown = false; } else { Color downPixel = colors[down, x]; neighborDifference.Down = GetPixelDistance(pixel, downPixel); } if (left < 0) { neighborDifference.HasLeft = false; } else if (left > 0) { neighborDifference.Left = retDifference[y, left].Right; } else { Color leftPixel = colors[y, left]; neighborDifference.Left = GetPixelDistance(pixel, leftPixel); } if (right >= lockBitmap.Width) { neighborDifference.HasRight = false; } else { Color rightPixel = colors[y, right]; neighborDifference.Right = GetPixelDistance(pixel, rightPixel); } retDifference[y, x] = neighborDifference; } } lockBitmap.UnlockBits(); return retDifference; }