public static NikseBitmap CropTopAndBottom(NikseBitmap bmp, out int topCropping)
        {
            int startTop = 0;
            int maxTop = bmp.Height - 2;
            if (maxTop > bmp.Height)
                maxTop = bmp.Height;
            for (int y = 0; y < maxTop; y++)
            {
                bool allTransparent = true;
                for (int x = 0; x < bmp.Width; x++)
                {
                    int a = bmp.GetAlpha(x, y);
                    if (a != 0)
                    {
                        allTransparent = false;
                        break;
                    }
                }
                if (!allTransparent)
                    break;
                startTop++;
            }
            //if (startTop > 9)
            //startTop -= 5; // if top space > 9, then allways leave blank 5 pixels on top (so . is not confused with ').
            topCropping = startTop;

            int h = bmp.Height;
            bool bottomCroppingDone = false;
            for (int y = bmp.Height - 1; y > 3; y--)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    int a = bmp.GetAlpha(x, y);
                    if (a != 0)
                    {
                        bottomCroppingDone = true;
                        break;
                    }
                }
                h = y;
                if (bottomCroppingDone)
                    break;
            }
            if (h - startTop + 1 <= 0)
                return new NikseBitmap(0, 0);
            return bmp.CopyRectangle(new Rectangle(0, startTop, bmp.Width, h - startTop + 1));
        }
        public static NikseBitmap CropTopAndBottom(NikseBitmap bmp, out int topCropping, int maxDifferentPixelsOnLine)
        {
            int startTop = 0;
            int maxTop = bmp.Height - 2;
            if (maxTop > bmp.Height)
                maxTop = bmp.Height;

            for (int y = 0; y < maxTop; y++)
            {
                int difference = 0;
                bool allTransparent = true;
                for (int x = 1; x < bmp.Width - 1; x++)
                {
                    int a = bmp.GetAlpha(x, y);
                    if (a != 0)
                    {
                        difference++;
                        if (difference >= maxDifferentPixelsOnLine)
                        {
                            allTransparent = false;
                            break;
                        }
                    }
                }
                if (!allTransparent)
                    break;
                startTop++;
            }
            if (startTop > 9)
                startTop -= 5; // if top space > 9, then allways leave blank 5 pixels on top (so . is not confused with ').
            topCropping = startTop;

            for (int y = bmp.Height - 1; y > 3; y--)
            {
                int difference = 0;
                bool allTransparent = true;
                for (int x = 1; x < bmp.Width - 1; x++)
                {
                    int a = bmp.GetAlpha(x, y);
                    if (a != 0)
                    {
                        difference++;
                        if (difference >= maxDifferentPixelsOnLine)
                        {
                            allTransparent = false;
                            break;
                        }
                    }
                }
                if (allTransparent == false)
                    return bmp.CopyRectangle(new Rectangle(0, startTop, bmp.Width - 1, y - startTop + 1));
            }
            return bmp;
        }
        internal static int IsBitmapsAlike(NikseBitmap bmp1, Ocr.Binary.BinaryOcrBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;

            for (int x = 1; x < bmp1.Width; x++)
            {
                for (int y = 1; y < bmp1.Height; y++)
                {
                    if (bmp1.GetAlpha(x, y) < 100 && bmp2.GetPixel(x, y) > 0)
                        different++;
                }
                if (different > maxDiff)
                    return different + 10;
            }
            return different;
        }
        internal static int IsBitmapsAlike(Ocr.Binary.BinaryOcrBitmap bmp1, NikseBitmap bmp2)
        {
            int different = 0;
            int maxDiff = bmp1.Width * bmp1.Height / 5;

            for (int x = 0; x < bmp1.Width; x++)
            {
                for (int y = 0; y < bmp1.Height; y++)
                {
                    //if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                    if (bmp1.GetPixel(x, y) > 0 && bmp2.GetAlpha(x, y) < 100)
                        different++;
                }
                if (different > maxDiff)
                    return different + 10;
            }
            return different;
        }
        private static List<Point> IsVerticalLineTransparetNew(NikseBitmap bmp, int x, out bool right, out bool clean)
        {
            right = false;
            bool left = false;
            int leftCount = 0;
            int rightCount = 0;
            clean = true;
            var points = new List<Point>();
            int y = 0;
            int maxSlide = bmp.Height / 4;
            while (y < bmp.Height)
            {
                if (bmp.GetAlpha(x, y) > 100)
                {
                    clean = false;
                    if (x == 0)
                        return null;

                    if (x < bmp.Width - 1 && y < bmp.Height - 1 && bmp.GetAlpha(x + 1, y) == 0 && bmp.GetAlpha(x + 1, y + 1) == 0)
                    {
                        //if pixels to the left - move right?
                        if (bmp.GetAlpha(x - 1, y) > 0)
                        {
                            x++; //(requires search for min/max x in points
                            right = true;
                        }
                        else if (x > 0 && bmp.GetAlpha(x - 1, y) == 0)
                        {
                            x--; //(requires search for min/max x in points
                            left = true;
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else if (x < bmp.Width - 1 && y == bmp.Height - 1 && bmp.GetAlpha(x + 1, y) == 0 && bmp.GetAlpha(x + 1, y - 1) == 0)
                    {
                        //if pixels to the left - move right?
                        if (bmp.GetAlpha(x - 1, y) > 0)
                        {
                            x++; //(requires search for min/max x in points
                        }
                        else
                        {
                            return null;
                        }
                        right = true;
                    }
                    else if (bmp.GetAlpha(x - 1, y) == 0)
                    {
                        x--;
                        left = true;
                    }
                    else if (y > 5 && bmp.GetAlpha(x - 1, y - 1) == 0)
                    {
                        x--;
                        y--;
                        left = true;
                        while (points.Count > 0 && points[points.Count - 1].Y > y)
                            points.RemoveAt(points.Count - 1);
                    }
                    else if (y > 5 && bmp.GetAlpha(x - 1, y - 2) == 0)
                    {
                        x--;
                        y -= 2;
                        left = true;
                        while (points.Count > 0 && points[points.Count - 1].Y > y)
                            points.RemoveAt(points.Count - 1);
                    }
                    else
                    {
                        return null;
                    }

                    if (left)
                        leftCount++;

                    if (right)
                        rightCount++;

                    if (leftCount > maxSlide || rightCount > maxSlide)
                        return null;
                }
                else
                {
                    points.Add(new Point(x, y));
                    y++;
                }
            }
            return points;
        }
 private static bool IsVerticalLineTransparentAlphaOnly(NikseBitmap bmp, ref int y, int x)
 {
     bool allTransparent = true;
     for (y = 0; y < bmp.Height - 1; y++)
     {
         int a = bmp.GetAlpha(x, y);
         if (a == 0) // still dark color...
         {
         }
         else
         {
             allTransparent = false;
             break;
         }
     }
     return allTransparent;
 }
 private static void RemoveBlackBarRight(NikseBitmap bmp)
 {
     int xRemoveBlackBar = bmp.Width - 1;
     for (int yRemoveBlackBar = 0; yRemoveBlackBar < bmp.Height; yRemoveBlackBar++)
     {
         byte[] c = bmp.GetPixelColors(xRemoveBlackBar, yRemoveBlackBar);
         if (c[0] == 0 || IsColorClose(c[0], c[1], c[2], c[3], Color.Black, 280))
         {
             if (bmp.GetAlpha(xRemoveBlackBar - 1, yRemoveBlackBar) == 0)
                 bmp.SetPixel(xRemoveBlackBar, yRemoveBlackBar, Color.Transparent);
         }
     }
 }
 public static List<ImageSplitterItem> SplitVertical(NikseBitmap bmp, int minLineHeight)
 { // split into lines
     int startY = 0;
     int size = 0;
     var parts = new List<ImageSplitterItem>();
     for (int y = 0; y < bmp.Height; y++)
     {
         bool allTransparent = true;
         for (int x = 0; x < bmp.Width; x++)
         {
             int a = bmp.GetAlpha(x, y);
             if (a != 0)
             {
                 allTransparent = false;
                 break;
             }
         }
         if (allTransparent)
         {
             if (size > 2 && size <= minLineHeight)
             {
                 size++; // at least 'lineMinHeight' pixels, like top of 'i'
             }
             else
             {
                 if (size > 2)
                 {
                     NikseBitmap part = bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1));
                     //                            part.Save("c:\\line_0_to_width.bmp");
                     parts.Add(new ImageSplitterItem(0, startY, part));
                     //                            bmp.Save("c:\\original.bmp");
                 }
                 size = 0;
                 startY = y;
             }
         }
         else
         {
             size++;
         }
     }
     if (size > 2)
     {
         if (size == bmp.Height)
         {
             if (size > 100)
                 return SplitVerticalTransparentOrBlack(bmp);
             else
                 parts.Add(new ImageSplitterItem(0, startY, bmp));
         }
         else
         {
             parts.Add(new ImageSplitterItem(0, startY, bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1))));
         }
     }
     return parts;
 }
        /// <summary>
        /// The initialize via nikse bmp.
        /// </summary>
        /// <param name="nbmp">
        /// The nbmp.
        /// </param>
        private void InitializeViaNikseBmp(NikseBitmap nbmp)
        {
            this.Width = nbmp.Width;
            this.Height = nbmp.Height;
            this._colors = new byte[this.Width * this.Height];
            for (int y = 0; y < this.Height; y++)
            {
                for (int x = 0; x < this.Width; x++)
                {
                    this.SetPixelViaAlpha(x, y, nbmp.GetAlpha(x, y));
                }
            }

            this.Hash = MurMurHash3.Hash(this._colors);
            this.CalcuateNumberOfColoredPixels();
        }
