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(); }
private bool IsCharacterMatchAtPoint(int x, int y, NeighborDifference[,] searchDifferences, CharacterData characterData, float tolerance) { if (x + characterData.Difference.GetLength(1) >= searchDifferences.GetLength(1) || y + characterData.Difference.GetLength(0) >= searchDifferences.GetLength(0)) return false; if (x == 31 && y == 140) { } for (int charY = 0; charY < characterData.Difference.GetLength(0); charY++) { for (int charX = 0; charX < characterData.Difference.GetLength(1); charX++) { if (x + charX == 279 && y + charY == 192) { } NeighborDifference characterDifference = characterData.Difference[charY, charX]; NeighborDifference searchDifference = searchDifferences[y + charY, x + charX]; if ((characterDifference.HasUp && Math.Abs(characterDifference.Up - searchDifference.Up) > tolerance) || (characterDifference.HasDown && Math.Abs(characterDifference.Down - searchDifference.Down) > tolerance) || (characterDifference.HasLeft && Math.Abs(characterDifference.Left - searchDifference.Left) > tolerance) || (characterDifference.HasRight && Math.Abs(characterDifference.Right - searchDifference.Right) > tolerance)) { return false; } if (characterDifference.IsPartOfChar &&((!characterDifference.HasUp && Math.Abs(searchDifference.Up) < 0.05) || (!characterDifference.HasDown && Math.Abs(searchDifference.Down) < 0.05) || (!characterDifference.HasLeft && Math.Abs(searchDifference.Left) < 0.05) || (!characterDifference.HasRight && Math.Abs(searchDifference.Right) < 0.05))) { return false; } } } return true; }
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; }