Working on ocr

This commit is contained in:
nikse.dk 2014-02-20 09:14:23 +01:00
parent 8407d4320d
commit 9a73eb4da1
6 changed files with 140 additions and 19 deletions

View File

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.comboBoxTexts = new System.Windows.Forms.ComboBox();
this.listBoxFileNames = new System.Windows.Forms.ListBox();
this.buttonOK = new System.Windows.Forms.Button();
@ -45,9 +46,13 @@
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.buttonCancel = new System.Windows.Forms.Button();
this.labelCount = new System.Windows.Forms.Label();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.saveImageAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.groupBoxCurrentCompareImage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// comboBoxTexts
@ -202,6 +207,7 @@
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Red;
this.pictureBox1.ContextMenuStrip = this.contextMenuStrip1;
this.pictureBox1.Location = new System.Drawing.Point(17, 104);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(52, 52);
@ -230,6 +236,20 @@
this.labelCount.TabIndex = 8;
this.labelCount.Text = "labelCount";
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.saveImageAsToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(158, 48);
//
// saveImageAsToolStripMenuItem
//
this.saveImageAsToolStripMenuItem.Name = "saveImageAsToolStripMenuItem";
this.saveImageAsToolStripMenuItem.Size = new System.Drawing.Size(157, 22);
this.saveImageAsToolStripMenuItem.Text = "Save image as...";
this.saveImageAsToolStripMenuItem.Click += new System.EventHandler(this.saveImageAsToolStripMenuItem_Click);
//
// VobSubEditCharacters
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -258,6 +278,7 @@
this.groupBoxCurrentCompareImage.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
@ -282,5 +303,8 @@
private System.Windows.Forms.CheckBox checkBoxItalic;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Label labelCount;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem saveImageAsToolStripMenuItem;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
}
}

View File

@ -560,5 +560,41 @@ namespace Nikse.SubtitleEdit.Forms
public bool ChangesMade { get; set; }
private void saveImageAsToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = Configuration.Settings.Language.VobSubOcr.SaveSubtitleImageAs;
saveFileDialog1.AddExtension = true;
saveFileDialog1.FileName = "Image";
saveFileDialog1.Filter = "PNG image|*.png|BMP image|*.bmp|GIF image|*.gif|TIFF image|*.tiff";
saveFileDialog1.FilterIndex = 0;
DialogResult result = saveFileDialog1.ShowDialog(this);
if (result == DialogResult.OK)
{
Bitmap bmp = pictureBox1.Image as Bitmap;
if (bmp == null)
{
MessageBox.Show("No image!");
return;
}
try
{
if (saveFileDialog1.FilterIndex == 0)
bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
else if (saveFileDialog1.FilterIndex == 1)
bmp.Save(saveFileDialog1.FileName);
else if (saveFileDialog1.FilterIndex == 2)
bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif);
else
bmp.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
}
}
}

View File

@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>172, 17</value>
</metadata>
</root>

View File

