Add thresshold setting UI for binary image for Tesseract4

This commit is contained in:
Nikolaj Olsson 2018-11-15 16:57:29 +01:00
parent 490a8ff1c2
commit ccb9e64ed8
6 changed files with 447 additions and 16 deletions

View File

@ -45,8 +45,8 @@
this.groupBoxCropping = new System.Windows.Forms.GroupBox();
this.checkBoxCropTransparent = new System.Windows.Forms.CheckBox();
this.groupBoxColors = new System.Windows.Forms.GroupBox();
this.checkBoxInvertColors = new System.Windows.Forms.CheckBox();
this.checkBoxYellowToWhite = new System.Windows.Forms.CheckBox();
this.checkBoxInvertColors = new System.Windows.Forms.CheckBox();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownThreshold)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxSubtitleImage)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
@ -73,7 +73,7 @@
0,
0});
this.numericUpDownThreshold.Minimum = new decimal(new int[] {
50,
1,
0,
0,
0});
@ -240,6 +240,16 @@
this.groupBoxColors.TabStop = false;
this.groupBoxColors.Text = "Colors";
//
// checkBoxYellowToWhite
//
this.checkBoxYellowToWhite.AutoSize = true;
this.checkBoxYellowToWhite.Location = new System.Drawing.Point(9, 42);
this.checkBoxYellowToWhite.Name = "checkBoxYellowToWhite";
this.checkBoxYellowToWhite.Size = new System.Drawing.Size(97, 17);
this.checkBoxYellowToWhite.TabIndex = 28;
this.checkBoxYellowToWhite.Text = "Yellow to white";
this.checkBoxYellowToWhite.UseVisualStyleBackColor = true;
//
// checkBoxInvertColors
//
this.checkBoxInvertColors.AutoSize = true;
@ -251,17 +261,7 @@
this.checkBoxInvertColors.UseVisualStyleBackColor = true;
this.checkBoxInvertColors.CheckedChanged += new System.EventHandler(this.checkBoxInvertColors_CheckedChanged);
//
// checkBoxYellowToWhite
//
this.checkBoxYellowToWhite.AutoSize = true;
this.checkBoxYellowToWhite.Location = new System.Drawing.Point(9, 42);
this.checkBoxYellowToWhite.Name = "checkBoxYellowToWhite";
this.checkBoxYellowToWhite.Size = new System.Drawing.Size(97, 17);
this.checkBoxYellowToWhite.TabIndex = 28;
this.checkBoxYellowToWhite.Text = "Yellow to white";
this.checkBoxYellowToWhite.UseVisualStyleBackColor = true;
//
// SetForeColorThreshold
// OcrPreprocessingSettings
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@ -279,7 +279,7 @@
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(790, 481);
this.Name = "SetForeColorThreshold";
this.Name = "OcrPreprocessingSettings";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "OCR image preprocessing";

View File

