private void MakeExpandImage() { var splitterItem = _splitterItems[_startIndex]; if (splitterItem.NikseBitmap == null) { return; } var expandList = new List <ImageSplitterItem> { splitterItem }; for (int i = 1; i < listBoxInspectItems.Items.Count; i++) { if (i < numericUpDownExpandCount.Value) { splitterItem = _splitterItems[_startIndex + i + _extraCount]; if (splitterItem.NikseBitmap == null) { break; } expandList.Add(splitterItem); } } ExpandItem = VobSubOcr.GetExpandedSelectionNew(_wholeImage, expandList); var newBmp = ExpandItem.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = newBmp; pictureBoxInspectItem.Width = newBmp.Width; pictureBoxInspectItem.Height = newBmp.Height; }
private void buttonAddBetterMatch_Click(object sender, EventArgs e) { var expandSelectionList = new List <ImageSplitterItem>(); if (listBoxInspectItems.SelectedIndex < 0) { return; } int index = _indexLookup[listBoxInspectItems.SelectedIndex]; var img = _imageList[index]; if (img.NikseBitmap == null) { return; } var match = _matchList[listBoxInspectItems.SelectedIndex]; if (match?.ExpandCount > 1) // expand match { addBetterMultiMatchToolStripMenuItem_Click(null, null); return; } using (var vobSubOcrNOcrCharacter = new VobSubOcrNOcrCharacter()) { var text = textBoxText.Text; vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(-1, -1), checkBoxItalic.Checked, true, expandSelectionList.Count > 1, text); DialogResult result = vobSubOcrNOcrCharacter.ShowDialog(this); var expandSelection = false; var shrinkSelection = false; if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; if (img.NikseBitmap != null) { expandSelectionList.Add(img); } } while (result == DialogResult.OK && (vobSubOcrNOcrCharacter.ShrinkSelection || vobSubOcrNOcrCharacter.ExpandSelection)) { if (expandSelection || shrinkSelection) { expandSelection = false; if (shrinkSelection && index > 0) { shrinkSelection = false; } else if (index + 1 < _imageList.Count && _imageList[index + 1].NikseBitmap != null) // only allow expand to EndOfLine or space { index++; expandSelectionList.Add(_imageList[index]); } img = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandSelectionList); // true } vobSubOcrNOcrCharacter.Initialize(_bitmap, img, new Point(0, 0), checkBoxItalic.Checked, true, expandSelectionList.Count > 1, string.Empty); result = vobSubOcrNOcrCharacter.ShowDialog(this); if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ShrinkSelection) { shrinkSelection = true; index--; if (expandSelectionList.Count > 0) { expandSelectionList.RemoveAt(expandSelectionList.Count - 1); } } else if (result == DialogResult.OK && vobSubOcrNOcrCharacter.ExpandSelection) { expandSelection = true; if (_imageList[index + 1].NikseBitmap != null) { index++; expandSelectionList.Add(_imageList[index]); } } } if (result == DialogResult.OK) { if (expandSelectionList.Count > 1) { vobSubOcrNOcrCharacter.NOcrChar.ExpandCount = expandSelectionList.Count; } _nOcrChars.Add(vobSubOcrNOcrCharacter.NOcrChar); DialogResult = DialogResult.OK; } } }
private void TrainLetter(ref int numberOfCharactersLearned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, string s, bool bold, bool italic, bool doubleLetter) { var bmp = GenerateImageFromTextWithStyle("H " + s, bold, italic); var nikseBitmap = new NikseBitmap(bmp); nikseBitmap.MakeTwoColor(280); nikseBitmap.CropTop(0, Color.FromArgb(0, 0, 0, 0)); var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nikseBitmap, 10, false, false, 25, false); if (list.Count == 3) { var item = list[2]; var match = nOcrD.GetMatch(item.NikseBitmap, item.Top, false, 25); if (match == null || match.Text != s) { labelInfo.Refresh(); Application.DoEvents(); var nOcrChar = new NOcrChar(s) { Width = item.NikseBitmap.Width, Height = item.NikseBitmap.Height, MarginTop = item.Top, Italic = italic, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + (doubleLetter ? 20 : 0), false, nOcrChar, item.NikseBitmap); nOcrD.Add(nOcrChar); numberOfCharactersLearned++; labelInfo.Text = string.Format(Configuration.Settings.Language.VobSubOcr.NowTraining, numberOfCharactersLearned, _subtitleFontName, numberOfCharactersSkipped); bmp.Dispose(); } else { numberOfCharactersSkipped++; } } else if (!doubleLetter) { if (list.Count == 4 && list[2].NikseBitmap != null && list[3].NikseBitmap != null) { // e.g. quote (") var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 2, }; VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 5, false, nOcrChar, expandItem.NikseBitmap); nOcrD.Add(nOcrChar); return; } if (list.Count == 5 && list[2].NikseBitmap != null && list[3].NikseBitmap != null && list[4].NikseBitmap != null) { // e.g. "%" var expandItem = VobSubOcr.GetExpandedSelectionNew(nikseBitmap, new List <ImageSplitterItem> { list[2], list[3], list[4] }); var match = nOcrD.GetMatchExpanded(nikseBitmap, expandItem, 2, list); if (match != null && match.Text == s) { numberOfCharactersSkipped++; return; } var nOcrChar = new NOcrChar(s) { Width = expandItem.NikseBitmap.Width, Height = expandItem.NikseBitmap.Height, MarginTop = expandItem.Top, Italic = italic, ExpandCount = 3, }; nOcrD.Add(nOcrChar); VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value + 10, false, nOcrChar, expandItem.NikseBitmap); return; } numberOfCharactersSkipped++; } }
private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e) { labelImageSize.Text = string.Empty; labelExpandCount.Text = string.Empty; if (listBoxInspectItems.SelectedIndex < 0) { return; } var idx = _indexLookup[listBoxInspectItems.SelectedIndex]; var img = _imageList[idx]; var match = _matchList[listBoxInspectItems.SelectedIndex]; if (img.NikseBitmap != null) { pictureBoxInspectItem.Width = img.NikseBitmap.Width; pictureBoxInspectItem.Height = img.NikseBitmap.Height; labelImageSize.Top = pictureBoxInspectItem.Top + img.NikseBitmap.Height + 7; labelImageSize.Text = img.NikseBitmap.Width + "x" + img.NikseBitmap.Height; if (match != null && match.ExpandCount > 1) { var expandList = new List <ImageSplitterItem>(); for (int i = idx; i < idx + match.ExpandCount; i++) { expandList.Add(_imageList[i]); } var expandItem = VobSubOcr.GetExpandedSelectionNew(new NikseBitmap(_bitmap), expandList); var old = expandItem.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = old; pictureBoxCharacter.Image = old; pictureBoxInspectItem.Width = old.Width; pictureBoxInspectItem.Height = old.Height; labelExpandCount.Text = $"Expand count: {match.ExpandCount}"; } else { var old = img.NikseBitmap.GetBitmap(); pictureBoxInspectItem.Image = old; pictureBoxCharacter.Image = old; } SizePictureBox(); } else { pictureBoxInspectItem.Image = null; pictureBoxCharacter.Image = null; } buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.AddBetterMatch; if (match == null) { if (img.NikseBitmap != null && img.SpecialCharacter == null) { // no match found buttonUpdate.Enabled = false; buttonDelete.Enabled = false; textBoxText.Text = string.Empty; _nOcrChar = null; pictureBoxCharacter.Invalidate(); buttonEditDB.Enabled = true; buttonAddBetterMatch.Enabled = true; buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } else { // 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; buttonAddBetterMatch.Text = LanguageSettings.Current.VobSubOcrCharacterInspect.Add; textBoxText.Enabled = true; checkBoxItalic.Enabled = true; } 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; } }