Exemplo n.º 10
0
 private void InitializeViaNikseBmp(NikseBitmap nbmp)
 {
     Width = nbmp.Width;
     Height = nbmp.Height;
     _colors = new byte[Width * Height];
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             SetPixelViaAlpha(x, y, nbmp.GetAlpha(x, y));
         }
     }
     Hash = MurMurHash3.Hash(_colors);
     CalcuateNumberOfColoredPixels();
 }
        private static List<ImageSplitterItem> SplitVerticalAggresive(NikseBitmap bmp, int minLineHeight, double averageLineHeight)
        {
            int startY = 0;
            int size = 0;
            var parts = new List<ImageSplitterItem>();
            for (int y = 0; y < bmp.Height; y++)
            {
                int a;
                bool allTransparent = true;
                for (int x = 0; x < bmp.Width; x++)
                {
                    a = bmp.GetAlpha(x, y);
                    if (a != 0)
                    {
                        allTransparent = false;
                        break;
                    }
                }

                if (size > 5 && size >= minLineHeight && size > averageLineHeight && !allTransparent && bmp.Width > 50 && y < bmp.Height - 5)
                {
                    var leftX = 0;
                    while (leftX < bmp.Width)
                    {
                        a = bmp.GetAlpha(leftX, y);
                        if (a != 0)
                        {
                            break;
                        }
                        leftX++;
                    }
                    var rightX = bmp.Width;
                    while (rightX > 0)
                    {
                        a = bmp.GetAlpha(rightX, y - 1);
                        if (a != 0)
                        {
                            break;
                        }
                        rightX--;
                    }
                    if (leftX >= rightX)
                    {
                        allTransparent = true;
                    }

                    leftX = 0;
                    while (leftX < bmp.Width)
                    {
                        a = bmp.GetAlpha(leftX, y - 1);
                        if (a != 0)
                        {
                            break;
                        }
                        leftX++;
                    }
                    rightX = bmp.Width;
                    while (rightX > 0)
                    {
                        a = bmp.GetAlpha(rightX, y);
                        if (a != 0)
                        {
                            break;
                        }
                        rightX--;
                    }
                    if (leftX >= rightX)
                    {
                        allTransparent = true;
                    }
                }

                if (allTransparent)
                {
                    if (size > 2 && size <= minLineHeight)
                    {
                        size++; // at least 'lineMinHeight' pixels, like top of 'i'
                    }
                    else
                    {
                        if (size > 2)
                        {
                            var part = bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1));
                            parts.Add(new ImageSplitterItem(0, startY, part));
                        }
                        size = 0;
                        startY = y;
                    }
                }
                else
                {
                    size++;
                }
            }
            if (size > 2)
            {
                if (size == bmp.Height)
                {
                    if (size > 100)
                        return SplitVerticalTransparentOrBlack(bmp);
                    parts.Add(new ImageSplitterItem(0, startY, bmp));
                }
                else
                {
                    parts.Add(new ImageSplitterItem(0, startY, bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1))));
                }
            }
            if (parts.Count == 1 && averageLineHeight > 5 && bmp.Height > averageLineHeight * 3)
            {
                return parts;
            }
            return parts;
        }
 private static bool IsVerticalLineTransparentAlphaOnly(NikseBitmap bmp, int x)
 {
     for (int y = 0; y < bmp.Height - 1; y++)
     {
         int a = bmp.GetAlpha(x, y);
         if (a != 0) // not a dark color
         {
             return false;
         }
     }
     return true;
 }
 /// <summary>
 /// split into lines
 /// </summary>
 public static List<ImageSplitterItem> SplitVertical(NikseBitmap bmp, int minLineHeight, double averageLineHeight = -1)
 {
     int startY = 0;
     int size = 0;
     var parts = new List<ImageSplitterItem>();
     for (int y = 0; y < bmp.Height; y++)
     {
         bool allTransparent = true;
         for (int x = 0; x < bmp.Width; x++)
         {
             int a = bmp.GetAlpha(x, y);
             if (a != 0)
             {
                 allTransparent = false;
                 break;
             }
         }
         if (allTransparent)
         {
             if (size > 1 && size <= minLineHeight)
             {
                 size++; // at least 'lineMinHeight' pixels, like top of 'i'
             }
             else
             {
                 if (size > 1)
                 {
                     var part = bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1));
                     parts.Add(new ImageSplitterItem(0, startY, part));
                 }
                 size = 0;
                 startY = y;
             }
         }
         else
         {
             size++;
         }
     }
     if (size > 1)
     {
         if (size == bmp.Height)
         {
             if (size > 100)
                 return SplitVerticalTransparentOrBlack(bmp);
             parts.Add(new ImageSplitterItem(0, startY, bmp));
         }
         else
         {
             parts.Add(new ImageSplitterItem(0, startY, bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1))));
         }
     }
     if (parts.Count == 1 && averageLineHeight > 5 && bmp.Height > averageLineHeight * 3)
     {
         return SplitVerticalAggresive(bmp, minLineHeight, averageLineHeight);
     }
     return parts;
 }
Exemplo n.º 14
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 - oc.MarginTop);
                            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 - oc.MarginTop);
                            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 - oc.MarginTop);
                            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 - oc.MarginTop);
                            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);
        }