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;
        }
Ejemplo n.º 2
0
        internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List <VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm)
        {
            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;
            _vobSubForm            = vobSubForm;
            _additions             = additions;
            _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();
        }
        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();
        }
Ejemplo n.º 4
0
        internal void Initialize(Bitmap bitmap, int pixelsIsSpace, bool rightToLeft, NOcrDb nOcrDb, VobSubOcr vobSubOcr, double unItalicFactor)
        {
            _bitmap = bitmap;
            var nbmp = new NikseBitmap(bitmap);

            nbmp.ReplaceNonWhiteWithTransparent();
            bitmap          = nbmp.GetBitmap();
            _bitmap2        = bitmap;
            _nocrChars      = nOcrDb.OcrCharacters;
            _matchList      = new List <VobSubOcr.CompareMatch>();
            _vobSubOcr      = vobSubOcr;
            _unItalicFactor = unItalicFactor;

            int minLineHeight = 6;

            _imageList = NikseBitmapImageSplitter.SplitBitmapToLettersNew(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom, minLineHeight);
            // _imageList = NikseBitmapImageSplitter.SplitBitmapToLetters(nbmp, pixelsIsSpace, rightToLeft, Configuration.Settings.VobSubOcr.TopToBottom);

            int index = 0;

            while (index < _imageList.Count)
            {
                ImageSplitterItem item = _imageList[index];
                if (item.NikseBitmap == null)
                {
                    listBoxInspectItems.Items.Add(item.SpecialCharacter);
                    _matchList.Add(null);
                }
                else
                {
                    nbmp = item.NikseBitmap;
                    nbmp.ReplaceNonWhiteWithTransparent();
                    item.Y += nbmp.CropTopTransparent(0);
                    nbmp.CropTransparentSidesAndBottom(0, true);
                    nbmp.ReplaceTransparentWith(Color.Black);

                    //get nocr matches
                    Nikse.SubtitleEdit.Forms.VobSubOcr.CompareMatch match = vobSubOcr.GetNOcrCompareMatchNew(item, nbmp, nOcrDb, _unItalicFactor, false, false);
                    if (match == null)
                    {
                        listBoxInspectItems.Items.Add("?");
                        _matchList.Add(null);
                    }
                    else
                    {
                        listBoxInspectItems.Items.Add(match.Text);
                        _matchList.Add(match);
                    }
                }
                index++;
            }
        }
Ejemplo n.º 5
0
        private CompareMatch GetCompareMatchNew(ImageSplitterItem targetItem, out CompareMatch secondBestGuess, List<ImageSplitterItem> list, int listIndex)
        {
            double maxDiff = _numericUpDownMaxErrorPct;
            secondBestGuess = null;
            int index = 0;
            int smallestDifference = 10000;
            int smallestIndex = -1;
            var target = targetItem.NikseBitmap;
            if (_binaryOcrDb == null)
            {
                return null;
            }

            var bob = new BinaryOcrBitmap(target) { X = targetItem.X, Y = targetItem.Top };

            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) || 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)
                        {
                            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)
                    {
                        return new CompareMatch(b.Text, b.Italic, b.ExpandCount, b.Key);
                    }
                }
            }

            FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, target, _binaryOcrDb, bob, maxDiff);
            if (maxDiff > 0)
            {
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(0, 1, target.Width, target.Height - 1));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(1, 0, target.Width - 1, target.Height));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
                if (target.Width > 16 && target.Height > 16 && (smallestIndex == -1 || smallestDifference * 100.0 / (target.Width * target.Height) > maxDiff))
                {
                    var t2 = target.CopyRectangle(new Rectangle(0, 0, target.Width - 1, target.Height));
                    FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, t2, _binaryOcrDb, bob, maxDiff);
                }
            }

            if (smallestIndex >= 0)
            {
                double differencePercentage = smallestDifference * 100.0 / (target.Width * target.Height);
                if (differencePercentage <= maxDiff)
                {
                    var hit = _binaryOcrDb.CompareImages[smallestIndex];

                    string text = hit.Text;
                    if (smallestDifference > 0)
                    {
                        int h = hit.Height;
                        if (text == "V" || text == "W" || text == "U" || text == "S" || text == "Z" || text == "O" || text == "X" || text == "Ø" || text == "C")
                        {
                            if (_binOcrLastLowercaseHeight > 3 && h - _binOcrLastLowercaseHeight < 2)
                                text = text.ToLower();
                        }
                        else if (text == "v" || text == "w" || text == "u" || text == "s" || text == "z" || text == "o" || text == "x" || text == "ø" || text == "c")
                        {
                            if (_binOcrLastUppercaseHeight > 3 && _binOcrLastUppercaseHeight - h < 2)
                                text = text.ToUpper();
                        }
                    }
                    else
                    {
                        SetBinOcrLowercaseUppercase(hit.Height, text);
                    }
                    if (differencePercentage > 0)
                    {
                        bool dummy;
                        if ((hit.Text == "l" || hit.Text == "!") && bob.IsLowercaseI(out dummy))
                        {
                            hit = null;
                        }
                        else if ((hit.Text == "i" || hit.Text == "!") && bob.IsLowercaseL())
                        {
                            hit = null;
                        }
                        else if ((hit.Text == "o" || hit.Text == "O") && bob.IsC())
                        {
                            return new CompareMatch(hit.Text == "o" ? "c" : "C", false, 0, null);
                        }
                        else if ((hit.Text == "c" || hit.Text == "C") && !bob.IsC() && bob.IsO())
                        {
                            return new CompareMatch(hit.Text == "c" ? "o" : "O", false, 0, null);
                        }
                    }

                    if (hit != null)
                    {
                        if (_binOcrLastLowercaseHeight < 0 && differencePercentage < 9 && (text == "e" || text == "d" || text == "a"))
                            _binOcrLastLowercaseHeight = bob.Height;
                        return new CompareMatch(text, hit.Italic, hit.ExpandCount, hit.Key);
                    }
                }

                var guess = _binaryOcrDb.CompareImages[smallestIndex];
                secondBestGuess = new CompareMatch(guess.Text, guess.Italic, guess.ExpandCount, guess.Key);
            }

            if (bob.IsPeriod())
            {
                return new CompareMatch(".", false, 0, null);
            }
            if (bob.IsComma())
            {
                return new CompareMatch(",", false, 0, null);
            }
            if (maxDiff > 0 && bob.IsApostrophe())
            {
                return new CompareMatch("'", false, 0, null);
            }
            if (bob.IsLowercaseJ()) // "j" detection must be before "i"
            {
                return new CompareMatch("j", false, 0, null);
            }
            bool italicLowercaseI;
            if (bob.IsLowercaseI(out italicLowercaseI))
            {
                return new CompareMatch("i", italicLowercaseI, 0, null);
            }
            if (bob.IsColon())
            {
                return new CompareMatch(":", false, 0, null);
            }
            if (bob.IsExclamationMark())
            {
                return new CompareMatch("!", false, 0, null);
            }
            if (bob.IsDash())
            {
                return new CompareMatch("-", false, 0, null);
            }

            return null;
        }
