예제 #1
0
        public void LoadOcrCharacters()
        {
            var list = new List<NOcrChar>();
            var listExpanded = new List<NOcrChar>();

            if (!File.Exists(FileName))
            {
                OcrCharacters = list;
                OcrCharactersExpanded = listExpanded;
                return;
            }

            using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
            {
                bool done = false;
                while (!done)
                {
                    var ocrChar = new NOcrChar(gz);
                    if (ocrChar.LoadedOk)
                    {
                        if (ocrChar.ExpandCount > 0)
                            listExpanded.Add(ocrChar);
                        else
                            list.Add(ocrChar);
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
            OcrCharacters = list;
            OcrCharactersExpanded = listExpanded;
        }
예제 #2
0
 public void Add(NOcrChar ocrChar)
 {
     if (ocrChar.ExpandCount > 0)
         OcrCharactersExpanded.Insert(0, ocrChar);
     else
         OcrCharacters.Insert(0, ocrChar);
 }
예제 #3
0
 public void Add(NOcrChar ocrChar)
 {
     if (ocrChar.ExpandCount > 0)
     {
         OcrCharactersExpanded.Insert(0, ocrChar);
     }
     else
     {
         OcrCharacters.Insert(0, ocrChar);
     }
 }
예제 #4
0
 public void Remove(NOcrChar ocrChar)
 {
     if (ocrChar.ExpandCount > 0)
     {
         OcrCharactersExpanded.Remove(ocrChar);
     }
     else
     {
         OcrCharacters.Remove(ocrChar);
     }
 }
예제 #5
0
 private void AddHistoryItem(NOcrChar nocrChar)
 {
     if (_historyIndex > 0 && _historyIndex < _history.Count - 1)
     {
         while (_history.Count > _historyIndex + 1)
             _history.RemoveAt(_history.Count - 1);
         _historyIndex = _history.Count - 1;
     }
     _history.Add(new NOcrChar(nocrChar));
     _historyIndex++;
 }
예제 #6
0
        public void LoadOcrCharacters()
        {
            var list         = new List <NOcrChar>();
            var listExpanded = new List <NOcrChar>();

            if (!File.Exists(FileName))
            {
                OcrCharacters         = list;
                OcrCharactersExpanded = listExpanded;
                return;
            }

            using (var stream = new MemoryStream())
            {
                using (var gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
                {
                    gz.CopyTo(stream);
                }

                stream.Position = 0;
                bool isVersion2;
                var  versionBuffer = new byte[Version.Length];
                stream.Read(versionBuffer, 0, versionBuffer.Length);
                isVersion2      = Encoding.ASCII.GetString(versionBuffer) == Version;
                stream.Position = 0;
                bool done = false;
                if (isVersion2)
                {
                    stream.Read(versionBuffer, 0, versionBuffer.Length);
                }
                while (!done)
                {
                    var ocrChar = new NOcrChar(stream, isVersion2);
                    if (ocrChar.LoadedOk)
                    {
                        if (ocrChar.ExpandCount > 0)
                        {
                            listExpanded.Add(ocrChar);
                        }
                        else
                        {
                            list.Add(ocrChar);
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
            }

            OcrCharacters         = list;
            OcrCharactersExpanded = listExpanded;
        }
        internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink)
        {
            listBoxLinesForeground.Items.Clear();
            listBoxlinesBackground.Items.Clear();
            NikseBitmap nbmp = new NikseBitmap(vobSubImage);
            nbmp.ReplaceTransparentWith(Color.Black);
            vobSubImage = nbmp.GetBitmap();

            radioButtonHot.Checked = true;
            ShrinkSelection = false;
            ExpandSelection = false;

            textBoxCharacters.Text = string.Empty;
            _nocrChar = new NOcrChar();
            _nocrChar.MarginTop = character.Y - character.ParentY;
            _imageWidth = character.NikseBitmap.Width;
            _imageHeight = character.NikseBitmap.Height;
            _drawLineOn = false;
            _warningNoNotForegroundLinesShown = false;

            buttonShrinkSelection.Visible = showShrink;

            if (position.X != -1 && position.Y != -1)
            {
                StartPosition = FormStartPosition.Manual;
                Left = position.X;
                Top = position.Y;
            }

            pictureBoxSubtitleImage.Image = vobSubImage;
            pictureBoxCharacter.Image = character.NikseBitmap.GetBitmap();

            Bitmap org = (Bitmap)vobSubImage.Clone();
            Bitmap bm = new Bitmap(org.Width, org.Height);
            Graphics g = Graphics.FromImage(bm);
            g.DrawImage(org, 0, 0, org.Width, org.Height);
            g.DrawRectangle(Pens.Red, character.X, character.Y, character.NikseBitmap.Width, character.NikseBitmap.Height - 1);
            g.Dispose();
            pictureBoxSubtitleImage.Image = bm;

            pictureBoxCharacter.Top = labelCharacters.Top + 16;
            SizePictureBox();
            checkBoxItalic.Checked = italicChecked;

            _history = new List<NOcrChar>();
            _historyIndex = -1;

            _nocrChar.Width = _imageWidth;
            _nocrChar.Height = _imageHeight;
            GenerateLineSegments(150, false, _nocrChar, new NikseBitmap(pictureBoxCharacter.Image as Bitmap));
            ShowOcrPoints();
            pictureBoxCharacter.Invalidate();
        }
예제 #8
0
        public static bool IsMatch(NikseBitmap bitmap, NOcrChar oc, int errorsAllowed)
        {
            var index  = 0;
            var errors = 0;

            while (index < oc.LinesForeground.Count)
            {
                var op = oc.LinesForeground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var c = bitmap.GetPixel(point.X, point.Y);
                        if (c.A <= 150 || c.R + c.G + c.B <= VobSubOcr.NocrMinColor)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }

                index++;
            }

            index = 0;
            while (index < oc.LinesBackground.Count)
            {
                var op = oc.LinesBackground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var c = bitmap.GetPixel(point.X, point.Y);
                        if (c.A > 150 && c.R + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }
                index++;
            }

            return(true);
        }
예제 #9
0
        public static bool IsMatch(NikseBitmap bitmap, NOcrChar oc, int errorsAllowed)
        {
            var index  = 0;
            var errors = 0;

            while (index < oc.LinesForeground.Count)
            {
                var op = oc.LinesForeground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var a = bitmap.GetAlpha(point.X, point.Y);
                        if (a <= 150)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }

                index++;
            }

            index = 0;
            while (index < oc.LinesBackground.Count)
            {
                var op = oc.LinesBackground[index];
                foreach (var point in op.ScaledGetPoints(oc, bitmap.Width, bitmap.Height))
                {
                    if (point.X >= 0 && point.Y >= 0 && point.X < bitmap.Width && point.Y < bitmap.Height)
                    {
                        var a = bitmap.GetAlpha(point.X, point.Y);
                        if (a > 150)
                        {
                            errors++;
                            if (errors > errorsAllowed)
                            {
                                return(false);
                            }
                        }
                    }
                }
                index++;
            }

            return(true);
        }
예제 #10
0
 public NOcrChar(NOcrChar old)
 {
     LinesForeground = new List<NOcrPoint>();
     LinesBackground = new List<NOcrPoint>();
     Text = old.Text;
     Width = old.Width;
     Height = old.Height;
     MarginTop = old.MarginTop;
     Italic = old.Italic;
     foreach (NOcrPoint p in old.LinesForeground)
         LinesForeground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y)));
     foreach (NOcrPoint p in old.LinesBackground)
         LinesBackground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y)));
 }
