Optimize BIC (minor)

This commit is contained in:
Nikolaj Olsson 2020-05-23 08:20:50 +02:00
parent a848d85b7d
commit e3cd2ac9ef
2 changed files with 21 additions and 46 deletions

View File

@ -53,7 +53,7 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
Height = height; Height = height;
_colors = new byte[Width * Height]; _colors = new byte[Width * Height];
Hash = MurMurHash3.Hash(_colors); Hash = MurMurHash3.Hash(_colors);
CalcuateNumberOfColoredPixels(); NumberOfColoredPixels = 0;
} }
public BinaryOcrBitmap(Stream stream) public BinaryOcrBitmap(Stream stream)
@ -113,27 +113,25 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
Width = nbmp.Width; Width = nbmp.Width;
Height = nbmp.Height; Height = nbmp.Height;
_colors = new byte[Width * Height]; _colors = new byte[Width * Height];
var numberOfColoredPixels = 0;
for (int y = 0; y < Height; y++) for (int y = 0; y < Height; y++)
{ {
for (int x = 0; x < Width; x++) for (int x = 0; x < Width; x++)
{ {
SetPixelViaAlpha(x, y, nbmp.GetAlpha(x, y)); var alpha = nbmp.GetAlpha(x, y);
if (alpha < 100)
{
_colors[Width * y + x] = 0;
}
else
{
_colors[Width * y + x] = 1;
numberOfColoredPixels++;
}
} }
} }
NumberOfColoredPixels = numberOfColoredPixels;
Hash = MurMurHash3.Hash(_colors); Hash = MurMurHash3.Hash(_colors);
CalcuateNumberOfColoredPixels();
}
private void CalcuateNumberOfColoredPixels()
{
NumberOfColoredPixels = 0;
for (int i = 0; i < _colors.Length; i++)
{
if (_colors[i] > 0)
{
NumberOfColoredPixels++;
}
}
} }
public bool AreColorsEqual(BinaryOcrBitmap other) public bool AreColorsEqual(BinaryOcrBitmap other)
@ -216,33 +214,9 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
return _colors[index]; return _colors[index];
} }
public void SetPixel(int x, int y, int c) public void SetPixel(int x, int y)
{ {
_colors[Width * y + x] = (byte)c; _colors[Width * y + x] = 1;
}
public void SetPixel(int x, int y, Color c)
{
if (c.A < 100)
{
_colors[Width * y + x] = 0;
}
else
{
_colors[Width * y + x] = 1;
}
}
public void SetPixelViaAlpha(int x, int y, int alpha)
{
if (alpha < 100)
{
_colors[Width * y + x] = 0;
}
else
{
_colors[Width * y + x] = 1;
}
} }
/// <summary> /// <summary>
@ -322,13 +296,13 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
int c = bob.GetPixel(x, y); int c = bob.GetPixel(x, y);
if (c > 0) if (c > 0)
{ {
nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y, 1); nbmp.SetPixel(bob.X - minX + x, bob.Y - minY + y);
} }
} }
} }
} }
return nbmp.ToOldBitmap(color); // Resursive return nbmp.ToOldBitmap(color); // Recursive
} }
else else
{ {

View File

@ -1,9 +1,9 @@
using System; using Nikse.SubtitleEdit.Core;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using Nikse.SubtitleEdit.Core;
namespace Nikse.SubtitleEdit.Logic.Ocr.Binary namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
{ {
@ -135,10 +135,11 @@ namespace Nikse.SubtitleEdit.Logic.Ocr.Binary
public int FindExactMatch(BinaryOcrBitmap bob) public int FindExactMatch(BinaryOcrBitmap bob)
{ {
var bobHash = bob.Hash;
for (int i = 0; i < CompareImages.Count; i++) for (int i = 0; i < CompareImages.Count; i++)
{ {
var b = CompareImages[i]; var b = CompareImages[i];
if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) if (bobHash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels)
{ {
if (AllowEqual(b, bob)) if (AllowEqual(b, bob))
{ {