Ejemplo n.º 6
0
        private string SaveCompareItemNew(ImageSplitterItem newTarget, string text, bool isItalic, List<ImageSplitterItem> expandList)
        {
            int expandCount = 0;
            if (expandList != null)
                expandCount = expandList.Count;

            if (expandCount > 0)
            {
                var bob = new BinaryOcrBitmap(expandList[0].NikseBitmap, isItalic, expandCount, text, expandList[0].X, expandList[0].Top);
                bob.ExpandedList = new List<BinaryOcrBitmap>();
                for (int j = 1; j < expandList.Count; j++)
                {
                    var expandedBob = new BinaryOcrBitmap(expandList[j].NikseBitmap);
                    expandedBob.X = expandList[j].X;
                    expandedBob.Y = expandList[j].Top;
                    bob.ExpandedList.Add(expandedBob);
                }
                _binaryOcrDb.Add(bob);
                _binaryOcrDb.Save();
                return bob.Key;
            }
            else
            {
                var bob = new BinaryOcrBitmap(newTarget.NikseBitmap, isItalic, expandCount, text, newTarget.X, newTarget.Top);
                _binaryOcrDb.Add(bob);
                _binaryOcrDb.Save();
                return bob.Key;
            }
        }
Ejemplo n.º 7
0
        private static NOcrChar NOcrFindExpandedMatch(NikseBitmap nbmp, ImageSplitterItem targetItem, List<NOcrChar> nOcrChars)
        {
            int w = targetItem.NikseBitmap.Width;
            foreach (NOcrChar oc in nOcrChars)
            {
                if (oc.ExpandCount > 1 && oc.Width > w && targetItem.X + oc.Width < nbmp.Width)
                {
                    bool ok = true;
                    var index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.GetPoints())
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.GetPoints())
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;

                    ok = true;
                    index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            Point p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nbmp.Width && p.Y < nbmp.Height)
                            {
                                Color c = nbmp.GetPixel(p.X, p.Y);
                                if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;
                }
            }
            return null;
        }