예제 #11
0
        public void TestNOcrSaveLoad()
        {
            string tempFileName = Path.GetTempFileName();
            var db = new NOcrDb(tempFileName);

            var nOcrChar = new NOcrChar("t");
            nOcrChar.ExpandCount = 0;
            nOcrChar.Italic = false;
            nOcrChar.MarginTop = 2;
            nOcrChar.Width = 10;
            nOcrChar.Height = 10;
            nOcrChar.LinesForeground.Add(new NOcrPoint(new Point(1, 1), new Point(2, 2)));
            nOcrChar.LinesBackground.Add(new NOcrPoint(new Point(3, 4), new Point(5, 6)));
            db.Add(nOcrChar);

            var nOcrChar2 = new NOcrChar("u");
            nOcrChar2.ExpandCount = 0;
            nOcrChar2.Italic = false;
            nOcrChar2.MarginTop = 3;
            nOcrChar2.Width = 12;
            nOcrChar2.Height = 12;
            nOcrChar2.LinesForeground.Add(new NOcrPoint(new Point(1, 1), new Point(2, 2)));
            nOcrChar2.LinesBackground.Add(new NOcrPoint(new Point(3, 4), new Point(5, 6)));
            db.Add(nOcrChar2);
            db.Save();

            db = new NOcrDb(tempFileName);
            Assert.IsTrue(db.OcrCharacters.Count == 2);

            Assert.IsTrue(db.OcrCharacters[0].Text == nOcrChar2.Text);
            Assert.IsTrue(db.OcrCharacters[0].Italic == nOcrChar2.Italic);
            Assert.IsTrue(db.OcrCharacters[0].MarginTop == nOcrChar2.MarginTop);
            Assert.IsTrue(db.OcrCharacters[0].LinesForeground.Count == nOcrChar2.LinesForeground.Count);
            Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.X == nOcrChar2.LinesForeground[0].Start.X);
            Assert.IsTrue(db.OcrCharacters[0].LinesForeground[0].Start.Y == nOcrChar2.LinesForeground[0].Start.Y);
            Assert.IsTrue(db.OcrCharacters[0].LinesBackground.Count == nOcrChar2.LinesBackground.Count);
            Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.X == nOcrChar2.LinesBackground[0].Start.X);
            Assert.IsTrue(db.OcrCharacters[0].LinesBackground[0].Start.Y == nOcrChar2.LinesBackground[0].Start.Y);

            Assert.IsTrue(db.OcrCharacters[1].Text == nOcrChar.Text);

            try
            {
                File.Delete(tempFileName);
            }
            catch
            {
            }
        }
