public void Add(NOcrChar ocrChar) { if (ocrChar.ExpandCount > 0) OcrCharactersExpanded.Insert(0, ocrChar); else OcrCharacters.Insert(0, ocrChar); }
private void AddHistoryItem(NOcrChar nocrChar) { if (_historyIndex > 0 && _historyIndex < _history.Count - 1) { while (_history.Count > _historyIndex + 1) _history.RemoveAt(_history.Count - 1); _historyIndex = _history.Count - 1; } _history.Add(new NOcrChar(nocrChar)); _historyIndex++; }
public void Add(NOcrChar ocrChar) { if (ocrChar.ExpandCount > 0) { OcrCharactersExpanded.Insert(0, ocrChar); } else { OcrCharacters.Insert(0, ocrChar); } }
public NOcrChar(NOcrChar old) { LinesForeground = new List<NOcrPoint>(); LinesBackground = new List<NOcrPoint>(); Text = old.Text; Width = old.Width; Height = old.Height; MarginTop = old.MarginTop; Italic = old.Italic; foreach (NOcrPoint p in old.LinesForeground) LinesForeground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y))); foreach (NOcrPoint p in old.LinesBackground) LinesBackground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y))); }
public void TestNOcrSaveLoad() { string tempFileName = System.IO.Path.GetTempFileName(); var db = new NOcrDb(tempFileName); var nOcrChar = new NOcrChar("t"); nOcrChar.ExpandCount = 0; nOcrChar.Italic = false; nOcrChar.MarginTop = 2; nOcrChar.Width = 10; nOcrChar.Height = 10; nOcrChar.LinesForeground.Add(new NOcrPoint(new Point(1,1), new Point(2,2))); nOcrChar.LinesBackground.Add(new NOcrPoint(new Point(3,4), new Point(5,6))); db.Add(nOcrChar); var nOcrChar2 = new NOcrChar("u"); nOcrChar2.ExpandCount = 0; nOcrChar2.Italic = false; nOcrChar2.MarginTop = 3; nOcrChar2.Width = 12; nOcrChar2.Height = 12; nOcrChar2.LinesForeground.Add(new NOcrPoint(new Point(1, 1), new Point(2, 2))); nOcrChar2.LinesBackground.Add(new NOcrPoint(new Point(3, 4), new Point(5, 6))); db.Add(nOcrChar2); db.Save(); db = new NOcrDb(tempFileName); Assert.IsTrue(db.OcrCharacters.Count == 2); Assert.IsTrue(db.OcrCharacters[0].Text == nOcrChar2.Text); Assert.IsTrue(db.OcrCharacters[0].Italic == nOcrChar2.Italic); Assert.IsTrue(db.OcrCharacters[0].MarginTop == nOcrChar2.MarginTop); Assert.IsTrue(db.OcrCharacters[0].LinesForeground.Count == nOcrChar2.LinesForeground.Count); Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.X == nOcrChar2.LinesForeground[0].Start.X); Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.Y == nOcrChar2.LinesForeground[0].Start.Y); Assert.IsTrue(db.OcrCharacters[0].LinesBackground.Count == nOcrChar2.LinesBackground.Count); Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.X == nOcrChar2.LinesBackground[0].Start.X); Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.Y == nOcrChar2.LinesBackground[0].Start.Y); Assert.IsTrue(db.OcrCharacters[1].Text == nOcrChar.Text); try { System.IO.File.Delete(tempFileName); } catch { } }
public NOcrChar(NOcrChar old) { LinesForeground = new List <NOcrPoint>(); LinesBackground = new List <NOcrPoint>(); Text = old.Text; Width = old.Width; Height = old.Height; MarginTop = old.MarginTop; Italic = old.Italic; foreach (NOcrPoint p in old.LinesForeground) { LinesForeground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y))); } foreach (NOcrPoint p in old.LinesBackground) { LinesBackground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y))); } }
public void LoadOcrCharacters() { var list = new List <NOcrChar>(); var listExpanded = new List <NOcrChar>(); if (!File.Exists(FileName)) { OcrCharacters = list; OcrCharactersExpanded = listExpanded; return; } using (Stream fs = File.OpenRead(FileName)) { using (Stream gz = new GZipStream(fs, CompressionMode.Decompress)) { bool done = false; while (!done) { var ocrChar = new NOcrChar(gz); if (ocrChar.LoadedOk) { if (ocrChar.ExpandCount > 0) { listExpanded.Add(ocrChar); } else { list.Add(ocrChar); } } else { done = true; } } } } OcrCharacters = list; OcrCharactersExpanded = listExpanded; }
internal Point GetScaledStart(NOcrChar ocrChar, int width, int height) { return new Point((int)Math.Round(Start.X * 100.0 / ocrChar.Width * width / 100.0), (int)Math.Round(Start.Y * 100.0 / ocrChar.Height * height / 100.0)); }
public List<Point> ScaledGetPoints(NOcrChar nOcrChar, int width, int height) { return GetPoints(GetScaledStart(nOcrChar, width, height), GetScaledEnd(nOcrChar, width, height)); }
private void Undo() { _drawLineOn = false; _startDone = false; if (_history.Count > 0 && _historyIndex > 0) { _historyIndex--; _nocrChar = new NOcrChar(_history[_historyIndex]); } else if (_historyIndex == 0) { var c = new NOcrChar(_nocrChar); c.LinesForeground.Clear(); c.LinesBackground.Clear(); _nocrChar = c; _historyIndex--; } ShowOcrPoints(); }
private void Redo() { if (_history.Count > 0 && _historyIndex < _history.Count - 1) { _historyIndex++; _nocrChar = new NOcrChar(_history[_historyIndex]); ShowOcrPoints(); } }
public int FindExactMatch(NOcrChar ocrChar) { return -1; }
public CompareMatch(string text, bool italic, int expandCount, string name, NOcrChar character) : this(text, italic, expandCount, name) { NOcrCharacter = character; }
public static List<NOcrChar> LoadNOcr(string fileName) { var nocrChars = new List<NOcrChar>(); if (File.Exists(fileName)) { try { var doc = new XmlDocument(); doc.Load(fileName); foreach (XmlNode node in doc.DocumentElement.SelectNodes("Char")) { var oc = new NOcrChar(node.Attributes["Text"].Value); oc.Width = Convert.ToInt32(node.Attributes["Width"].Value, CultureInfo.InvariantCulture); oc.Height = Convert.ToInt32(node.Attributes["Height"].Value, CultureInfo.InvariantCulture); oc.MarginTop = Convert.ToInt32(node.Attributes["MarginTop"].Value, CultureInfo.InvariantCulture); if (node.Attributes["Italic"] != null) oc.Italic = Convert.ToBoolean(node.Attributes["Italic"].Value, CultureInfo.InvariantCulture); if (node.Attributes["ExpandCount"] != null) oc.ExpandCount = Convert.ToInt32(node.Attributes["ExpandCount"].Value, CultureInfo.InvariantCulture); foreach (XmlNode pointNode in node.SelectNodes("Point")) { var op = new NOcrPoint(DecodePoint(pointNode.Attributes["Start"].Value), DecodePoint(pointNode.Attributes["End"].Value)); XmlAttribute a = pointNode.Attributes["On"]; if (a != null && Convert.ToBoolean(a.Value)) oc.LinesForeground.Add(op); else oc.LinesBackground.Add(op); } nocrChars.Add(oc); } } catch (Exception exception) { MessageBox.Show(exception.Message); } } return nocrChars; }
public CompareMatch(string text, bool italic, int expandCount, string name, NOcrChar character) { Text = text; Italic = italic; ExpandCount = expandCount; Name = name; NOcrCharacter = character; }
public int FindExactMatch(NOcrChar ocrChar) { return(-1); }
internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List<VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm) { listBoxLinesForeground.Items.Clear(); listBoxlinesBackground.Items.Clear(); NikseBitmap nbmp = new NikseBitmap(vobSubImage); nbmp.ReplaceTransparentWith(Color.Black); vobSubImage = nbmp.GetBitmap(); radioButtonHot.Checked = true; ShrinkSelection = false; ExpandSelection = false; textBoxCharacters.Text = string.Empty; _vobSubForm = vobSubForm; _additions = additions; _nocrChar = new NOcrChar(); _nocrChar.MarginTop = character.Y - character.ParentY; _imageWidth = character.NikseBitmap.Width; _imageHeight = character.NikseBitmap.Height; _drawLineOn = false; _warningNoNotForegroundLinesShown = false; buttonShrinkSelection.Visible = showShrink; if (position.X != -1 && position.Y != -1) { StartPosition = FormStartPosition.Manual; Left = position.X; Top = position.Y; } pictureBoxSubtitleImage.Image = vobSubImage; pictureBoxCharacter.Image = character.NikseBitmap.GetBitmap(); Bitmap org = (Bitmap)vobSubImage.Clone(); Bitmap bm = new Bitmap(org.Width, org.Height); Graphics g = Graphics.FromImage(bm); g.DrawImage(org, 0, 0, org.Width, org.Height); g.DrawRectangle(Pens.Red, character.X, character.Y, character.NikseBitmap.Width, character.NikseBitmap.Height - 1); g.Dispose(); pictureBoxSubtitleImage.Image = bm; pictureBoxCharacter.Top = labelCharacters.Top + 16; SizePictureBox(); checkBoxItalic.Checked = italicChecked; _history = new List<NOcrChar>(); _historyIndex = -1; _nocrChar.Width = _imageWidth; _nocrChar.Height = _imageHeight; GenerateLineSegments(150, false, _nocrChar, new NikseBitmap(pictureBoxCharacter.Image as Bitmap)); ShowOcrPoints(); pictureBoxCharacter.Invalidate(); }
public static void GenerateLineSegments(int numberOfLines, bool veryPrecise, NOcrChar nOcrChar, NikseBitmap nbmp) { int giveUpCount = 10000; var r = new Random(); int count = 0; int hits = 0; bool tempVeryPrecise = veryPrecise; while (hits < numberOfLines && count < giveUpCount) { var start = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); var end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); if (hits < 5 && count < 100) // a few large lines { for (int k = 0; k < 500; k++) { if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) > nOcrChar.Height / 2) { break; } else { end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); } } } else // and a lot of small lines { for (int k = 0; k < 500; k++) { if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) < 5) { break; } else { end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); } } } var op = new NOcrPoint(start, end); bool ok = true; foreach (NOcrPoint existingOp in nOcrChar.LinesForeground) { if (existingOp.Start.X == op.Start.X && existingOp.Start.Y == op.Start.Y && existingOp.End.X == op.End.X && existingOp.End.Y == op.End.Y) ok = false; } if (ok && IsMatchPointForeGround(op, !tempVeryPrecise, nbmp, nOcrChar)) { nOcrChar.LinesForeground.Add(op); //AddHistoryItem(nOcrChar); hits++; } count++; if (count > giveUpCount - 100 && !tempVeryPrecise) tempVeryPrecise = true; } count = 0; hits = 0; tempVeryPrecise = veryPrecise; while (hits < numberOfLines && count < giveUpCount) { var start = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); var end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); if (hits < 5 && count < 100) // a few large lines { for (int k = 0; k < 500; k++) { if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) > nOcrChar.Height / 2) { break; } else { end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); } } } else // and a lot of small lines { for (int k = 0; k < 500 ; k++) { if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) < 5) { break; } else { end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height)); } } } var op = new NOcrPoint(start, end); bool ok = true; foreach (NOcrPoint existingOp in nOcrChar.LinesBackground) { if (existingOp.Start.X == op.Start.X && existingOp.Start.Y == op.Start.Y && existingOp.End.X == op.End.X && existingOp.End.Y == op.End.Y) ok = false; } if (ok && IsMatchPointBackGround(op, !tempVeryPrecise, nbmp, nOcrChar)) { nOcrChar.LinesBackground.Add(op); //AddHistoryItem(nOcrChar); hits++; } count++; if (count > giveUpCount - 100 && !tempVeryPrecise) tempVeryPrecise = true; } }
private static bool IsMatchPointForeGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar) { if (Math.Abs(op.Start.X - op.End.X) < 2 && Math.Abs(op.End.Y - op.Start.Y) < 2) return false; foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height)) { if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height) { Color c = nbmp.GetPixel(point.X, point.Y); if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor) { } else { return false; } if (loose) { if (nbmp.Width > 10 && point.X + 1 < nbmp.Width) { c = nbmp.GetPixel(point.X + 1, point.Y); if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor) { } else { return false; } } if (nbmp.Width > 10 && point.X >= 1) { c = nbmp.GetPixel(point.X - 1, point.Y); if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor) { } else { return false; } } if (nbmp.Height > 10 && point.Y + 1 < nbmp.Height) { c = nbmp.GetPixel(point.X, point.Y + 1); if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor) { } else { return false; } } if (nbmp.Height > 10 && point.Y >= 1) { c = nbmp.GetPixel(point.X, point.Y - 1); if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor) { } else { return false; } } } } } return true; }
internal Point GetScaledEnd(NOcrChar ocrChar, int width, int height) { return(new Point((int)Math.Round(End.X * 100.0 / ocrChar.Width * width / 100.0), (int)Math.Round(End.Y * 100.0 / ocrChar.Height * height / 100.0))); }
public List <Point> ScaledGetPoints(NOcrChar nOcrChar, int width, int height) { return(GetPoints(GetScaledStart(nOcrChar, width, height), GetScaledEnd(nOcrChar, width, height))); }
private static NOcrChar MakeItalicNOcrChar(NOcrChar oldChar, int movePixelsLeft, double unItalicFactor) { var c = new NOcrChar(); foreach (NOcrPoint op in oldChar.LinesForeground) { c.LinesForeground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor))); } foreach (NOcrPoint op in oldChar.LinesBackground) { c.LinesBackground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor))); } c.Text = oldChar.Text; c.Width = oldChar.Width; c.Height = oldChar.Height; c.MarginTop = oldChar.MarginTop; c.Italic = true; return c; }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { if (listBoxInspectItems.SelectedIndex < 0) return; var img = _imageList[listBoxInspectItems.SelectedIndex]; if (img.NikseBitmap != null) { pictureBoxInspectItem.Width = img.NikseBitmap.Width; pictureBoxInspectItem.Height = img.NikseBitmap.Height; var old = img.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = old; pictureBoxCharacter.Image = old; SizePictureBox(); } else { pictureBoxInspectItem.Image = null; pictureBoxCharacter.Image = null; } var match = _matchList[listBoxInspectItems.SelectedIndex]; if (match == null) { // spaces+new lines _nocrChar = null; pictureBoxCharacter.Invalidate(); buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonEditDB.Enabled = false; buttonAddBetterMatch.Enabled = false; textBoxText.Text = string.Empty; textBoxText.Enabled = false; checkBoxItalic.Checked = false; checkBoxItalic.Enabled = false; } else if (match.NOcrCharacter == null) { // no match found buttonUpdate.Enabled = false; buttonDelete.Enabled = false; textBoxText.Text = string.Empty; checkBoxItalic.Checked = match.Italic; _nocrChar = null; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = false; checkBoxItalic.Enabled = false; } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; textBoxText.Text = match.Text; checkBoxItalic.Checked = match.Italic; _nocrChar = match.NOcrCharacter; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } }
public static List<NOcrChar> LoadNOcrForTesseract(string xmlRessourceName) { var nocrChars = new List<NOcrChar>(); System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); Stream strm = asm.GetManifestResourceStream(xmlRessourceName); if (strm != null) { XmlDocument doc = new XmlDocument(); var rdr = new StreamReader(strm); using (var zip = new System.IO.Compression.GZipStream(rdr.BaseStream, System.IO.Compression.CompressionMode.Decompress)) { byte[] data = new byte[175000]; zip.Read(data, 0, 175000); doc.LoadXml(System.Text.Encoding.UTF8.GetString(data)); } rdr.Close(); try { foreach (XmlNode node in doc.DocumentElement.SelectNodes("Char")) { var oc = new NOcrChar(node.Attributes["Text"].Value); oc.Width = Convert.ToInt32(node.Attributes["Width"].Value, CultureInfo.InvariantCulture); oc.Height = Convert.ToInt32(node.Attributes["Height"].Value, CultureInfo.InvariantCulture); oc.MarginTop = Convert.ToInt32(node.Attributes["MarginTop"].Value, CultureInfo.InvariantCulture); if (node.Attributes["Italic"] != null) oc.Italic = Convert.ToBoolean(node.Attributes["Italic"].Value, CultureInfo.InvariantCulture); if (node.Attributes["ExpandCount"] != null) oc.ExpandCount = Convert.ToInt32(node.Attributes["ExpandCount"].Value, CultureInfo.InvariantCulture); foreach (XmlNode pointNode in node.SelectNodes("Point")) { var op = new NOcrPoint(DecodePoint(pointNode.Attributes["Start"].Value), DecodePoint(pointNode.Attributes["End"].Value)); XmlAttribute a = pointNode.Attributes["On"]; if (a != null && Convert.ToBoolean(a.Value)) oc.LinesForeground.Add(op); else oc.LinesBackground.Add(op); } nocrChars.Add(oc); } } catch (Exception exception) { MessageBox.Show(exception.Message); } } return nocrChars; }
private void listBoxFileNames_SelectedIndexChanged(object sender, EventArgs e) { labelNOcrCharInfo.Text = string.Empty; if (listBoxFileNames.SelectedIndex < 0) return; _nocrChar = listBoxFileNames.Items[listBoxFileNames.SelectedIndex] as NOcrChar; if (_nocrChar == null) { pictureBoxCharacter.Invalidate(); groupBoxCurrentCompareImage.Enabled = false; listBoxLinesForeground.Items.Clear(); listBoxlinesBackground.Items.Clear(); } else { textBoxText.Text = _nocrChar.Text; checkBoxItalic.Checked = _nocrChar.Italic; pictureBoxCharacter.Invalidate(); groupBoxCurrentCompareImage.Enabled = true; labelNOcrCharInfo.Text = string.Format("Size: {0}x{1}, margin top: {2} ", _nocrChar.Width, _nocrChar.Height, _nocrChar.MarginTop); if (pictureBoxCharacter.Image != null) { if (IsMatch()) { groupBoxCurrentCompareImage.BackColor = Color.LightGreen; } else { groupBoxCurrentCompareImage.BackColor = Control.DefaultBackColor; } } _drawLineOn = false; _history = new List<NOcrChar>(); _historyIndex = -1; if (_bitmap == null) { var bitmap = new Bitmap(_nocrChar.Width, _nocrChar.Height); NikseBitmap nbmp = new NikseBitmap(bitmap); nbmp.Fill(Color.White); bitmap = nbmp.GetBitmap(); pictureBoxCharacter.Image = bitmap; SizePictureBox(); ShowOcrPoints(); } } }
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List<string> charactersLearned, string s, bool bold) { Bitmap bmp = GenerateImageFromTextWithStyle(s, bold); var nbmp = new NikseBitmap(bmp); nbmp.MakeTwoColor(210, 280); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25); if (list.Count == 1) { NOcrChar match = nOcrD.GetMatch(list[0].NikseBitmap); if (match == null) { pictureBox1.Image = list[0].NikseBitmap.GetBitmap(); this.Refresh(); Application.DoEvents(); System.Threading.Thread.Sleep(100); NOcrChar nOcrChar = new NOcrChar(s); nOcrChar.Width = list[0].NikseBitmap.Width; nOcrChar.Height = list[0].NikseBitmap.Height; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value, checkBoxVeryAccurate.Checked, nOcrChar, list[0].NikseBitmap); nOcrD.Add(nOcrChar); charactersLearned.Add(s); numberOfCharactersLeaned++; labelInfo.Text = string.Format("Now training font '{1}', total characters leaned is {0}, {2} skipped", numberOfCharactersLeaned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } }
public void LoadOcrCharacters() { var list = new List<NOcrChar>(); var listExpanded = new List<NOcrChar>(); if (!File.Exists(FileName)) { OcrCharacters = list; OcrCharactersExpanded = listExpanded; return; } using (Stream fs = File.OpenRead(FileName)) { using (Stream gz = new GZipStream(fs, CompressionMode.Decompress)) { bool done = false; while (!done) { var ocrChar = new NOcrChar(gz); if (ocrChar.LoadedOk) { if (ocrChar.ExpandCount > 0) listExpanded.Add(ocrChar); else list.Add(ocrChar); } else { done = true; } } } } OcrCharacters = list; OcrCharactersExpanded = listExpanded; }