private void MakeExpandImage() { var splitterItem = _splitterItems[_startIndex]; if (splitterItem.NikseBitmap == null) { return; } ExpandedMatch = new BinaryOcrBitmap(new NikseBitmap(splitterItem.NikseBitmap), false, (int)numericUpDownExpandCount.Value, string.Empty, splitterItem.X, splitterItem.Y) { ExpandedList = new List <BinaryOcrBitmap>() }; for (int i = 1; i < listBoxInspectItems.Items.Count; i++) { if (i < numericUpDownExpandCount.Value) { splitterItem = _splitterItems[_startIndex + i]; if (splitterItem.NikseBitmap == null) { break; } ExpandedMatch.ExpandedList.Add(new BinaryOcrBitmap(splitterItem.NikseBitmap, false, 0, null, splitterItem.X, splitterItem.Y)); } } pictureBoxInspectItem.Image = ExpandedMatch.ToOldBitmap(); }
private void buttonDelete_Click(object sender, EventArgs e) { if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { return; } listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex] = Configuration.Settings.Language.VobSubOcr.NoMatch; if (_selectedCompareBinaryOcrBitmap != null) { if (_selectedCompareBinaryOcrBitmap.ExpandCount > 0) { _binOcrDb.CompareImagesExpanded.Remove(_selectedCompareBinaryOcrBitmap); } else { _binOcrDb.CompareImages.Remove(_selectedCompareBinaryOcrBitmap); } _selectedCompareBinaryOcrBitmap = null; } else { ImageCompareDocument.DocumentElement.RemoveChild(_selectedCompareNode); _selectedCompareNode = null; } listBoxInspectItems_SelectedIndexChanged(null, null); }
private void TrainLetter(ref int numberOfCharactersLearned, ref int numberOfCharactersSkipped, BinaryOcrDb db, string s, bool bold, bool italic) { var bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic); var nbmp = new NikseBitmap(bmp); nbmp.MakeTwoColor(280); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25, false); if (list.Count == 3) { var item = list[2]; var bob = new BinaryOcrBitmap(item.NikseBitmap, italic, 0, s, item.X, item.Y); var match = db.FindExactMatch(bob); if (match < 0) { db.Add(bob); numberOfCharactersLearned++; bmp.Dispose(); } else { numberOfCharactersSkipped++; } } }
private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, BinaryOcrDb db, List <string> charactersLearned, string s, bool bold, bool italic) { Bitmap bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic); var nbmp = new NikseBitmap(bmp); nbmp.MakeTwoColor(280); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25); if (list.Count == 3) { var item = list[2]; var bob = new BinaryOcrBitmap(item.NikseBitmap, italic, 0, s, item.X, item.Y); var match = db.FindExactMatch(bob); if (match < 0) { labelInfo.Refresh(); Application.DoEvents(); db.Add(bob); charactersLearned.Add(s); numberOfCharactersLeaned++; labelInfo.Text = string.Format("Now training font '{1}', total characters learned is {0}, {2} skipped", numberOfCharactersLeaned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } }
public void TestMethodBinOcrSaveLoad() { string tempFileName = Path.GetTempFileName(); var db = new BinaryOcrDb(tempFileName); var nbmp = new NikseBitmap(2, 2); nbmp.SetPixel(0, 0, Color.Transparent); nbmp.SetPixel(1, 0, Color.Transparent); nbmp.SetPixel(1, 0, Color.Transparent); nbmp.SetPixel(1, 1, Color.White); var bob = new BinaryOcrBitmap(nbmp); bob.Text = "Debug"; db.Add(bob); nbmp.SetPixel(0, 0, Color.White); var bob2 = new BinaryOcrBitmap(nbmp); bob2.X = 2; bob2.Y = 4; bob2.Text = "tt"; bob2.Italic = true; bob2.ExpandCount = 2; bob2.ExpandedList = new System.Collections.Generic.List <BinaryOcrBitmap>(); bob2.ExpandedList.Add(bob2); db.Add(bob2); db.Save(); db = new BinaryOcrDb(tempFileName, true); Assert.IsTrue(db.CompareImages.Count == 1); Assert.IsTrue(db.CompareImagesExpanded.Count == 1); Assert.IsTrue(bob.Width == db.CompareImages[0].Width); Assert.IsTrue(bob.Height == db.CompareImages[0].Height); Assert.IsTrue(bob.NumberOfColoredPixels == db.CompareImages[0].NumberOfColoredPixels); Assert.IsTrue(bob.Hash == db.CompareImages[0].Hash); Assert.IsTrue(bob.Italic == db.CompareImages[0].Italic); Assert.IsTrue(bob.ExpandCount == db.CompareImages[0].ExpandCount); Assert.IsTrue(bob.Text == db.CompareImages[0].Text); Assert.IsTrue(bob2.Width == db.CompareImagesExpanded[0].Width); Assert.IsTrue(bob2.Height == db.CompareImagesExpanded[0].Height); Assert.IsTrue(bob2.NumberOfColoredPixels == db.CompareImagesExpanded[0].NumberOfColoredPixels); Assert.IsTrue(bob2.Hash == db.CompareImagesExpanded[0].Hash); Assert.IsTrue(bob2.Italic == db.CompareImagesExpanded[0].Italic); Assert.IsTrue(bob2.ExpandCount == db.CompareImagesExpanded[0].ExpandCount); Assert.IsTrue(bob2.Text == db.CompareImagesExpanded[0].Text); Assert.IsTrue(bob2.X == db.CompareImagesExpanded[0].X); Assert.IsTrue(bob2.Y == db.CompareImagesExpanded[0].Y); try { File.Delete(tempFileName); } catch { } }
public void InitializeDetectFont(BinaryOcrBitmap bob, string text) { if (string.IsNullOrEmpty(text)) { return; } _autoDetectFontBob = bob; _autoDetectFontText = text; }
private void AddToListView(BinaryOcrBitmap bob) { _data.Add(new ListViewData { Checked = true, BinaryOcrBitmap = bob }); var item = new ListViewItem(string.Empty) { Tag = bob, Checked = true }; item.SubItems.Add(bob.ToString()); listView1.Items.Add(item); imageList1.Images.Add(bob.ToOldBitmap(Color.Black)); item.ImageIndex = imageList1.Images.Count - 1; }
public void TestMethodBinOcrSaveLoadTestExceptions() { string tempFileName = FileUtil.GetTempFileName(".db"); var db = new BinaryOcrDb(tempFileName); var nbmp = new NikseBitmap(2, 2); nbmp.SetPixel(0, 0, Color.Transparent); nbmp.SetPixel(1, 0, Color.Transparent); nbmp.SetPixel(1, 0, Color.Transparent); nbmp.SetPixel(1, 1, Color.White); var bob = new BinaryOcrBitmap(nbmp); bob.Text = "S"; db.Add(bob); nbmp.SetPixel(0, 0, Color.White); var bob2 = new BinaryOcrBitmap(nbmp); bob2.X = 2; bob2.Y = 4; bob2.Text = null; bob2.Italic = true; bob2.ExpandCount = 3; bob2.ExpandedList = new System.Collections.Generic.List <BinaryOcrBitmap>(); bob2.ExpandedList.Add(bob2); try { db.Add(bob2); } catch { return; } Assert.Fail(); try { File.Delete(tempFileName); } catch { } }
private void buttonDelete_Click(object sender, EventArgs e) { if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { return; } listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex] = LanguageSettings.Current.VobSubOcr.NoMatch; if (_selectedCompareBinaryOcrBitmap != null) { if (_selectedCompareBinaryOcrBitmap.ExpandCount > 0) { _binOcrDb.CompareImagesExpanded.Remove(_selectedCompareBinaryOcrBitmap); } else { _binOcrDb.CompareImages.Remove(_selectedCompareBinaryOcrBitmap); } DeleteMultiMatch = _selectedCompareBinaryOcrBitmap.ExpandCount > 0; _selectedCompareBinaryOcrBitmap = null; _binOcrDb.Save(); if (DeleteMultiMatch) { LastIndex = listBoxInspectItems.SelectedIndex; DialogResult = DialogResult.OK; return; } } else { ImageCompareDocument.DocumentElement.RemoveChild(_selectedCompareNode); _selectedCompareNode = null; } listBoxInspectItems_SelectedIndexChanged(null, null); }
private void ButtonDeleteClick(object sender, EventArgs e) { if (listBoxFileNames.Items.Count == 0) { return; } int oldComboBoxIndex = comboBoxTexts.SelectedIndex; string target = GetSelectedFileName(); if (_binOcrDb != null) { BinaryOcrBitmap bob = GetSelectedBinOcrBitmap(); if (bob != null) { if (bob.ExpandCount > 0) { _binOcrDb.CompareImagesExpanded.Remove(bob); } else { _binOcrDb.CompareImages.Remove(bob); } if (Additions != null && Additions.Count > 0) { for (int i = Additions.Count - 1; i >= 0; i--) { if (Additions[i].Name == bob.Key) { Additions.RemoveAt(i); Refill(Additions); break; } } } Refill(Additions); } if (oldComboBoxIndex >= 0 && oldComboBoxIndex < comboBoxTexts.Items.Count) { comboBoxTexts.SelectedIndex = oldComboBoxIndex; } return; } XmlNode node = _compareDoc.DocumentElement.SelectSingleNode("Item[.='" + target + "']"); if (node != null) { _compareDoc.DocumentElement.RemoveChild(node); if (Additions != null && Additions.Count > 0) { for (int i = Additions.Count - 1; i >= 0; i--) { if (Additions[i].Name == target) { Additions.RemoveAt(i); Refill(Additions); break; } } } Refill(Additions); if (Additions == null || Additions.Count == 0) { if (oldComboBoxIndex < comboBoxTexts.Items.Count) { comboBoxTexts.SelectedIndex = oldComboBoxIndex; } } } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageInfo.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } _selectedCompareNode = null; _selectedCompareBinaryOcrBitmap = null; pictureBoxInspectItem.Image = _imageSources[listBoxInspectItems.SelectedIndex]; pictureBoxCompareBitmap.Image = null; pictureBoxCompareBitmapDouble.Image = null; int index = (listBoxInspectItems.SelectedIndex); var match = _matches[index]; _selectedMatch = match; if (!string.IsNullOrEmpty(match.Name)) { Bitmap bitmap = new Bitmap(1, 1); if (_binOcrDb != null) { bool bobFound = false; foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex])); if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels) { buttonAddBetterMatch.Enabled = false; // exact match } else { buttonAddBetterMatch.Enabled = true; } bobFound = true; break; } } if (!bobFound) { foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; bitmap = bob.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; buttonAddBetterMatch.Enabled = false; // exact match labelExpandCount.Text = string.Format("Expand count: {0}", bob.ExpandCount); break; } } } } else { foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes) { if (node.Attributes["Text"] != null && node.InnerText == match.Name) { string text = node.Attributes["Text"].InnerText; string imageFileName = node.InnerText; imageFileName = Path.Combine(_directoryPath, imageFileName); textBoxText.Text = text; checkBoxItalic.Checked = node.Attributes["Italic"] != null; string databaseName = Path.Combine(_directoryPath, "Images.db"); using (var f = new FileStream(databaseName, FileMode.Open)) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); f.Position = pos; ManagedBitmap mbmp = new ManagedBitmap(f); bitmap = mbmp.ToOldBitmap(); pictureBoxCompareBitmap.Image = bitmap; pictureBoxCompareBitmapDouble.Width = bitmap.Width * 2; pictureBoxCompareBitmapDouble.Height = bitmap.Height * 2; pictureBoxCompareBitmapDouble.Image = bitmap; } catch (Exception exception) { MessageBox.Show(exception.Message); } } try { labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height); } catch { } _selectedCompareNode = node; break; } } } } if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = false; textBoxText.Enabled = false; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = false; } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; if (_selectedCompareNode != null) { buttonAddBetterMatch.Enabled = true; } textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } }
private void buttonAddBetterMatch_Click(object sender, EventArgs e) { if (listBoxInspectItems.SelectedIndex < 0) { return; } if (listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex].ToString() == textBoxText.Text) { textBoxText.SelectAll(); textBoxText.Focus(); return; } if (_selectedCompareNode != null) { XmlNode newNode = ImageCompareDocument.CreateElement("Item"); XmlAttribute text = newNode.OwnerDocument.CreateAttribute("Text"); text.InnerText = textBoxText.Text; newNode.Attributes.Append(text); string databaseName = Path.Combine(_directoryPath, "Images.db"); FileStream f; long pos = 0; if (!File.Exists(databaseName)) { using (f = new FileStream(databaseName, FileMode.Create)) { pos = f.Position; new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f); } } else { using (f = new FileStream(databaseName, FileMode.Append)) { pos = f.Position; new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f); } } string name = pos.ToString(CultureInfo.InvariantCulture); newNode.InnerText = name; SetItalic(newNode); ImageCompareDocument.DocumentElement.AppendChild(newNode); int index = listBoxInspectItems.SelectedIndex; _matches[index].Name = name; _matches[index].ExpandCount = 0; _matches[index].Italic = checkBoxItalic.Checked; _matches[index].Text = textBoxText.Text; listBoxInspectItems.Items.Clear(); for (int i = 0; i < _matches.Count; i++) { listBoxInspectItems.Items.Add(_matches[i].Text); } listBoxInspectItems.SelectedIndex = index; ShowCount(); listBoxInspectItems_SelectedIndexChanged(null, null); } else if (_selectedCompareBinaryOcrBitmap != null) { var nbmp = new NikseBitmap((pictureBoxInspectItem.Image as Bitmap)); int x = 0; int y = 0; if (_selectedMatch != null && _selectedMatch.ImageSplitterItem != null) { x = _selectedMatch.X; y = _selectedMatch.Y; } var bob = new BinaryOcrBitmap(nbmp, checkBoxItalic.Checked, 0, textBoxText.Text, x, y); _binOcrDb.Add(bob); int index = listBoxInspectItems.SelectedIndex; _matches[index].Name = bob.Key; _matches[index].ExpandCount = 0; _matches[index].Italic = checkBoxItalic.Checked; _matches[index].Text = textBoxText.Text; listBoxInspectItems.Items.Clear(); for (int i = 0; i < _matches.Count; i++) { listBoxInspectItems.Items.Add(_matches[i].Text); } listBoxInspectItems.SelectedIndex = index; listBoxInspectItems_SelectedIndexChanged(null, null); ShowCount(); } }
private void buttonCancel_Click(object sender, System.EventArgs e) { ExpandedMatch = null; DialogResult = DialogResult.Cancel; }
private void buttonAddBetterMatch_Click(object sender, EventArgs e) { if (listBoxInspectItems.SelectedIndex < 0) { return; } if (listBoxInspectItems.Items[listBoxInspectItems.SelectedIndex].ToString() == textBoxText.Text) { textBoxText.SelectAll(); textBoxText.Focus(); return; } if (_selectedCompareBinaryOcrBitmap != null || (_binOcrDb != null && (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch || _selectedMatch.NOcrCharacter == null))) { var nbmp = new NikseBitmap((pictureBoxInspectItem.Image as Bitmap)); int x = 0; int y = 0; if (_selectedMatch != null && _selectedMatch.ImageSplitterItem != null) { if (_selectedMatch.ImageSplitterItem != null) { x = _selectedMatch.ImageSplitterItem.X; y = _selectedMatch.ImageSplitterItem.Top; } else { x = _selectedMatch.X; y = _selectedMatch.Y; } } var bob = new BinaryOcrBitmap(nbmp, checkBoxItalic.Checked, 0, textBoxText.Text, x, y); _binOcrDb.Add(bob); int index = listBoxInspectItems.SelectedIndex; _matches[index].Name = bob.Key; _matches[index].ExpandCount = 0; _matches[index].Italic = checkBoxItalic.Checked; _matches[index].Text = textBoxText.Text; listBoxInspectItems.Items.Clear(); for (int i = 0; i < _matches.Count; i++) { listBoxInspectItems.Items.Add(_matches[i].Text); } listBoxInspectItems.SelectedIndex = index; listBoxInspectItems_SelectedIndexChanged(null, null); ShowCount(); // update other letters that are exact matches for (int i = 0; i < _matches.Count; i++) { if (i != index && i < _imageSources.Count && _matches[i].ExpandCount == 0) { var newMatch = _binOcrDb.FindExactMatch(new BinaryOcrBitmap(new NikseBitmap(_imageSources[i]))); if (newMatch >= 0 && _binOcrDb.CompareImages[newMatch].Hash == bob.Hash) { _matches[i].Name = bob.Key; _matches[i].ExpandCount = 0; _matches[i].Italic = checkBoxItalic.Checked; _matches[i].Text = textBoxText.Text; listBoxInspectItems.Items[i] = textBoxText.Text; } } } return; } if (_selectedCompareNode != null) { XmlNode newNode = ImageCompareDocument.CreateElement("Item"); var text = newNode.OwnerDocument.CreateAttribute("Text"); text.InnerText = textBoxText.Text; newNode.Attributes.Append(text); string databaseName = Path.Combine(_directoryPath, "Images.db"); FileStream f; long pos; if (!File.Exists(databaseName)) { using (f = new FileStream(databaseName, FileMode.Create)) { pos = f.Position; new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f); } } else { using (f = new FileStream(databaseName, FileMode.Append)) { pos = f.Position; new ManagedBitmap(pictureBoxInspectItem.Image as Bitmap).AppendToStream(f); } } string name = pos.ToString(CultureInfo.InvariantCulture); newNode.InnerText = name; SetItalic(newNode); ImageCompareDocument.DocumentElement.AppendChild(newNode); int index = listBoxInspectItems.SelectedIndex; _matches[index].Name = name; _matches[index].ExpandCount = 0; _matches[index].Italic = checkBoxItalic.Checked; _matches[index].Text = textBoxText.Text; listBoxInspectItems.Items.Clear(); for (int i = 0; i < _matches.Count; i++) { listBoxInspectItems.Items.Add(_matches[i].Text); } listBoxInspectItems.SelectedIndex = index; ShowCount(); listBoxInspectItems_SelectedIndexChanged(null, null); } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageInfo.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } _selectedCompareNode = null; _selectedCompareBinaryOcrBitmap = null; var img = _imageSources[listBoxInspectItems.SelectedIndex]; pictureBoxInspectItem.Image = img; if (img != null) { pictureBoxInspectItem.Width = img.Width + 2; pictureBoxInspectItem.Height = img.Height + 2; } pictureBoxCompareBitmap.Image = null; pictureBoxCompareBitmapDouble.Image = null; int index = listBoxInspectItems.SelectedIndex; var match = _matches[index]; _selectedMatch = match; if (!string.IsNullOrEmpty(match.Name)) { if (_binOcrDb != null) { bool bobFound = false; foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImages) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; SetDbItemView(bob.ToOldBitmap()); var matchBob = new BinaryOcrBitmap(new NikseBitmap(_imageSources[listBoxInspectItems.SelectedIndex])); if (matchBob.Hash == bob.Hash && matchBob.Width == bob.Width && matchBob.Height == bob.Height && matchBob.NumberOfColoredPixels == bob.NumberOfColoredPixels) { buttonAddBetterMatch.Enabled = false; // exact match } else { buttonAddBetterMatch.Enabled = true; } bobFound = true; break; } } if (!bobFound) { foreach (BinaryOcrBitmap bob in _binOcrDb.CompareImagesExpanded) { if (match.Name == bob.Key) { textBoxText.Text = bob.Text; checkBoxItalic.Checked = bob.Italic; _selectedCompareBinaryOcrBitmap = bob; var oldBitmap = bob.ToOldBitmap(); SetDbItemView(oldBitmap); int dif = 1; if (oldBitmap.Width == match.ImageSplitterItem.NikseBitmap.Width && oldBitmap.Height == match.ImageSplitterItem.NikseBitmap.Height) { dif = NikseBitmapImageSplitter.IsBitmapsAlike(match.ImageSplitterItem.NikseBitmap, oldBitmap); } buttonAddBetterMatch.Enabled = dif > 0; // if exact match then don't allow "Add better match" labelExpandCount.Text = $"Expand count: {bob.ExpandCount}"; break; } } } } else { foreach (XmlNode node in ImageCompareDocument.DocumentElement.ChildNodes) { if (node.Attributes["Text"] != null && node.InnerText == match.Name) { string text = node.Attributes["Text"].InnerText; textBoxText.Text = text; checkBoxItalic.Checked = node.Attributes["Italic"] != null; string databaseName = Path.Combine(_directoryPath, "Images.db"); using (var f = new FileStream(databaseName, FileMode.Open)) { try { string name = node.InnerText; int pos = Convert.ToInt32(name); f.Position = pos; var mbmp = new ManagedBitmap(f); var bitmap = mbmp.ToOldBitmap(); SetDbItemView(mbmp.ToOldBitmap()); labelImageInfo.Text = string.Format(Configuration.Settings.Language.VobSubEditCharacters.Image + " - {0}x{1}", bitmap.Width, bitmap.Height); } catch (Exception exception) { labelImageInfo.Text = Configuration.Settings.Language.VobSubEditCharacters.Image; MessageBox.Show(exception.Message); } } _selectedCompareNode = node; break; } } } } buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.AddBetterMatch; if (_selectedMatch.Text == Configuration.Settings.Language.VobSubOcr.NoMatch) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = Configuration.Settings.Language.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = true; pictureBoxCompareBitmap.Visible = true; pictureBoxCompareBitmapDouble.Visible = true; labelDoubleSize.Visible = true; } else if (_selectedCompareNode == null && _selectedCompareBinaryOcrBitmap == null) { buttonUpdate.Enabled = false; buttonDelete.Enabled = false; buttonAddBetterMatch.Enabled = true; textBoxText.Enabled = true; textBoxText.Text = string.Empty; checkBoxItalic.Enabled = false; pictureBoxCompareBitmap.Visible = false; pictureBoxCompareBitmapDouble.Visible = false; labelDoubleSize.Visible = false; if (img == null) { buttonAddBetterMatch.Enabled = false; } } else { buttonUpdate.Enabled = true; buttonDelete.Enabled = true; if (_selectedCompareNode != null) { buttonAddBetterMatch.Enabled = true; } textBoxText.Enabled = true; checkBoxItalic.Enabled = true; pictureBoxCompareBitmap.Visible = true; pictureBoxCompareBitmapDouble.Visible = true; labelDoubleSize.Visible = true; } }
private void BinaryOcrTrain_Shown(object sender, EventArgs e) { if (string.IsNullOrEmpty(_autoDetectFontText)) { return; } SelectAll_Click(null, null); int numberOfCharactersLeaned = 0; int numberOfCharactersSkipped = 0; foreach (ListViewItem fontItem in listViewFonts.CheckedItems) { _subtitleFontName = fontItem.Text; labelInfo.Text = $"Checking font '{_subtitleFontName}'... {AutoDetectedFonts.Count} hits"; labelInfo.Refresh(); Application.DoEvents(); for (_subtitleFontSize = 20; _subtitleFontSize <= 100; _subtitleFontSize++) { var charactersLearned = new List <string>(); if (!string.IsNullOrEmpty(_autoDetectFontText)) { var s = _autoDetectFontText; var bicDb = new BinaryOcrDb(null); TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, false, false); if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0) { 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); if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0) { AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " italic"); } if (checkBoxBold.Checked) { bicDb = new BinaryOcrDb(null); TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, bicDb, charactersLearned, s, true, false); if (bicDb.FindExactMatch(_autoDetectFontBob) >= 0) { AutoDetectedFonts.Add(_subtitleFontName + " " + _subtitleFontSize + " bold"); } } } } } DialogResult = DialogResult.OK; }