@ -0,0 +1,201 @@
namespace Nikse.SubtitleEdit.Forms.Ocr
{
partial class OcrPreprocessingT4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.labelDescription = new System.Windows.Forms.Label();
this.numericUpDownThreshold = new System.Windows.Forms.NumericUpDown();
this.pictureBoxSubtitleImage = new System.Windows.Forms.PictureBox();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonOK = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBoxBinaryImageCompareThresshold = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownThreshold)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxSubtitleImage)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.groupBoxBinaryImageCompareThresshold.SuspendLayout();
this.SuspendLayout();
//
// labelDescription
//
this.labelDescription.AutoSize = true;
this.labelDescription.Location = new System.Drawing.Point(17, 58);
this.labelDescription.Name = "labelDescription";
this.labelDescription.Size = new System.Drawing.Size(372, 13);
this.labelDescription.TabIndex = 16;
this.labelDescription.Text = "Adjust value until text is shown clearly (normally values between 200 and 300)";
//
// numericUpDownThreshold
//
this.numericUpDownThreshold.Location = new System.Drawing.Point(20, 32);
this.numericUpDownThreshold.Maximum = new decimal(new int[] {
765,
0,
0,
0});
this.numericUpDownThreshold.Minimum = new decimal(new int[] {
1,
0,
0,
0});
this.numericUpDownThreshold.Name = "numericUpDownThreshold";
this.numericUpDownThreshold.Size = new System.Drawing.Size(57, 20);
this.numericUpDownThreshold.TabIndex = 15;
this.numericUpDownThreshold.Value = new decimal(new int[] {
200,
0,
0,
0});
this.numericUpDownThreshold.ValueChanged += new System.EventHandler(this.numericUpDownThreshold_ValueChanged);
//
// pictureBoxSubtitleImage
//
this.pictureBoxSubtitleImage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBoxSubtitleImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxSubtitleImage.Location = new System.Drawing.Point(12, 247);
this.pictureBoxSubtitleImage.Name = "pictureBoxSubtitleImage";
this.pictureBoxSubtitleImage.Size = new System.Drawing.Size(804, 135);
this.pictureBoxSubtitleImage.TabIndex = 14;
this.pictureBoxSubtitleImage.TabStop = false;
this.pictureBoxSubtitleImage.Click += new System.EventHandler(this.pictureBoxSubtitleImage_Click);
//
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonCancel.Location = new System.Drawing.Point(741, 558);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 21);
this.buttonCancel.TabIndex = 13;
this.buttonCancel.Text = "C&ancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// buttonOK
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonOK.Location = new System.Drawing.Point(660, 558);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 21);
this.buttonOK.TabIndex = 12;
this.buttonOK.Text = "&OK";
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Location = new System.Drawing.Point(12, 412);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(804, 140);
this.pictureBox1.TabIndex = 17;
this.pictureBox1.TabStop = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 231);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(73, 13);
this.label2.TabIndex = 21;
this.label2.Text = "Original image";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 396);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(132, 13);
this.label3.TabIndex = 22;
this.label3.Text = "Image after pre-processing";
//
// groupBoxBinaryImageCompareThresshold
//
this.groupBoxBinaryImageCompareThresshold.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBoxBinaryImageCompareThresshold.Controls.Add(this.numericUpDownThreshold);
this.groupBoxBinaryImageCompareThresshold.Controls.Add(this.labelDescription);
this.groupBoxBinaryImageCompareThresshold.Location = new System.Drawing.Point(15, 12);
this.groupBoxBinaryImageCompareThresshold.Name = "groupBoxBinaryImageCompareThresshold";
this.groupBoxBinaryImageCompareThresshold.Size = new System.Drawing.Size(801, 216);
this.groupBoxBinaryImageCompareThresshold.TabIndex = 29;
this.groupBoxBinaryImageCompareThresshold.TabStop = false;
//
// OcrPreprocessingT4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(828, 591);
this.Controls.Add(this.groupBoxBinaryImageCompareThresshold);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.pictureBoxSubtitleImage);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(790, 481);
this.Name = "OcrPreprocessingT4";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "OCR image preprocessing";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SetForeColorThreshold_KeyDown);
((System.ComponentModel.ISupportInitialize)(this.numericUpDownThreshold)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxSubtitleImage)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.groupBoxBinaryImageCompareThresshold.ResumeLayout(false);
this.groupBoxBinaryImageCompareThresshold.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label labelDescription;
private System.Windows.Forms.NumericUpDown numericUpDownThreshold;
private System.Windows.Forms.PictureBox pictureBoxSubtitleImage;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBoxBinaryImageCompareThresshold;
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Core;
using Nikse.SubtitleEdit.Logic.Ocr;
using Bitmap = System.Drawing.Bitmap;
namespace Nikse.SubtitleEdit.Forms.Ocr
{
public partial class OcrPreprocessingT4 : Form
{
private readonly bool _isBinaryImageCompare;
private readonly NikseBitmap _source;
public PreprocessingSettings PreprocessingSettings { get; }
public OcrPreprocessingT4(Bitmap bitmap, bool isBinaryImageCompare, PreprocessingSettings preprocessingSettings)
{
_isBinaryImageCompare = isBinaryImageCompare;
InitializeComponent();
_source = new NikseBitmap(bitmap);
pictureBoxSubtitleImage.Image = bitmap;
if (preprocessingSettings != null)
{
PreprocessingSettings = preprocessingSettings;
}
else
{
PreprocessingSettings = new PreprocessingSettings
{
BinaryImageCompareThresshold = Configuration.Settings.Tools.OcrBinaryImageCompareRgbThreshold
};
}
numericUpDownThreshold.Value = PreprocessingSettings.BinaryImageCompareThresshold;
RefreshImage();
}
private void numericUpDownThreshold_ValueChanged(object sender, EventArgs e)
{
RefreshImage();
}
private void RefreshImage()
{
PreprocessingSettings.BinaryImageCompareThresshold = (int)numericUpDownThreshold.Value;
pictureBox1.Image?.Dispose();
var n = new NikseBitmap(_source);
n.MakeTwoColor((int)numericUpDownThreshold.Value, Color.White, Color.Black);
pictureBox1.Image = n.GetBitmap();
}
private void buttonOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void pictureBoxSubtitleImage_Click(object sender, EventArgs e)
{
var bmp = pictureBoxSubtitleImage.Image as Bitmap;
if (bmp == null)
return;
}
private void SetForeColorThreshold_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
}
}
private void checkBoxCropTransparent_CheckedChanged(object sender, EventArgs e)
{
RefreshImage();
}
}
}