@ -1314,7 +1314,6 @@ namespace Nikse.SubtitleEdit.Forms
returnBmp = b;
}
}
return null;
}
else if (_xSubList != null)
{
@ -2514,21 +2513,38 @@ namespace Nikse.SubtitleEdit.Forms
if (list != null && list.Count > listIndex + j && list[listIndex + j].Y < minY)
minY = list[listIndex + j].Y;
}
if (parentBitmap.Height >= compareItem.Height + minY)
if (parentBitmap.Height >= compareItem.Height + minY)
{
var cutBitmap = parentBitmap.CopyRectangle(new Rectangle(targetItem.X, minY, compareItem.Width, compareItem.Height));
int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, cutBitmap);
if (dif < smallestDifference)
List<ImageSplitterItem> expandSelectionList = new List<ImageSplitterItem>();
for (int k=0; k < compareItem.ExpandCount; k++)
{
bool allow = true;
if (Math.Abs(target.Height - compareItem.Height) > 5 && compareItem.Text == "\"")
allow = false;
if (allow)
if (k + listIndex < list.Count)
{
smallestDifference = dif;
smallestIndex = index;
if (dif == 0)
break; // foreach ending
if (list[k + listIndex].NikseBitmap == null)
break;
expandSelectionList.Add(list[k + listIndex]);
}
}
if (expandSelectionList.Count == compareItem.ExpandCount)
{
var cutItem = GetExpandedSelectionNew(parentBitmap, expandSelectionList);
if (cutItem.NikseBitmap.Width == compareItem.Width && cutItem.NikseBitmap.Height == compareItem.Height)
{
//var cutBitmap = parentBitmap.CopyRectangle(new Rectangle(targetItem.X, minY, compareItem.Width, compareItem.Height));
int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem, cutItem.NikseBitmap);
if (dif < smallestDifference)
{
bool allow = true;
if (Math.Abs(target.Height - compareItem.Height) > 5 && compareItem.Text == "\"")
allow = false;
if (allow)
{
smallestDifference = dif;
smallestIndex = index;
if (dif == 0)
break; // foreach ending
}
}
}
}
}
@ -3595,7 +3611,7 @@ namespace Nikse.SubtitleEdit.Forms
index++;
expandSelectionList.Add(list[index]);
}
item = GetExpandedSelection(parentBitmap, expandSelectionList, checkBoxRightToLeft.Checked);
item = GetExpandedSelectionNew(parentBitmap, expandSelectionList);
_vobSubOcrCharacter.Initialize(bitmap, item, _manualOcrDialogPosition, _italicCheckedLast, expandSelectionList.Count > 1, null, _lastAdditions, this);
DialogResult result = _vobSubOcrCharacter.ShowDialog(this);
@ -4345,6 +4361,39 @@ namespace Nikse.SubtitleEdit.Forms
}
}
internal static ImageSplitterItem GetExpandedSelectionNew(NikseBitmap bitmap, List<ImageSplitterItem> expandSelectionList)
{
int minimumX = expandSelectionList[0].X;
int maximumX = expandSelectionList[expandSelectionList.Count - 1].X + expandSelectionList[expandSelectionList.Count - 1].NikseBitmap.Width;
int minimumY = expandSelectionList[0].Y;
int maximumY = expandSelectionList[0].Y + expandSelectionList[0].NikseBitmap.Height;
NikseBitmap nbmp = new NikseBitmap(bitmap.Width, bitmap.Height);
nbmp = new NikseBitmap(bitmap);
nbmp.Fill(Color.Transparent);
foreach (ImageSplitterItem item in expandSelectionList)
{
for (int y = 0; y < item.NikseBitmap.Height; y++)
{
for (int x = 0; x < item.NikseBitmap.Width; x++)
{
int a = item.NikseBitmap.GetAlpha(x, y);
if (a > 100)
nbmp.SetPixel(item.X + x, item.Y + y, Color.White);
}
}
if (item.Y < minimumY)
minimumY = item.Y;
if (item.Y + item.NikseBitmap.Height > maximumY)
maximumY = item.Y + item.NikseBitmap.Height;
}
nbmp.CropTransparentSidesAndBottom(0, true);
int topCropping;
nbmp = NikseBitmapImageSplitter.CropTopAndBottom(nbmp, out topCropping);
return new ImageSplitterItem(minimumX, minimumY, nbmp);
}
private static string GetStringWithItalicTags(List<CompareMatch> matches)
{
StringBuilder paragraph = new StringBuilder();

View File

@ -554,7 +554,7 @@ namespace Nikse.SubtitleEdit.Logic
if (alpha != 0)
{
done = true;
newHeight = y + maximumCropping;
newHeight = y + maximumCropping + 1;
if (newHeight > Height)
newHeight = Height;
}

View File

@ -58,7 +58,7 @@ namespace Nikse.SubtitleEdit.Logic
}
}
if (allTransparent == false)
return bmp.CopyRectangle(new Rectangle(0, startTop, bmp.Width - 1, y-startTop+1));
return bmp.CopyRectangle(new Rectangle(0, startTop, bmp.Width, y-startTop+1));
}
return bmp;
}
@ -676,15 +676,17 @@ namespace Nikse.SubtitleEdit.Logic
width = FindMaxX(points, x) - startX;
width++;
NikseBitmap b1 = bmp0.CopyRectangle(new Rectangle(startX, 0, width, bmp.Height));
// b1.GetBitmap().Save(@"C:\data\" + x.ToString().PadLeft(3, '0') + ".bmp");
int addY;
b1 = CropTopAndBottom(b1, out addY);
if (spacePixels >= xOrMorePixelsMakesSpace && parts.Count > 0)
parts.Add(new ImageSplitterItem(" "));
int addY;
b1 = CropTopAndBottom(b1, out addY);
parts.Add(new ImageSplitterItem(startX + lineSplitterItem.X, addY + lineSplitterItem.Y, b1)); //y is what?
// remove pixels before next letter;
int begin = 0;
foreach (Point p in points)
@ -755,11 +757,15 @@ namespace Nikse.SubtitleEdit.Logic
{
x--;
y--;
while (points.Count > 0 && points[points.Count - 1].Y > y)
points.RemoveAt(points.Count - 1);
}
else if (y > 5 && bmp.GetAlpha(x - 1, y - 2) == 0)
{
x--;
y -= 2;
while (points.Count > 0 && points[points.Count - 1].Y > y)
points.RemoveAt(points.Count - 1);
}
else
{