예제 #12
0
 public NOcrChar(NOcrChar old)
 {
     LinesForeground = new List <NOcrPoint>();
     LinesBackground = new List <NOcrPoint>();
     Text            = old.Text;
     Width           = old.Width;
     Height          = old.Height;
     MarginTop       = old.MarginTop;
     Italic          = old.Italic;
     foreach (NOcrPoint p in old.LinesForeground)
     {
         LinesForeground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y)));
     }
     foreach (NOcrPoint p in old.LinesBackground)
     {
         LinesBackground.Add(new NOcrPoint(new Point(p.Start.X, p.Start.Y), new Point(p.End.X, p.End.Y)));
     }
 }
예제 #13
0
        public static NOcrChar MakeItalicNOcrChar(NOcrChar oldChar, int movePixelsLeft, double unItalicFactor)
        {
            var c = new NOcrChar();

            foreach (NOcrPoint op in oldChar.LinesForeground)
            {
                c.LinesForeground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor)));
            }

            foreach (NOcrPoint op in oldChar.LinesBackground)
            {
                c.LinesBackground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor)));
            }

            c.Text      = oldChar.Text;
            c.Width     = oldChar.Width;
            c.Height    = oldChar.Height;
            c.MarginTop = oldChar.MarginTop;
            c.Italic    = true;
            return(c);
        }