Ejemplo n.º 8
0
        private CompareMatch GetCompareMatch(ImageSplitterItem targetItem, NikseBitmap parentBitmap, out CompareMatch secondBestGuess, List<ImageSplitterItem> list, int listIndex)
        {
            secondBestGuess = null;
            int index = 0;
            int smallestDifference = 10000;
            int smallestIndex = -1;
            NikseBitmap target = targetItem.NikseBitmap;
            if (_compareBitmaps == null)
            {
                return null;
            }

            foreach (CompareItem compareItem in _compareBitmaps)
            {
                // check for expand match!
                if (compareItem.ExpandCount > 0 && compareItem.Bitmap.Width > target.Width &&
                    parentBitmap.Width >= compareItem.Bitmap.Width + targetItem.X) // &&   parentBitmap.Height >= compareItem.Bitmap.Height + targetItem.Y) //NIXE-debug-test- what not correct?
                {
                    int minY = targetItem.Y;
                    for (int j = 1; j < compareItem.ExpandCount; j++)
                    {
                        if (list != null && list.Count > listIndex + j && list[listIndex + j].Y < minY)
                            minY = list[listIndex + j].Y;
                    }
                    if (parentBitmap.Height >= compareItem.Bitmap.Height + minY)
                    {
                        var cutBitmap = parentBitmap.CopyRectangle(new Rectangle(targetItem.X, minY, compareItem.Bitmap.Width, compareItem.Bitmap.Height));
                        int dif = NikseBitmapImageSplitter.IsBitmapsAlike(compareItem.Bitmap, cutBitmap);
                        if (dif < smallestDifference && (Math.Abs(target.Height - compareItem.Bitmap.Height) <= 5 || compareItem.Text != "\""))
                        {
                            smallestDifference = dif;
                            smallestIndex = index;
                            if (dif == 0)
                                break; // foreach ending
                        }
                    }
                }
                index++;
            }

            // Search images with minor location changes
            FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, target, _compareBitmaps);

            if (smallestDifference * 100.0 / (target.Width * target.Height) > _vobSubOcrSettings.AllowDifferenceInPercent && target.Width < 70)
            {
                if (smallestDifference > 2 && target.Width > 25)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(4, 0, target.Width - 4, target.Height));
                    FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap, _compareBitmaps);
                }

                if (smallestDifference > 2 && target.Width > 12)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(1, 0, target.Width - 2, target.Height));
                    FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap, _compareBitmaps);
                }

                if (smallestDifference > 2 && target.Width > 12)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(0, 0, target.Width - 2, target.Height));
                    FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap, _compareBitmaps);
                }

                if (smallestDifference > 2 && target.Width > 12)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(1, 0, target.Width - 2, target.Height));
                    int topCrop = 0;
                    var cutBitmap2 = NikseBitmapImageSplitter.CropTopAndBottom(cutBitmap, out topCrop, 2);
                    if (cutBitmap2.Height != target.Height)
                        FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap2, _compareBitmaps);
                }

                if (smallestDifference > 2 && target.Width > 15)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(1, 0, target.Width - 2, target.Height));
                    int topCrop = 0;
                    var cutBitmap2 = NikseBitmapImageSplitter.CropTopAndBottom(cutBitmap, out topCrop);
                    if (cutBitmap2.Height != target.Height)
                        FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap2, _compareBitmaps);
                }

                if (smallestDifference > 2 && target.Width > 15)
                {
                    var cutBitmap = target.CopyRectangle(new Rectangle(1, 0, target.Width - 2, target.Height));
                    int topCrop;
                    var cutBitmap2 = NikseBitmapImageSplitter.CropTopAndBottom(cutBitmap, out topCrop);
                    if (cutBitmap2.Height != target.Height)
                        FindBestMatch(ref index, ref smallestDifference, ref smallestIndex, cutBitmap2, _compareBitmaps);
                }
            }

            if (smallestIndex >= 0)
            {
                double differencePercentage = smallestDifference * 100.0 / (target.Width * target.Height);
                double maxDiff = (double)numericUpDownMaxErrorPct.Value;
                if (differencePercentage <= maxDiff)
                {
                    var hit = _compareBitmaps[smallestIndex];
                    return new CompareMatch(hit.Text, hit.Italic, hit.ExpandCount, hit.Name);
                }

                var guess = _compareBitmaps[smallestIndex];
                secondBestGuess = new CompareMatch(guess.Text, guess.Italic, guess.ExpandCount, guess.Name);
            }

            return null;
        }
Ejemplo n.º 9
0
        internal CompareMatch GetNOcrCompareMatchNew(ImageSplitterItem targetItem, NikseBitmap parentBitmap, NOcrDb nOcrDb, bool tryItalicScaling, bool deepSeek)
        {
            var expandedResult = NOcrFindExpandedMatch(parentBitmap, targetItem, nOcrDb.OcrCharactersExpanded);
            if (expandedResult != null)
            {
                return new CompareMatch(expandedResult.Text, expandedResult.Italic, expandedResult.ExpandCount, null, expandedResult);
            }

            bool italic;
            var result = NOcrFindBestMatchNew(targetItem, targetItem.Y - targetItem.ParentY, out italic, nOcrDb, tryItalicScaling, deepSeek);
            if (result == null)
            {
                if (checkBoxNOcrCorrect.Checked)
                    return null;
                return new CompareMatch("*", false, 0, null);
            }

            // Fix uppercase/lowercase issues (not I/l)
            if (result.Text == "e")
                _nocrLastLowercaseHeight = targetItem.NikseBitmap.Height;
            else if (_nocrLastLowercaseHeight == -1 && result.Text == "a")
                _nocrLastLowercaseHeight = targetItem.NikseBitmap.Height;

            if (result.Text == "E" || result.Text == "H" || result.Text == "R" || result.Text == "D" || result.Text == "T")
                _nocrLastUppercaseHeight = targetItem.NikseBitmap.Height;
            else if (_nocrLastUppercaseHeight == -1 && result.Text == "M")
                _nocrLastUppercaseHeight = targetItem.NikseBitmap.Height;

            if (result.Text == "V" || result.Text == "W" || result.Text == "U" || result.Text == "S" || result.Text == "Z" || result.Text == "O" || result.Text == "X" || result.Text == "Ø" || result.Text == "C")
            {
                if (_nocrLastLowercaseHeight > 3 && targetItem.NikseBitmap.Height - _nocrLastLowercaseHeight < 2)
                    result.Text = result.Text.ToLower();
            }
            else if (result.Text == "v" || result.Text == "w" || result.Text == "u" || result.Text == "s" || result.Text == "z" || result.Text == "o" || result.Text == "x" || result.Text == "ø" || result.Text == "c")
            {
                if (_nocrLastUppercaseHeight > 3 && _nocrLastUppercaseHeight - targetItem.NikseBitmap.Height < 2)
                    result.Text = result.Text.ToUpper();
            }

            if (italic)
                return new CompareMatch(result.Text, true, 0, null, result);
            else
                return new CompareMatch(result.Text, result.Italic, 0, null, result);
        }
