From 493eb648cec40c1a7a700716db0d4fd7d1cc63fb Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 29 May 2016 09:32:42 +0200 Subject: [PATCH] Minor improvement for ocr method "Binary image compare" regarding "expanded matches" --- src/Forms/VobSubOcr.cs | 59 +++++++++++-- src/Forms/VobSubOcrCharacter.Designer.cs | 100 ++++++++++++----------- src/Forms/VobSubOcrCharacter.cs | 10 ++- src/Forms/VobSubOcrCharacter.resx | 45 ++++++++++ 4 files changed, 154 insertions(+), 60 deletions(-) diff --git a/src/Forms/VobSubOcr.cs b/src/Forms/VobSubOcr.cs index 9ced1a2ab..5a6e05221 100644 --- a/src/Forms/VobSubOcr.cs +++ b/src/Forms/VobSubOcr.cs @@ -3083,19 +3083,27 @@ namespace Nikse.SubtitleEdit.Forms for (int k = 0; k < _binaryOcrDb.CompareImagesExpanded.Count; k++) { var b = _binaryOcrDb.CompareImagesExpanded[k]; - if (bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) + if ((bob.Hash == b.Hash && bob.Width == b.Width && bob.Height == b.Height && bob.NumberOfColoredPixels == b.NumberOfColoredPixels) || GetPixelDifPercentage(b, bob, target) <= maxDiff) { bool ok = false; for (int i = 0; i < b.ExpandedList.Count; i++) { - if (listIndex + i + 1 < list.Count && list[listIndex + i + 1].NikseBitmap != null && b.ExpandedList[i].Hash == new BinaryOcrBitmap(list[listIndex + i + 1].NikseBitmap).Hash) + if (listIndex + i + 1 < list.Count && list[listIndex + i + 1].NikseBitmap != null) { - ok = true; - } - else - { - ok = false; - break; + var bobNext = new BinaryOcrBitmap(list[listIndex + i + 1].NikseBitmap); + if (b.ExpandedList[i].Hash == bobNext.Hash) + { + ok = true; + } + else if (GetPixelDifPercentage(b.ExpandedList[i], bobNext, list[listIndex + i + 1].NikseBitmap) <= maxDiff) + { + ok = true; + } + else + { + ok = false; + break; + } } } if (ok) @@ -3198,7 +3206,7 @@ namespace Nikse.SubtitleEdit.Forms } if (bob.IsLowercaseJ()) // "j" detection must be before "i" { - return new CompareMatch("j", false, 0, null); + return new CompareMatch("j", false, 0, null); } bool italicLowercaseI; if (bob.IsLowercaseI(out italicLowercaseI)) @@ -3221,6 +3229,37 @@ namespace Nikse.SubtitleEdit.Forms return null; } + private double GetPixelDifPercentage(BinaryOcrBitmap expanded, BinaryOcrBitmap bobNext, NikseBitmap nbmpNext) + { + var difColoredPercentage = (Math.Abs(expanded.NumberOfColoredPixels - bobNext.NumberOfColoredPixels)) * 100.0 / (bobNext.Width * bobNext.Height); + if (difColoredPercentage > 1 && expanded.Width < 3 || bobNext.Width < 3) + return 100; + + int dif = int.MaxValue; + if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width) + { + dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded); + } + else if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width + 1) + { + dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded); + } + else if (expanded.Height == bobNext.Height && expanded.Width == bobNext.Width - 1) + { + dif = NikseBitmapImageSplitter.IsBitmapsAlike(expanded, nbmpNext); + } + else if (expanded.Width == bobNext.Width && expanded.Height == bobNext.Height + 1) + { + dif = NikseBitmapImageSplitter.IsBitmapsAlike(nbmpNext, expanded); + } + else if (expanded.Width == bobNext.Width && expanded.Height == bobNext.Height - 1) + { + dif = NikseBitmapImageSplitter.IsBitmapsAlike(expanded, nbmpNext); + } + var percentage = dif * 100.0 / (bobNext.Width * bobNext.Height); + return percentage; + } + public static Bitmap CopyBitmapSection(Bitmap srcBitmap, Rectangle section) { var bmp = new Bitmap(section.Width, section.Height); @@ -5571,6 +5610,7 @@ namespace Nikse.SubtitleEdit.Forms { if (_compareBitmaps == null) LoadImageCompareBitmaps(); + _numericUpDownMaxErrorPct = (double)numericUpDownMaxErrorPct.Value; } else if (_ocrMethodIndex == _ocrMethodNocr) { @@ -5624,6 +5664,7 @@ namespace Nikse.SubtitleEdit.Forms } _nOcrDb = new NOcrDb(_binaryOcrDb.FileName.Replace(".db", ".nocr")); checkBoxNOcrCorrect.Checked = true; + _numericUpDownMaxErrorPct = (double)numericUpDownMaxErrorPct.Value; } progressBar1.Maximum = max; diff --git a/src/Forms/VobSubOcrCharacter.Designer.cs b/src/Forms/VobSubOcrCharacter.Designer.cs index 20b666e8d..42b5166ae 100644 --- a/src/Forms/VobSubOcrCharacter.Designer.cs +++ b/src/Forms/VobSubOcrCharacter.Designer.cs @@ -29,21 +29,21 @@ private void InitializeComponent() { this.components = new System.ComponentModel.Container(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle7 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle8 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle9 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle10 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle11 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle12 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle13 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle14 = new System.Windows.Forms.DataGridViewCellStyle(); - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle15 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle61 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle62 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle63 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle64 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle65 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle66 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle67 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle68 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle69 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle70 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle71 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle72 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle73 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle74 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle75 = new System.Windows.Forms.DataGridViewCellStyle(); this.buttonOK = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); this.labelSubtitleImage = new System.Windows.Forms.Label(); @@ -1032,11 +1032,11 @@ // this.labelItalicOn.Anchor = System.Windows.Forms.AnchorStyles.Top; this.labelItalicOn.AutoSize = true; - this.labelItalicOn.Font = new System.Drawing.Font("Tahoma", 9.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.labelItalicOn.Font = new System.Drawing.Font("Tahoma", 28F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.labelItalicOn.ForeColor = System.Drawing.Color.Red; - this.labelItalicOn.Location = new System.Drawing.Point(193, 10); + this.labelItalicOn.Location = new System.Drawing.Point(193, 1); this.labelItalicOn.Name = "labelItalicOn"; - this.labelItalicOn.Size = new System.Drawing.Size(40, 16); + this.labelItalicOn.Size = new System.Drawing.Size(119, 46); this.labelItalicOn.TabIndex = 26; this.labelItalicOn.Text = "Italic"; // @@ -1048,6 +1048,7 @@ this.dataGridView1.AllowUserToResizeRows = false; this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView1.BackgroundColor = System.Drawing.Color.DimGray; this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.ColumnHeadersVisible = false; @@ -1074,15 +1075,16 @@ this.dataGridView1.ReadOnly = true; this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing; + this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect; - this.dataGridView1.Size = new System.Drawing.Size(553, 48); + this.dataGridView1.Size = new System.Drawing.Size(553, 49); this.dataGridView1.TabIndex = 27; this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick); // // Column1 // - dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column1.DefaultCellStyle = dataGridViewCellStyle1; + dataGridViewCellStyle61.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column1.DefaultCellStyle = dataGridViewCellStyle61; this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; this.Column1.ReadOnly = true; @@ -1091,8 +1093,8 @@ // // Column2 // - dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column2.DefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle62.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column2.DefaultCellStyle = dataGridViewCellStyle62; this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; this.Column2.ReadOnly = true; @@ -1101,8 +1103,8 @@ // // Column3 // - dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column3.DefaultCellStyle = dataGridViewCellStyle3; + dataGridViewCellStyle63.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column3.DefaultCellStyle = dataGridViewCellStyle63; this.Column3.HeaderText = "Column3"; this.Column3.Name = "Column3"; this.Column3.ReadOnly = true; @@ -1111,8 +1113,8 @@ // // Column4 // - dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column4.DefaultCellStyle = dataGridViewCellStyle4; + dataGridViewCellStyle64.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column4.DefaultCellStyle = dataGridViewCellStyle64; this.Column4.HeaderText = "Column4"; this.Column4.Name = "Column4"; this.Column4.ReadOnly = true; @@ -1121,8 +1123,8 @@ // // Column5 // - dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column5.DefaultCellStyle = dataGridViewCellStyle5; + dataGridViewCellStyle65.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column5.DefaultCellStyle = dataGridViewCellStyle65; this.Column5.HeaderText = "Column5"; this.Column5.Name = "Column5"; this.Column5.ReadOnly = true; @@ -1131,8 +1133,8 @@ // // Column6 // - dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column6.DefaultCellStyle = dataGridViewCellStyle6; + dataGridViewCellStyle66.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column6.DefaultCellStyle = dataGridViewCellStyle66; this.Column6.HeaderText = "Column6"; this.Column6.Name = "Column6"; this.Column6.ReadOnly = true; @@ -1141,8 +1143,8 @@ // // Column7 // - dataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column7.DefaultCellStyle = dataGridViewCellStyle7; + dataGridViewCellStyle67.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column7.DefaultCellStyle = dataGridViewCellStyle67; this.Column7.HeaderText = "Column7"; this.Column7.Name = "Column7"; this.Column7.ReadOnly = true; @@ -1151,8 +1153,8 @@ // // Column8 // - dataGridViewCellStyle8.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column8.DefaultCellStyle = dataGridViewCellStyle8; + dataGridViewCellStyle68.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column8.DefaultCellStyle = dataGridViewCellStyle68; this.Column8.HeaderText = "Column8"; this.Column8.Name = "Column8"; this.Column8.ReadOnly = true; @@ -1161,8 +1163,8 @@ // // Column9 // - dataGridViewCellStyle9.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column9.DefaultCellStyle = dataGridViewCellStyle9; + dataGridViewCellStyle69.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column9.DefaultCellStyle = dataGridViewCellStyle69; this.Column9.HeaderText = "Column9"; this.Column9.Name = "Column9"; this.Column9.ReadOnly = true; @@ -1171,8 +1173,8 @@ // // Column10 // - dataGridViewCellStyle10.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column10.DefaultCellStyle = dataGridViewCellStyle10; + dataGridViewCellStyle70.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column10.DefaultCellStyle = dataGridViewCellStyle70; this.Column10.HeaderText = "Column10"; this.Column10.Name = "Column10"; this.Column10.ReadOnly = true; @@ -1181,8 +1183,8 @@ // // Column11 // - dataGridViewCellStyle11.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column11.DefaultCellStyle = dataGridViewCellStyle11; + dataGridViewCellStyle71.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column11.DefaultCellStyle = dataGridViewCellStyle71; this.Column11.HeaderText = "Column11"; this.Column11.Name = "Column11"; this.Column11.ReadOnly = true; @@ -1191,8 +1193,8 @@ // // Column12 // - dataGridViewCellStyle12.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column12.DefaultCellStyle = dataGridViewCellStyle12; + dataGridViewCellStyle72.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column12.DefaultCellStyle = dataGridViewCellStyle72; this.Column12.HeaderText = "Column12"; this.Column12.Name = "Column12"; this.Column12.ReadOnly = true; @@ -1201,8 +1203,8 @@ // // Column13 // - dataGridViewCellStyle13.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column13.DefaultCellStyle = dataGridViewCellStyle13; + dataGridViewCellStyle73.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column13.DefaultCellStyle = dataGridViewCellStyle73; this.Column13.HeaderText = "Column13"; this.Column13.Name = "Column13"; this.Column13.ReadOnly = true; @@ -1211,8 +1213,8 @@ // // Column14 // - dataGridViewCellStyle14.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column14.DefaultCellStyle = dataGridViewCellStyle14; + dataGridViewCellStyle74.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column14.DefaultCellStyle = dataGridViewCellStyle74; this.Column14.HeaderText = "Column14"; this.Column14.Name = "Column14"; this.Column14.ReadOnly = true; @@ -1221,8 +1223,8 @@ // // Column15 // - dataGridViewCellStyle15.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; - this.Column15.DefaultCellStyle = dataGridViewCellStyle15; + dataGridViewCellStyle75.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; + this.Column15.DefaultCellStyle = dataGridViewCellStyle75; this.Column15.HeaderText = "Column15"; this.Column15.Name = "Column15"; this.Column15.ReadOnly = true; diff --git a/src/Forms/VobSubOcrCharacter.cs b/src/Forms/VobSubOcrCharacter.cs index b01d2eda7..81b962ff7 100644 --- a/src/Forms/VobSubOcrCharacter.cs +++ b/src/Forms/VobSubOcrCharacter.cs @@ -35,8 +35,8 @@ namespace Nikse.SubtitleEdit.Forms germanToolStripMenuItem.Text = language.German; checkBoxAutoSubmitOfFirstChar.Text = language.AutoSubmitOnFirstChar; - dataGridView1.Rows.Add("♪", "á", "é", "í", "ó", "ö", "ő", "ú", "ü", "ű"); - dataGridView1.Rows.Add("♫", "Á", "É", "Í", "Ó", "Ö", "Ő", "Ú", "Ü", "Ű"); + dataGridView1.Rows.Add("♪", "á", "é", "í", "ó", "ö", "ő", "ú", "ü", "ű", "x", "z"); + dataGridView1.Rows.Add("♫", "Á", "É", "Í", "Ó", "Ö", "Ő", "Ú", "Ü", "Ű", "X", "Z"); UiUtil.FixLargeFonts(this, buttonCancel); } @@ -156,13 +156,19 @@ namespace Nikse.SubtitleEdit.Forms { labelCharactersAsText.Font = new Font(labelCharactersAsText.Font.FontFamily, labelCharactersAsText.Font.Size, FontStyle.Italic); textBoxCharacters.Font = new Font(textBoxCharacters.Font.FontFamily, textBoxCharacters.Font.Size, FontStyle.Italic); + dataGridView1.Font = new Font(dataGridView1.Font.FontFamily, dataGridView1.Font.Size, FontStyle.Italic); labelItalicOn.Visible = true; + checkBoxItalic.Font = new Font(checkBoxItalic.Font.FontFamily, checkBoxItalic.Font.Size, FontStyle.Italic | FontStyle.Bold); + checkBoxItalic.ForeColor = Color.Red; } else { labelCharactersAsText.Font = new Font(labelCharactersAsText.Font.FontFamily, labelCharactersAsText.Font.Size); textBoxCharacters.Font = new Font(textBoxCharacters.Font.FontFamily, textBoxCharacters.Font.Size); + dataGridView1.Font = new Font(dataGridView1.Font.FontFamily, dataGridView1.Font.Size); labelItalicOn.Visible = false; + checkBoxItalic.Font = new Font(checkBoxItalic.Font.FontFamily, checkBoxItalic.Font.Size); + checkBoxItalic.ForeColor = Control.DefaultForeColor; } } diff --git a/src/Forms/VobSubOcrCharacter.resx b/src/Forms/VobSubOcrCharacter.resx index 38acedf9a..7fb32eb32 100644 --- a/src/Forms/VobSubOcrCharacter.resx +++ b/src/Forms/VobSubOcrCharacter.resx @@ -165,4 +165,49 @@ True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + \ No newline at end of file