예제 #14
0
        public void LoadOcrCharacters()
        {
            var list         = new List <NOcrChar>();
            var listExpanded = new List <NOcrChar>();

            if (!File.Exists(FileName))
            {
                OcrCharacters         = list;
                OcrCharactersExpanded = listExpanded;
                return;
            }

            using (Stream gz = new GZipStream(File.OpenRead(FileName), CompressionMode.Decompress))
            {
                bool done = false;
                while (!done)
                {
                    var ocrChar = new NOcrChar(gz);
                    if (ocrChar.LoadedOk)
                    {
                        if (ocrChar.ExpandCount > 0)
                        {
                            listExpanded.Add(ocrChar);
                        }
                        else
                        {
                            list.Add(ocrChar);
                        }
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
            OcrCharacters         = list;
            OcrCharactersExpanded = listExpanded;
        }
예제 #15
0
        private static bool IsMatchPointBackGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar)
        {
            foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height))
            {
                if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                {
                    Color c = nbmp.GetPixel(point.X, point.Y);
                    if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                    {
                        return false;
                    }

                    if (nbmp.Width > 10 && point.X + 1 < nbmp.Width)
                    {
                        c = nbmp.GetPixel(point.X + 1, point.Y);
                        if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                        {
                            return false;
                        }
                    }

                    if (loose)
                    {
                        if (nbmp.Width > 10 && point.X >= 1)
                        {
                            c = nbmp.GetPixel(point.X - 1, point.Y);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }

                        if (nbmp.Height > 10 && point.Y + 1 < nbmp.Height)
                        {
                            c = nbmp.GetPixel(point.X, point.Y + 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }

                        if (nbmp.Height > 10 && point.Y >= 1)
                        {
                            c = nbmp.GetPixel(point.X, point.Y - 1);
                            if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                            {
                                return false;
                            }
                        }
                    }

                }
            }
            return true;
        }
예제 #16
0
 private void Undo()
 {
     _drawLineOn = false;
     _startDone = false;
     if (_history.Count > 0 && _historyIndex > 0)
     {
         _historyIndex--;
         _nocrChar = new NOcrChar(_history[_historyIndex]);
     }
     else if (_historyIndex == 0)
     {
         var c = new NOcrChar(_nocrChar);
         c.LinesForeground.Clear();
         c.LinesBackground.Clear();
         _nocrChar = c;
         _historyIndex--;
     }
     ShowOcrPoints();
 }
예제 #17
0
 private void Redo()
 {
     if (_history.Count > 0 && _historyIndex < _history.Count - 1)
     {
         _historyIndex++;
         _nocrChar = new NOcrChar(_history[_historyIndex]);
         ShowOcrPoints();
     }
 }
 internal Point GetScaledEnd(NOcrChar ocrChar, int width, int height)
 {
     return new Point((int)Math.Round(this.End.X * 100.0 / ocrChar.Width * width / 100.0), (int)Math.Round(this.End.Y * 100.0 / ocrChar.Height * height / 100.0));
 }
예제 #19
0
        public static List<NOcrChar> LoadNOcrForTesseract(string xmlRessourceName)
        {
            var nocrChars = new List<NOcrChar>();
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream strm = asm.GetManifestResourceStream(xmlRessourceName);
            if (strm != null)
            {
                XmlDocument doc = new XmlDocument();
                var rdr = new StreamReader(strm);
                using (var zip = new System.IO.Compression.GZipStream(rdr.BaseStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    byte[] data = new byte[175000];
                    zip.Read(data, 0, 175000);
                    doc.LoadXml(Encoding.UTF8.GetString(data));
                }
                rdr.Close();

                try
                {
                    foreach (XmlNode node in doc.DocumentElement.SelectNodes("Char"))
                    {
                        var oc = new NOcrChar(node.Attributes["Text"].Value);
                        oc.Width = Convert.ToInt32(node.Attributes["Width"].Value, CultureInfo.InvariantCulture);
                        oc.Height = Convert.ToInt32(node.Attributes["Height"].Value, CultureInfo.InvariantCulture);
                        oc.MarginTop = Convert.ToInt32(node.Attributes["MarginTop"].Value, CultureInfo.InvariantCulture);
                        if (node.Attributes["Italic"] != null)
                            oc.Italic = Convert.ToBoolean(node.Attributes["Italic"].Value, CultureInfo.InvariantCulture);
                        if (node.Attributes["ExpandCount"] != null)
                            oc.ExpandCount = Convert.ToInt32(node.Attributes["ExpandCount"].Value, CultureInfo.InvariantCulture);
                        foreach (XmlNode pointNode in node.SelectNodes("Point"))
                        {
                            var op = new NOcrPoint(DecodePoint(pointNode.Attributes["Start"].Value), DecodePoint(pointNode.Attributes["End"].Value));
                            XmlAttribute a = pointNode.Attributes["On"];
                            if (a != null && Convert.ToBoolean(a.Value))
                                oc.LinesForeground.Add(op);
                            else
                                oc.LinesBackground.Add(op);
                        }
                        nocrChars.Add(oc);
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
            return nocrChars;
        }
예제 #20
0
 internal Point GetScaledEnd(NOcrChar ocrChar, int width, int height)
 {
     return(new Point((int)Math.Round(End.X * 100.0 / ocrChar.Width * width / 100.0), (int)Math.Round(End.Y * 100.0 / ocrChar.Height * height / 100.0)));
 }
        /// <summary>
        /// The add history item.
        /// </summary>
        /// <param name="nocrChar">
        /// The nocr char.
        /// </param>
        private void AddHistoryItem(NOcrChar nocrChar)
        {
            if (this._historyIndex > 0 && this._historyIndex < this._history.Count - 1)
            {
                while (this._history.Count > this._historyIndex + 1)
                {
                    this._history.RemoveAt(this._history.Count - 1);
                }

                this._historyIndex = this._history.Count - 1;
            }

            this._history.Add(new NOcrChar(nocrChar));
            this._historyIndex++;
        }
예제 #22
0
        private void TrainLetter(ref int numberOfCharactersLeaned, ref int numberOfCharactersSkipped, NOcrDb nOcrD, List<string> charactersLearned, string s, bool bold)
        {
            Bitmap bmp = GenerateImageFromTextWithStyle(s, bold);
            var nbmp = new NikseBitmap(bmp);
            nbmp.MakeTwoColor(280);
            var list = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, 10, false, false, 25);
            if (list.Count == 1)
            {
                NOcrChar match = nOcrD.GetMatch(list[0].NikseBitmap);
                if (match == null)
                {
                    pictureBox1.Image = list[0].NikseBitmap.GetBitmap();
                    this.Refresh();
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(100);

                    NOcrChar nOcrChar = new NOcrChar(s);
                    nOcrChar.Width = list[0].NikseBitmap.Width;
                    nOcrChar.Height = list[0].NikseBitmap.Height;
                    VobSubOcrNOcrCharacter.GenerateLineSegments((int)numericUpDownSegmentsPerCharacter.Value, checkBoxVeryAccurate.Checked, nOcrChar, list[0].NikseBitmap);
                    nOcrD.Add(nOcrChar);

                    charactersLearned.Add(s);
                    numberOfCharactersLeaned++;
                    labelInfo.Text = string.Format("Now training font '{1}', total characters leaned is {0}, {2} skipped", numberOfCharactersLeaned, _subtitleFontName, numberOfCharactersSkipped);
                    bmp.Dispose();
                }
                else
                {
                    numberOfCharactersSkipped++;
                }
            }
        }
 public List<Point> ScaledGetPoints(NOcrChar nOcrChar, int width, int height)
 {
     return GetPoints(this.GetScaledStart(nOcrChar, width, height), this.GetScaledEnd(nOcrChar, width, height));
 }
예제 #24
0
        public static void GenerateLineSegments(int numberOfLines, bool veryPrecise, NOcrChar nOcrChar, NikseBitmap nbmp)
        {
            int giveUpCount = 10000;
            var r = new Random();
            int count = 0;
            int hits = 0;
            bool tempVeryPrecise = veryPrecise;
            while (hits < numberOfLines && count < giveUpCount)
            {
                var start = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                var end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));

                if (hits < 5 && count < 100) // a few large lines
                {
                    for (int k = 0; k < 500; k++)
                    {
                        if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) > nOcrChar.Height / 2)
                        {
                            break;
                        }
                        else
                        {
                            end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                        }
                    }
                }
                else // and a lot of small lines
                {
                    for (int k = 0; k < 500; k++)
                    {
                        if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) < 5)
                        {
                            break;
                        }
                        else
                        {
                            end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                        }
                    }
                }

                var op = new NOcrPoint(start, end);
                bool ok = true;
                foreach (NOcrPoint existingOp in nOcrChar.LinesForeground)
                {
                    if (existingOp.Start.X == op.Start.X && existingOp.Start.Y == op.Start.Y &&
                        existingOp.End.X == op.End.X && existingOp.End.Y == op.End.Y)
                        ok = false;
                }
                if (ok && IsMatchPointForeGround(op, !tempVeryPrecise, nbmp, nOcrChar))
                {
                    nOcrChar.LinesForeground.Add(op);
                    //AddHistoryItem(nOcrChar);
                    hits++;
                }
                count++;
                if (count > giveUpCount - 100 && !tempVeryPrecise)
                    tempVeryPrecise = true;
            }

            count = 0;
            hits = 0;
            tempVeryPrecise = veryPrecise;
            while (hits < numberOfLines && count < giveUpCount)
            {
                var start = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                var end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));

                if (hits < 5 && count < 100) // a few large lines
                {
                    for (int k = 0; k < 500; k++)
                    {
                        if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) > nOcrChar.Height / 2)
                        {
                            break;
                        }
                        else
                        {
                            end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                        }
                    }
                }
                else // and a lot of small lines
                {
                    for (int k = 0; k < 500; k++)
                    {
                        if (Math.Abs(start.X - end.X) + Math.Abs(start.Y - end.Y) < 5)
                        {
                            break;
                        }
                        else
                        {
                            end = new Point(r.Next(nOcrChar.Width), r.Next(nOcrChar.Height));
                        }
                    }
                }

                var op = new NOcrPoint(start, end);
                bool ok = true;
                foreach (NOcrPoint existingOp in nOcrChar.LinesBackground)
                {
                    if (existingOp.Start.X == op.Start.X && existingOp.Start.Y == op.Start.Y &&
                        existingOp.End.X == op.End.X && existingOp.End.Y == op.End.Y)
                        ok = false;
                }
                if (ok && IsMatchPointBackGround(op, !tempVeryPrecise, nbmp, nOcrChar))
                {
                    nOcrChar.LinesBackground.Add(op);
                    //AddHistoryItem(nOcrChar);
                    hits++;
                }
                count++;

                if (count > giveUpCount - 100 && !tempVeryPrecise)
                    tempVeryPrecise = true;

            }

        }
 /// <summary>
 /// The redo.
 /// </summary>
 private void Redo()
 {
     if (this._history.Count > 0 && this._historyIndex < this._history.Count - 1)
     {
         this._historyIndex++;
         this._nocrChar = new NOcrChar(this._history[this._historyIndex]);
         this.ShowOcrPoints();
     }
 }