Ejemplo n.º 10
0
        private static NOcrChar NOcrFindBestMatchNew(ImageSplitterItem targetItem, int topMargin, out bool italic, NOcrDb nOcrDb, bool tryItalicScaling, bool deepSeek)
        {
            italic = false;
            if (nOcrDb == null)
            {
                return null;
            }

            var nbmp = targetItem.NikseBitmap;
            int index;
            foreach (NOcrChar oc in nOcrDb.OcrCharacters)
            {
                if (Math.Abs(oc.Width - nbmp.Width) < 3 && Math.Abs(oc.Height - nbmp.Height) < 3 && Math.Abs(oc.MarginTop - topMargin) < 3)
                { // only very accurate matches

                    bool ok = true;
                    index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    Point p = new Point(point.X - 1, point.Y);
                                    if (p.X < 0)
                                        p.X = 1;
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (nbmp.Width > 20 && c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                {
                                    Point p = new Point(point.X, point.Y);
                                    if (oc.Width > 19 && point.X > 0)
                                        p.X = p.X - 1;
                                    c = nbmp.GetPixel(p.X, p.Y);
                                    if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;
                }
            }

            foreach (NOcrChar oc in nOcrDb.OcrCharacters)
            {
                int marginTopDiff = Math.Abs(oc.MarginTop - topMargin);
                if (Math.Abs(oc.Width - nbmp.Width) < 4 && Math.Abs(oc.Height - nbmp.Height) < 4 && marginTopDiff > 4 && marginTopDiff < 9)
                { // only very accurate matches - but not for margin top

                    bool ok = true;
                    index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesForeground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                {
                                }
                                else
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        NOcrPoint op = oc.LinesBackground[index];
                        foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                        }
                        index++;
                    }
                    if (ok)
                        return oc;
                }
            }

            // try some resize if aspect ratio is about the same
            double widthPercent = nbmp.Height * 100.0 / nbmp.Width;
            foreach (NOcrChar oc in nOcrDb.OcrCharacters)
            {
                if (!oc.IsSensitive)
                {
                    if (Math.Abs(oc.WidthPercent - widthPercent) < 15 && oc.Width > 12 && oc.Height > 19 && nbmp.Width > 19 && nbmp.Height > 12 && Math.Abs(oc.MarginTop - topMargin) < nbmp.Height / 4)
                    {
                        bool ok = true;
                        index = 0;
                        while (index < oc.LinesForeground.Count && ok)
                        {
                            NOcrPoint op = oc.LinesForeground[index];
                            foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                            index++;
                        }
                        index = 0;
                        while (index < oc.LinesBackground.Count && ok)
                        {
                            NOcrPoint op = oc.LinesBackground[index];
                            foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                            index++;
                        }
                        if (ok)
                            return oc;
                    }
                }
            }

            if (deepSeek) // if we do now draw then just try anything...
            {
                widthPercent = nbmp.Height * 100.0 / nbmp.Width;

                foreach (NOcrChar oc in nOcrDb.OcrCharacters)
                {
                    if (!oc.IsSensitive)
                    {
                        if (Math.Abs(oc.WidthPercent - widthPercent) < 40 && nbmp.Height > 11) // && oc.Height > 12 && oc.Width > 16 && nbmp.Width > 16 && nbmp.Height > 12 && Math.Abs(oc.MarginTop - topMargin) < 15)
                        {
                            bool ok = true;
                            foreach (NOcrPoint op in oc.LinesForeground)
                            {
                                foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                        {
                                        }
                                        else
                                        {
                                            ok = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            foreach (NOcrPoint op in oc.LinesBackground)
                            {
                                foreach (Point point in op.ScaledGetPoints(oc, 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 + c.G + c.B > NocrMinColor)
                                        {
                                            ok = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (ok)
                                return oc;
                        }
                    }
                }

                foreach (NOcrChar oc in nOcrDb.OcrCharacters)
                {
                    if (Math.Abs(oc.WidthPercent - widthPercent) < 40 && oc.Height > 12 && oc.Width > 19 && nbmp.Width > 19 && nbmp.Height > 12 && Math.Abs(oc.MarginTop - topMargin) < 15)
                    {
                        bool ok = true;
                        foreach (NOcrPoint op in oc.LinesForeground)
                        {
                            foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width - 3, 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 + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        foreach (NOcrPoint op in oc.LinesBackground)
                        {
                            foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width - 3, 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 + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        if (ok)
                            return oc;
                    }
                }

                foreach (NOcrChar oc in nOcrDb.OcrCharacters)
                {
                    if (Math.Abs(oc.WidthPercent - widthPercent) < 40 && oc.Height > 12 && oc.Width > 19 && nbmp.Width > 19 && nbmp.Height > 12 && Math.Abs(oc.MarginTop - topMargin) < 15)
                    {
                        bool ok = true;
                        foreach (NOcrPoint op in oc.LinesForeground)
                        {
                            foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height - 4))
                            {
                                if (point.X >= 0 && point.Y + 4 >= 0 && point.X < nbmp.Width && point.Y + 4 < nbmp.Height)
                                {
                                    Color c = nbmp.GetPixel(point.X, point.Y + 4);
                                    if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                    }
                                    else
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        foreach (NOcrPoint op in oc.LinesBackground)
                        {
                            foreach (Point point in op.ScaledGetPoints(oc, nbmp.Width, nbmp.Height - 4))
                            {
                                if (point.X >= 0 && point.Y + 4 >= 0 && point.X < nbmp.Width && point.Y + 4 < nbmp.Height)
                                {
                                    Color c = nbmp.GetPixel(point.X, point.Y + 4);
                                    if (c.A > 150 && c.R + c.G + c.B > NocrMinColor)
                                    {
                                        ok = false;
                                        break;
                                    }
                                }
                            }
                        }
                        if (ok)
                            return oc;
                    }
                }
            }

            if (tryItalicScaling)
            {
                //  int left = targetItem.X;
                //  int width = targetItem.Bitmap.Width;
                //  //if (left > 3)
                //  //{
                //  //    left -= 3;
                //  //    width += 3;
                //  //}
                //  var temp = ImageSplitter.Copy(parentBitmap, new Rectangle(left, targetItem.Y, width , targetItem.Bitmap.Height));
                //  var bitmap2 = UnItalic(temp, unItalicFactor);
                //  //var nbmpUnItalic = new NikseBitmap(unItalicedBmp);
                //  //nbmpUnItalic.ReplaceNonWhiteWithTransparent();
                //  //Bitmap bitmap2 = nbmpUnItalic.GetBitmap();
                ////  bitmap2.Save(@"D:\Download\__" + Guid.NewGuid().ToString() + ".bmp");
                //  var list = ImageSplitter.SplitBitmapToLetters(bitmap2, 10, false, false);
                //  var matches = new List<NOcrChar>();
                //  bool unitalicOk = true;
                //  foreach (var spi in list)
                //  {
                //      var m = NOcrFindBestMatch(spi, topMargin, out italic, nOcrChars, unItalicFactor, false, true);
                //      if (m == null)
                //      {
                //          if (spi.Bitmap.Width > 2)
                //          {
                //              unitalicOk = false;
                //              break;
                //          }
                //      }
                //      else
                //      {
                //          matches.Add(m);
                //      }
                //  }

                //  if (unitalicOk && matches.Count > 0)
                //  {
                //      italic = true;
                //      if (matches.Count == 1)
                //      {
                //          return matches[0];
                //      }
                //      else if (matches.Count > 1)
                //      {
                //          NOcrChar c = new NOcrChar(matches[0]);
                //          c.LinesBackground.Clear();
                //          c.LinesForeground.Clear();
                //          c.Text = string.Empty;
                //          foreach (var m in matches)
                //              c.Text += m.Text;
                //          return c;
                //      }
                //  }
            }

            return null;
        }
Ejemplo n.º 11
0
        internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showExpand, bool showShrink, string text)
        {
            listBoxLinesForeground.Items.Clear();
            listBoxlinesBackground.Items.Clear();
            var nbmp = new NikseBitmap(vobSubImage);

            vobSubImage = nbmp.GetBitmap();

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

            textBoxCharacters.Text = text;
            NOcrChar = new NOcrChar {
                MarginTop = character.Top
            };
            _imageWidth  = character.NikseBitmap.Width;
            _imageHeight = character.NikseBitmap.Height;
            _drawLineOn  = false;
            _warningNoNotForegroundLinesShown = false;

            buttonExpandSelection.Visible = showExpand;
            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;
            numericUpDownLinesToDraw.Value = Configuration.Settings.VobSubOcr.LineOcrLinesToAutoGuess;
            GenerateLineSegments((int)numericUpDownLinesToDraw.Value, false, NOcrChar, new NikseBitmap(pictureBoxCharacter.Image as Bitmap));
            ShowOcrPoints();
            pictureBoxCharacter.Invalidate();
            numericUpDownLinesToDraw.ValueChanged += (sender, args) =>
            {
                Configuration.Settings.VobSubOcr.LineOcrLinesToAutoGuess = (int)numericUpDownLinesToDraw.Value;
                buttonGuessAgain_Click(null, null);
            };

            textBoxCharacters.Focus();
        }
Ejemplo n.º 12
0
        internal static CompareMatch GetNOcrCompareMatch(ImageSplitterItem targetItem, NikseBitmap parentBitmap, NOcrThreadParameter p)
        {
            bool italic;
            var expandedResult = NOcrFindExpandedMatch(parentBitmap, targetItem, p.NOcrChars);
            if (expandedResult != null)
                return new CompareMatch(expandedResult.Text, expandedResult.Italic, expandedResult.ExpandCount, null, expandedResult);

            var result = NOcrFindBestMatch(targetItem, targetItem.Y - targetItem.ParentY, out italic, p.NOcrChars, p.UnItalicFactor, p.AdvancedItalicDetection, true);
            if (result == null)
                return null;

            // Fix uppercase/lowercase issues (not I/l)
            if (result.Text == "e")
                p.NOcrLastLowercaseHeight = targetItem.NikseBitmap.Height;
            else if (p.NOcrLastLowercaseHeight == -1 && result.Text == "a")
                p.NOcrLastLowercaseHeight = targetItem.NikseBitmap.Height;

            if (result.Text == "E" || result.Text == "H" || result.Text == "R" || result.Text == "D" || result.Text == "T")
                p.NOcrLastUppercaseHeight = targetItem.NikseBitmap.Height;
            else if (p.NOcrLastUppercaseHeight == -1 && result.Text == "M")
                p.NOcrLastUppercaseHeight = targetItem.NikseBitmap.Height;

            if (result.Text == "V" || result.Text == "W" || result.Text == "U" || result.Text == "S" || result.Text == "Z" || result.Text == "O" || result.Text == "X" || result.Text == "Ø" || result.Text == "C")
            {
                if (p.NOcrLastLowercaseHeight > 3 && targetItem.NikseBitmap.Height - p.NOcrLastLowercaseHeight < 2)
                    result.Text = result.Text.ToLower();
            }
            else if (result.Text == "v" || result.Text == "w" || result.Text == "u" || result.Text == "s" || result.Text == "z" || result.Text == "o" || result.Text == "x" || result.Text == "ø" || result.Text == "c")
            {
                if (p.NOcrLastUppercaseHeight > 3 && p.NOcrLastUppercaseHeight - targetItem.NikseBitmap.Height < 2)
                    result.Text = result.Text.ToUpper();
            }

            if (italic)
                return new CompareMatch(result.Text, true, 0, null, result);
            return new CompareMatch(result.Text, result.Italic, 0, null, result);
        }
Ejemplo n.º 13
0
 public CompareMatch(string text, bool italic, int expandCount, string name, ImageSplitterItem imageSplitterItem)
     : this(text, italic, expandCount, name)
 {
     ImageSplitterItem = imageSplitterItem;
 }
Ejemplo n.º 14
0
        private CompareMatch GetCompareMatchNew(ImageSplitterItem targetItem, out CompareMatch secondBestGuess, List<ImageSplitterItem> list, int listIndex)
        {
            double maxDiff = _numericUpDownMaxErrorPct;
            secondBestGuess = null;
            int index = 0;
            int smallestDifference = 10000;
            int smallestIndex = -1;
            NikseBitmap target = targetItem.NikseBitmap;
            if (_binaryOcrDb == null)
            {
                return null;
            }

            var bob = new BinaryOcrBitmap(target);

            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)
                {
                    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)
                        {
                            ok = true;
                        }
                        else
                        {
                            ok = false;
                            break;
                        }
                    }
                    if (ok)
                    {
                        secondBestGuess = null;
                        return new CompareMatch(b.Text, b.Italic, b.ExpandCount, b.Key);
                    }
                }
            }

            FindBestMatchNew(ref index, ref smallestDifference, ref smallestIndex, target, _binaryOcrDb, bob, maxDiff);

            if (smallestIndex >= 0)
            {
                double differencePercentage = smallestDifference * 100.0 / (target.Width * target.Height);
                if (differencePercentage <= maxDiff)
                {
                    var hit = _binaryOcrDb.CompareImages[smallestIndex];

                    string text = hit.Text;
                    if (smallestDifference > 0)
                    {
                        int h = hit.Height;
                        if (text == "V" || text == "W" || text == "U" || text == "S" || text == "Z" || text == "O" || text == "X" || text == "Ø" || text == "C")
                        {
                            if (_binOcrLastLowercaseHeight > 3 && h - _binOcrLastLowercaseHeight < 2)
                                text = text.ToLower();
                        }
                        else if (text == "v" || text == "w" || text == "u" || text == "s" || text == "z" || text == "o" || text == "x" || text == "ø" || text == "c")
                        {
                            if (_binOcrLastUppercaseHeight > 3 && _binOcrLastUppercaseHeight - h < 2)
                                text = text.ToUpper();
                        }
                    }
                    else
                    {
                        SetBinOcrLowercaseUppercase(hit.Height, text);
                    }

                    return new CompareMatch(text, hit.Italic, hit.ExpandCount, hit.Key);
                }

                var guess = _binaryOcrDb.CompareImages[smallestIndex];
                secondBestGuess = new CompareMatch(guess.Text, guess.Italic, guess.ExpandCount, guess.Key);
            }

            return null;
        }
Ejemplo n.º 15
0
        public NOcrChar GetMatchExpanded(NikseBitmap nikseBitmap, ImageSplitterItem targetItem, int listIndex, List <ImageSplitterItem> list)
        {
            int w = targetItem.NikseBitmap.Width;

            for (var i = 0; i < OcrCharactersExpanded.Count; i++)
            {
                var oc = OcrCharactersExpanded[i];
                if (oc.ExpandCount > 1 && oc.Width > w && targetItem.X + oc.Width < nikseBitmap.Width)
                {
                    bool ok    = true;
                    var  index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        var op = oc.LinesForeground[index];
                        foreach (var point in op.GetPoints())
                        {
                            var p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nikseBitmap.Width && p.Y < nikseBitmap.Height)
                            {
                                var a = nikseBitmap.GetAlpha(p.X, p.Y);
                                if (a <= 150)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                            else if (p.X >= 0 && p.Y >= 0)
                            {
                                ok = false;
                                break;
                            }
                        }

                        index++;
                    }

                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        var op = oc.LinesBackground[index];
                        foreach (var point in op.GetPoints())
                        {
                            var p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nikseBitmap.Width && p.Y < nikseBitmap.Height)
                            {
                                var a = nikseBitmap.GetAlpha(p.X, p.Y);
                                if (a > 150)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                            else if (p.X >= 0 && p.Y >= 0)
                            {
                                ok = false;
                                break;
                            }
                        }

                        index++;
                    }

                    if (ok)
                    {
                        var size = GetTotalSize(listIndex, list, oc.ExpandCount);
                        if (Math.Abs(size.X - oc.Width) < 3 && Math.Abs(size.Y - oc.Height) < 3)
                        {
                            return(oc);
                        }
                    }
                }
            }

            for (var i = 0; i < OcrCharactersExpanded.Count; i++)
            {
                var oc = OcrCharactersExpanded[i];
                if (oc.ExpandCount > 1 && oc.Width > w && targetItem.X + oc.Width < nikseBitmap.Width)
                {
                    bool ok    = true;
                    var  index = 0;
                    while (index < oc.LinesForeground.Count && ok)
                    {
                        var op = oc.LinesForeground[index];
                        foreach (var point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            var p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nikseBitmap.Width && p.Y < nikseBitmap.Height)
                            {
                                var a = nikseBitmap.GetAlpha(p.X, p.Y);
                                if (a <= 150)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                            else if (p.X >= 0 && p.Y >= 0)
                            {
                                ok = false;
                                break;
                            }
                        }

                        index++;
                    }

                    index = 0;
                    while (index < oc.LinesBackground.Count && ok)
                    {
                        var op = oc.LinesBackground[index];
                        foreach (var point in op.ScaledGetPoints(oc, oc.Width, oc.Height - 1))
                        {
                            var p = new Point(point.X + targetItem.X, point.Y + targetItem.Y);
                            if (p.X >= 0 && p.Y >= 0 && p.X < nikseBitmap.Width && p.Y < nikseBitmap.Height)
                            {
                                var a = nikseBitmap.GetAlpha(p.X, p.Y);
                                if (a > 150)
                                {
                                    ok = false;
                                    break;
                                }
                            }
                            else if (p.X >= 0 && p.Y >= 0)
                            {
                                ok = false;
                                break;
                            }
                        }

                        index++;
                    }

                    if (ok)
                    {
                        var size         = GetTotalSize(listIndex, list, oc.ExpandCount);
                        var widthPercent = size.Y * 100.0 / size.X;
                        if (Math.Abs(widthPercent - oc.WidthPercent) < 15 &&
                            Math.Abs(size.X - oc.Width) < 25 && Math.Abs(size.Y - oc.Height) < 20)
                        {
                            return(oc);
                        }
                    }
                }
            }

            return(null);
        }
        private static List<ImageSplitterItem> SplitHorizontal(ImageSplitterItem verticalItem, int xOrMorePixelsMakesSpace)
        { // split line into letters
            NikseBitmap bmp = verticalItem.NikseBitmap;
            var parts = new List<ImageSplitterItem>();
            int size = 0;
            int startX = 0;
            int lastEndX = 0;
            int y = 0;
            bool spaceJustAdded = false;

            for (int x = 0; x < bmp.Width - 1; x++)
            {
                bool allTransparent = IsVerticalLineTransparent(bmp, ref y, x);

                // check if line is transparent and cursive
                bool cursiveOk = false;
                int tempY = 0;
                if (allTransparent == false &&
                    size > 5 &&
                    y > 3 &&
                    x < bmp.Width - 2 &&
                    !IsVerticalLineTransparent(bmp, ref tempY, x + 1))
                {
                    //Add space?
                    if (lastEndX > 0 && lastEndX + xOrMorePixelsMakesSpace < startX)
                    {
                        int cleanCount = 0;
                        for (int j = lastEndX; j < startX; j++)
                        {
                            int y1 = j;
                            if (IsVerticalLineTransparentAlphaOnly(bmp, ref y1, j))
                                cleanCount++;
                        }
                        if (cleanCount > 0 && !spaceJustAdded)
                        {
                            parts.Add(new ImageSplitterItem(" "));
                            spaceJustAdded = true;
                        }
                    }

                    var cursivePoints = new List<Point>();

                    cursiveOk = IsCursiveVerticalLineTransparent(bmp, size, y, x, cursivePoints);

                    if (cursiveOk)
                    {
                        // make letter image
                        int end = x + 1 - startX;
                        if (startX > 0)
                        {
                            startX--;
                            end++;
                        }
                        NikseBitmap b1 = bmp.CopyRectangle(new Rectangle(startX, 0, end, bmp.Height));
                        //                         b1.Save(@"d:\temp\cursive.bmp"); // just for debugging

                        // make non-black/transparent stuff from other letter transparent
                        foreach (Point p in cursivePoints)
                        {
                            for (int fixY = p.Y; fixY < bmp.Height; fixY++)
                                b1.SetPixel(p.X - startX, fixY, Color.Transparent);
                        }

                        RemoveBlackBarRight(b1);
                        //                                                b1.Save(@"d:\temp\cursive-cleaned.bmp"); // just for debugging

                        // crop and save image
                        int addY;
                        b1 = CropTopAndBottom(b1, out addY);
                        parts.Add(new ImageSplitterItem(startX, verticalItem.Y + addY, b1));
                        spaceJustAdded = false;
                        size = 0;
                        startX = x + 1;
                        lastEndX = x;
                    }
                }

                if (!cursiveOk)
                {
                    if (allTransparent)
                    {
                        if (size > 0)
                        {
                            if (size > 1)
                            {
                                //Add space?
                                if (lastEndX > 0 && lastEndX + xOrMorePixelsMakesSpace < startX)
                                {
                                    int cleanCount = 0;
                                    for (int j = lastEndX; j < startX; j++)
                                    {
                                        int y1 = j;
                                        if (IsVerticalLineTransparentAlphaOnly(bmp, ref y1, j))
                                            cleanCount++;
                                    }
                                    if (cleanCount > 2 && !spaceJustAdded)
                                    {
                                        parts.Add(new ImageSplitterItem(" "));
                                    }
                                }

                                if (startX > 0)
                                    startX--;
                                lastEndX = x;
                                int end = x + 1 - startX;
                                NikseBitmap part = bmp.CopyRectangle(new Rectangle(startX, 0, end, bmp.Height));
                                RemoveBlackBarRight(part);
                                int addY;
                                //                            part.Save("c:\\before" + startX.ToString() + ".bmp"); // just for debugging
                                part = CropTopAndBottom(part, out addY);
                                //                            part.Save("c:\\after" + startX.ToString() + ".bmp"); // just for debugging
                                parts.Add(new ImageSplitterItem(startX, verticalItem.Y + addY, part));
                                spaceJustAdded = false;
                                //                                part.Save(@"d:\temp\cursive.bmp"); // just for debugging
                            }
                            size = 0;
                        }
                        startX = x + 1;
                    }
                    else
                    {
                        size++;
                    }
                }
            }

            if (size > 0)
            {
                if (lastEndX > 0 && lastEndX + xOrMorePixelsMakesSpace < startX && !spaceJustAdded)
                    parts.Add(new ImageSplitterItem(" "));

                if (startX > 0)
                    startX--;
                lastEndX = bmp.Width - 1;
                int end = lastEndX + 1 - startX;
                NikseBitmap part = bmp.CopyRectangle(new Rectangle(startX, 0, end, bmp.Height - 1));
                int addY;
                part = CropTopAndBottom(part, out addY);
                parts.Add(new ImageSplitterItem(startX, verticalItem.Y + addY, part));
                //part.Save(@"d:\temp\cursive.bmp"); // just for debugging
            }
            return parts;
        }
        private static IEnumerable<ImageSplitterItem> SplitHorizontalNew(ImageSplitterItem lineSplitterItem, int xOrMorePixelsMakesSpace)
        {
            var bmp = new NikseBitmap(lineSplitterItem.NikseBitmap);
            bmp.AddTransparentLineRight();
            var parts = new List<ImageSplitterItem>();
            int startX = 0;
            int width = 0;
            int spacePixels = 0;
            int subtractSpacePixels = 0;
            for (int x = 0; x < bmp.Width; x++)
            {
                bool right;
                bool clean;
                List<Point> points = IsVerticalLineTransparetNew(bmp, x, out right, out clean);

                if (points != null && clean)
                {
                    spacePixels++;
                }

                if (right && points != null)
                {
                    int add = FindMaxX(points, x) - x;
                    width = width + add;
                    subtractSpacePixels = add;
                }

                if (points == null)
                {
                    width++;
                }
                else if (width > 1)
                {
                    var bmp0 = new NikseBitmap(bmp);
                    // remove pixels after current;
                    int k = 0;
                    foreach (Point p in points)
                    {
                        bmp0.MakeVerticalLinePartTransparent(p.X, p.X + k, p.Y);
                        k++;
                    }
                    width = FindMaxX(points, x) - startX;
                    width++;
                    NikseBitmap b1 = bmp0.CopyRectangle(new Rectangle(startX, 0, width, bmp.Height));

                    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;
                    const int begin = 0;
                    foreach (Point p in points)
                    {
                        bmp.MakeVerticalLinePartTransparent(begin, p.X, p.Y);
                    }
                    width = 1;
                    startX = FindMinX(points, x);
                    spacePixels = -subtractSpacePixels;
                    subtractSpacePixels = 0;
                }
                else
                {
                    width = 1;
                    startX = FindMinX(points, x);
                }
            }
            return parts;
        }
