This commit is contained in:
Nikolaj Olsson 2020-05-21 08:12:18 +02:00
parent bee7fedae5
commit 18fee311ce
2 changed files with 59 additions and 0 deletions

View File

@ -305,6 +305,41 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
{
AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize);
}
else
{
// allow for error %
var smallestDifference = int.MaxValue;
BinaryOcrBitmap hit = null;
foreach (var compareItem in bicDb.CompareImages)
{
if (compareItem.Width == _autoDetectFontBob.Width && compareItem.Height == _autoDetectFontBob.Height) // precise math in size
{
if (Math.Abs(compareItem.NumberOfColoredPixels - _autoDetectFontBob.NumberOfColoredPixels) < 3)
{
int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, _autoDetectFontBob);
if (dif < smallestDifference)
{
if (!BinaryOcrDb.AllowEqual(compareItem, _autoDetectFontBob))
{
continue;
}
smallestDifference = dif;
hit = compareItem;
if (dif < 3)
{
break; // foreach ending
}
}
}
}
}
if (smallestDifference < 13)
{
AutoDetectedFonts.Add($"{_subtitleFontName} {_subtitleFontSize} - diff={smallestDifference}");
}
}
bicDb = new BinaryOcrDb(null);
TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, false, true);

View File

@ -1131,6 +1131,30 @@ namespace Nikse.SubtitleEdit.Logic
return different;
}
internal static int IsBitmapsAlike(Ocr.Binary.BinaryOcrBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
{
int different = 0;
int maxDiff = bmp1.Width * bmp1.Height / 5;
for (int y = 0; y < bmp1.Height; y++)
{
var pixel = y * bmp1.Width;
for (int x = 0; x < bmp1.Width; x++)
{
if (bmp1.GetPixel(pixel) != bmp2.GetPixel(pixel))
{
different++;
}
pixel++;
}
if (different > maxDiff)
{
return different + 10;
}
}
return different;
}
internal static int IsBitmapsAlike(NikseBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
{
int different = 0;