예제 #26
0
 /// <summary>
 /// The add.
 /// </summary>
 /// <param name="ocrChar">
 /// The ocr char.
 /// </param>
 public void Add(NOcrChar ocrChar)
 {
     if (ocrChar.ExpandCount > 0)
     {
         this.OcrCharactersExpanded.Insert(0, ocrChar);
     }
     else
     {
         this.OcrCharacters.Insert(0, ocrChar);
     }
 }
예제 #27
0
 public List <Point> ScaledGetPoints(NOcrChar nOcrChar, int width, int height)
 {
     return(GetPoints(GetScaledStart(nOcrChar, width, height), GetScaledEnd(nOcrChar, width, height)));
 }
        /// <summary>
        /// The undo.
        /// </summary>
        private void Undo()
        {
            this._drawLineOn = false;
            this._startDone = false;
            if (this._history.Count > 0 && this._historyIndex > 0)
            {
                this._historyIndex--;
                this._nocrChar = new NOcrChar(this._history[this._historyIndex]);
            }
            else if (this._historyIndex == 0)
            {
                var c = new NOcrChar(this._nocrChar);
                c.LinesForeground.Clear();
                c.LinesBackground.Clear();
                this._nocrChar = c;
                this._historyIndex--;
            }

            this.ShowOcrPoints();
        }