Ejemplo n.º 18
0
        internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List <VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm)
        {
            ShrinkSelection = false;
            ExpandSelection = false;

            textBoxCharacters.Text = string.Empty;
            if (bestGuess != null)
            {
                buttonGuess.Visible = false; // hm... not too useful :(
                buttonGuess.Text    = bestGuess.Text;
            }
            else
            {
                buttonGuess.Visible = false;
            }

            _vobSubForm = vobSubForm;
            _additions  = additions;

            buttonShrinkSelection.Visible = showShrink;

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

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

            if (_additions.Count > 0)
            {
                var last = _additions[_additions.Count - 1];
                buttonLastEdit.Visible = true;
                if (last.Italic)
                {
                    buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size, FontStyle.Italic);
                }
                else
                {
                    buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size);
                }
                pictureBoxLastEdit.Visible = true;
                pictureBoxLastEdit.Image   = last.Image.GetBitmap();
                buttonLastEdit.Text        = string.Format(Configuration.Settings.Language.VobSubOcrCharacter.EditLastX, last.Text);
                pictureBoxLastEdit.Top     = buttonLastEdit.Top - last.Image.Height + buttonLastEdit.Height;
            }
            else
            {
                buttonLastEdit.Visible     = false;
                pictureBoxLastEdit.Visible = false;
            }

            using (var org = (Bitmap)vobSubImage.Clone())
            {
                var bm = new Bitmap(org.Width, org.Height);
                using (var 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);
                }
                pictureBoxSubtitleImage.Image = bm;
            }

            pictureBoxCharacter.Top = labelCharacters.Top + 16;
            pictureBoxLastEdit.Left = buttonLastEdit.Left + buttonLastEdit.Width + 5;
        }
