private static void FindBestMatchNew(ref int index, ref int smallestDifference, ref int smallestIndex, NikseBitmap target, BinaryOcrDb binOcrDb, BinaryOcrBitmap bob, double maxDiff) { var bobExactMatch = binOcrDb.FindExactMatch(bob); if (bobExactMatch >= 0) { index = bobExactMatch; smallestDifference = 0; smallestIndex = bobExactMatch; return; } if (maxDiff < 0.2 || target.Width < 3 || target.Height < 5) return; int numberOfForegroundColors = bob.NumberOfColoredPixels; const int minForeColorMatch = 90; index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width && compareItem.Height == target.Height) // precise math in size { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < 3) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif < 3) { break; // foreach ending } } } } index++; } if (smallestDifference > 1) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width && compareItem.Height == target.Height) // precise math in size { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < 40) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) { break; // foreach ending } } } } index++; } } if (target.Width > 16 && target.Height > 16 && smallestDifference > 2) // for other than very narrow letter (like 'i' and 'l' and 'I'), try more sizes { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width && compareItem.Height == target.Height - 1) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } if (smallestDifference > 2) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width && compareItem.Height == target.Height + 1) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 3) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width + 1 && compareItem.Height == target.Height + 1) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 5) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width - 1 && compareItem.Height == target.Height - 1) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 5) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width - 1 == target.Width && compareItem.Height == target.Height) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 9 && target.Width > 11) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width - 2 && compareItem.Height == target.Height) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 9 && target.Width > 14) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width - 3 && compareItem.Height == target.Height) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 9 && target.Width > 14) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width == target.Width && compareItem.Height == target.Height - 3) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, target); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } if (smallestDifference > 9 && target.Width > 14) { index = 0; foreach (var compareItem in binOcrDb.CompareImages) { if (compareItem.Width - 2 == target.Width && compareItem.Height == target.Height) { if (Math.Abs(compareItem.NumberOfColoredPixels - numberOfForegroundColors) < minForeColorMatch) { int dif = NikseBitmapImageSplitter.IsBitmapsAlike(target, compareItem); if (dif < smallestDifference) { smallestDifference = dif; smallestIndex = index; if (dif == 0) break; // foreach ending } } } index++; } } } if (smallestDifference == 0) { if (smallestIndex > 200) { var hit = binOcrDb.CompareImages[smallestIndex]; binOcrDb.CompareImages.RemoveAt(smallestIndex); binOcrDb.CompareImages.Insert(0, hit); smallestIndex = 0; index = 0; } } }