예제 #29
0
 private static NOcrChar MakeItalicNOcrChar(NOcrChar oldChar, int movePixelsLeft, double unItalicFactor)
 {
     var c = new NOcrChar();
     foreach (NOcrPoint op in oldChar.LinesForeground)
     {
         c.LinesForeground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor)));
     }
     foreach (NOcrPoint op in oldChar.LinesBackground)
     {
         c.LinesBackground.Add(new NOcrPoint(MakePointItalic(op.Start, oldChar.Height, movePixelsLeft, unItalicFactor), MakePointItalic(op.End, oldChar.Height, movePixelsLeft, unItalicFactor)));
     }
     c.Text = oldChar.Text;
     c.Width = oldChar.Width;
     c.Height = oldChar.Height;
     c.MarginTop = oldChar.MarginTop;
     c.Italic = true;
     return c;
 }
        private void listBoxInspectItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxInspectItems.SelectedIndex < 0)
                return;

            var img = _imageList[listBoxInspectItems.SelectedIndex];
            if (img.NikseBitmap != null)
            {
                pictureBoxInspectItem.Width = img.NikseBitmap.Width;
                pictureBoxInspectItem.Height = img.NikseBitmap.Height;
                var old = img.NikseBitmap.GetBitmap();
                pictureBoxInspectItem.Image = old;
                pictureBoxCharacter.Image = old;
                SizePictureBox();
            }
            else
            {
                pictureBoxInspectItem.Image = null;
                pictureBoxCharacter.Image = null;
            }

            var match = _matchList[listBoxInspectItems.SelectedIndex];
            if (match == null)
            { // 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;
                textBoxText.Enabled = false;
                checkBoxItalic.Enabled = false;
            }
            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;
            }
        }