Ejemplo n.º 19
0
        internal void Initialize(Bitmap vobSubImage, ImageSplitterItem character, Point position, bool italicChecked, bool showShrink, VobSubOcr.CompareMatch bestGuess, List<VobSubOcr.ImageCompareAddition> additions, VobSubOcr vobSubForm)
        {
            ShrinkSelection = false;
            ExpandSelection = false;

            textBoxCharacters.Text = string.Empty;
            if (bestGuess != null)
            {
                buttonGuess.Visible = false; // hm... not too useful :(
                buttonGuess.Text = bestGuess.Text;
            }
            else
            {
                buttonGuess.Visible = false;
            }

            _vobSubForm = vobSubForm;
            _additions = additions;

            buttonShrinkSelection.Visible = showShrink;

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

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

            if (_additions.Count > 0)
            {
                var last = _additions[_additions.Count - 1];
                buttonLastEdit.Visible = true;
                if (last.Italic)
                    buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size, FontStyle.Italic);
                else
                    buttonLastEdit.Font = new Font(buttonLastEdit.Font.FontFamily, buttonLastEdit.Font.Size);
                pictureBoxLastEdit.Visible = true;
                pictureBoxLastEdit.Image = last.Image.GetBitmap();
                buttonLastEdit.Text = string.Format(Configuration.Settings.Language.VobSubOcrCharacter.EditLastX, last.Text);
                pictureBoxLastEdit.Top = buttonLastEdit.Top - last.Image.Height + buttonLastEdit.Height;
            }
            else
            {
                buttonLastEdit.Visible = false;
                pictureBoxLastEdit.Visible = false;
            }

            using (var org = (Bitmap)vobSubImage.Clone())
            {
                var bm = new Bitmap(org.Width, org.Height);
                using (var 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);
                }
                pictureBoxSubtitleImage.Image = bm;
            }

            pictureBoxCharacter.Top = labelCharacters.Top + 16;
            pictureBoxLastEdit.Left = buttonLastEdit.Left + buttonLastEdit.Width + 5;
        }