View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="colorDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -6933,8 +6933,6 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
nOcrTrainingToolStripMenuItem.Visible = useNocrCompare;
toolStripSeparatorImageCompare.Visible = useNocrCompare || enableIfImageCompare;
setForecolorThresholdToolStripMenuItem.Visible = _ocrMethodIndex != _ocrMethodTesseract4;
}
private void SaveImageAsToolStripMenuItemClick(object sender, EventArgs e)
@ -8059,6 +8057,22 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
var bmp = GetSubtitleBitmap(_selectedIndex);
_preprocessingSettings = temp;
_fromMenuItem = false;
if (_ocrMethodIndex == _ocrMethodTesseract4)
{
using (var form = new OcrPreprocessingT4(bmp, _ocrMethodIndex == _ocrMethodBinaryImageCompare, new PreprocessingSettings { BinaryImageCompareThresshold = Configuration.Settings.Tools.OcrTesseract4RgbThreshold }))
{
if (form.ShowDialog(this) == DialogResult.OK)
{
ResetTesseractThread();
_preprocessingSettings = form.PreprocessingSettings;
Configuration.Settings.Tools.OcrTesseract4RgbThreshold = _preprocessingSettings.BinaryImageCompareThresshold;
SubtitleListView1SelectedIndexChanged(null, null);
}
}
return;
}
using (var form = new OcrPreprocessingSettings(bmp, _ocrMethodIndex == _ocrMethodBinaryImageCompare, _preprocessingSettings))
{
if (form.ShowDialog(this) == DialogResult.OK)

View File

@ -548,6 +548,12 @@
<Compile Include="Forms\Ocr\GetTesseract302Dictionaries.Designer.cs">
<DependentUpon>GetTesseract302Dictionaries.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Ocr\OcrPreprocessingT4.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\Ocr\OcrPreprocessingT4.Designer.cs">
<DependentUpon>OcrPreprocessingT4.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Ocr\OcrPreprocessingSettings.cs">
<SubType>Form</SubType>
</Compile>
@ -1244,6 +1250,9 @@
<EmbeddedResource Include="Forms\Ocr\GetTesseract302Dictionaries.resx">
<DependentUpon>GetTesseract302Dictionaries.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\Ocr\OcrPreprocessingT4.resx">
<DependentUpon>OcrPreprocessingT4.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\Ocr\OcrPreprocessingSettings.resx">
<DependentUpon>OcrPreprocessingSettings.cs</DependentUpon>
</EmbeddedResource>