예제 #31
0
 public CompareMatch(string text, bool italic, int expandCount, string name, NOcrChar character)
     : this(text, italic, expandCount, name)
 {
     NOcrCharacter = character;
 }
예제 #32
0
        private void listBoxFileNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelNOcrCharInfo.Text = string.Empty;
            if (listBoxFileNames.SelectedIndex < 0)
                return;

            _nocrChar = listBoxFileNames.Items[listBoxFileNames.SelectedIndex] as NOcrChar;
            if (_nocrChar == null)
            {
                pictureBoxCharacter.Invalidate();
                groupBoxCurrentCompareImage.Enabled = false;
                listBoxLinesForeground.Items.Clear();
                listBoxlinesBackground.Items.Clear();
            }
            else
            {
                textBoxText.Text = _nocrChar.Text;
                checkBoxItalic.Checked = _nocrChar.Italic;
                pictureBoxCharacter.Invalidate();
                groupBoxCurrentCompareImage.Enabled = true;
                labelNOcrCharInfo.Text = string.Format("Size: {0}x{1}, margin top: {2} ", _nocrChar.Width, _nocrChar.Height, _nocrChar.MarginTop);

                if (pictureBoxCharacter.Image != null)
                {
                    if (IsMatch())
                    {
                        groupBoxCurrentCompareImage.BackColor = Color.LightGreen;
                    }
                    else
                    {
                        groupBoxCurrentCompareImage.BackColor = Control.DefaultBackColor;
                    }
                }
                _drawLineOn = false;
                _history = new List<NOcrChar>();
                _historyIndex = -1;

                if (_bitmap == null)
                {
                    var bitmap = new Bitmap(_nocrChar.Width, _nocrChar.Height);
                    var nbmp = new NikseBitmap(bitmap);
                    nbmp.Fill(Color.White);
                    pictureBoxCharacter.Image = nbmp.GetBitmap(); 
                    SizePictureBox();
                    ShowOcrPoints();
                    bitmap.Dispose();
                }
            }
        }