Example #1
0
        private static void RemoveBlackBarRight(NikseBitmap bmp)
        {
            int xRemoveBlackBar = bmp.Width - 1;

            for (int yRemoveBlackBar = 0; yRemoveBlackBar < bmp.Height; yRemoveBlackBar++)
            {
                Color c = bmp.GetPixel(xRemoveBlackBar, yRemoveBlackBar);
                if (c.A == 0 || IsColorClose(c, Color.Black, 280))
                {
                    if (bmp.GetPixel(xRemoveBlackBar - 1, yRemoveBlackBar).A == 0)
                    {
                        bmp.SetPixel(xRemoveBlackBar, yRemoveBlackBar, Color.Transparent);
                    }
                }
            }
        }
Example #2
0
        private static bool IsCursiveVerticalLineTransparent(NikseBitmap bmp, int size, int y, int x, List <Point> cursivePoints)
        {
            bool cursiveOk = true;
            int  newY      = y;
            int  newX      = x;

            while (cursiveOk && newY < bmp.Height - 1)
            {
                Color c0 = bmp.GetPixel(newX, newY);
                if (c0.A == 0 || IsColorClose(c0, Color.Black, 280))
                {
                    newY++;
                }
                else
                {
                    Color c1 = bmp.GetPixel(newX - 1, newY - 1);
                    Color c2 = bmp.GetPixel(newX - 1, newY);
                    if ((c1.A == 0 || IsColorClose(c1, Color.Black, 280)) && // still dark color...
                        (c2.A == 0 || IsColorClose(c2, Color.Black, 280)))
                    {
                        cursivePoints.Add(new Point(newX, newY));
                        if (newX > 1)
                        {
                            newX--;
                        }
                        else
                        {
                            cursiveOk = false;
                        }

                        newY++;
                    }
                    else
                    {
                        cursiveOk = false;
                    }
                }

                if (newX < x - size)
                {
                    cursiveOk = false;
                }
            }
            return(cursiveOk);
        }
Example #3
0
        public static List <ImageSplitterItem> SplitVerticalTransparentOrBlack(NikseBitmap bmp, int lineMinHeight)
        {
            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++)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A > 20 && c.R + c.G + c.B > 20)
                    {
                        allTransparent = false;
                        break;
                    }
                }
                if (allTransparent)
                {
                    if (size > 2 && size <= 15)
                    {
                        size++; // at least 15 pixels, like top of 'i' or top of 'È'
                    }
                    else
                    {
                        if (size > 8)
                        {
                            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)
                {
                    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);
        }
Example #4
0
 public ManagedBitmap(NikseBitmap nbmp)
 {
     Width = nbmp.Width;
     Height = nbmp.Height;
     _colors = new Color[Width * Height];
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             this.SetPixel(x, y, nbmp.GetPixel(x, y));
         }
     }
 }
 public ManagedBitmap(NikseBitmap nbmp)
 {
     Width   = nbmp.Width;
     Height  = nbmp.Height;
     _colors = new Color[Width * Height];
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             this.SetPixel(x, y, nbmp.GetPixel(x, y));
         }
     }
 }
Example #6
0
        public static List <ImageSplitterItem> SplitToLinesTransparentOrBlack(NikseBitmap bmp)
        {
            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++)
                {
                    var c = bmp.GetPixel(x, y);
                    if (c.A > 20 && c.R + c.G + c.B > 20)
                    {
                        allTransparent = false;
                        break;
                    }
                }
                if (allTransparent)
                {
                    if (size > 2 && size <= 15)
                    {
                        size++; // at least 15 pixels, like top of 'i' or top of 'È'
                    }
                    else
                    {
                        if (size > 8)
                        {
                            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)
            {
                parts.Add(size == bmp.Height ? new ImageSplitterItem(0, startY, bmp) : new ImageSplitterItem(0, startY, bmp.CopyRectangle(new Rectangle(0, startY, bmp.Width, size + 1))));
            }
            return(parts);
        }
Example #7
0
        private static bool IsVerticalLineTransparent(NikseBitmap bmp, ref int y, int x)
        {
            bool allTransparent = true;

            for (y = 0; y < bmp.Height - 1; y++)
            {
                Color c = bmp.GetPixel(x, y);
                if (c.A == 0 || IsColorClose(c, Color.Black, 280)) // still dark color...
                {
                }
                else
                {
                    allTransparent = false;
                    break;
                }
            }
            return(allTransparent);
        }
Example #8
0
        public static int IsBitmapsAlike(NikseBitmap bmp1, Bitmap bmp2)
        {
            int different = 0;
            int maxDiff   = bmp1.Width * bmp1.Height / 5;

            for (int y = 0; y < bmp1.Height; y++)
            {
                for (int x = 0; x < bmp1.Width; x++)
                {
                    if (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                    {
                        different++;
                    }
                }
            }
            if (different > maxDiff)
            {
                return(different + 10);
            }
            return(different);
        }
Example #9
0
        internal static unsafe int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap 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 (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                    {
                        different++;
                    }
                }
                if (different > maxDiff)
                {
                    return(different + 10);
                }
            }
            return(different);
        }
        public static List<ImageSplitterItem> SplitVerticalTransparentOrBlack(NikseBitmap bmp)
        {
            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++)
                {
                    Color c = bmp.GetPixel(x, y);
                    if (c.A > 20 && c.R + c.G + c.B > 20)
                    {
                        allTransparent = false;
                        break;
                    }
                }
                if (allTransparent)
                {
                    if (size > 2 && size <= 15)
                    {
                        size++; // at least 15 pixels, like top of 'i' or top of 'È'
                    }
                    else
                    {
                        if (size > 8)
                        {
                            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)
                    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;
        }
Example #11
0
        private static NOcrChar NOcrFindExpandedMatch(NikseBitmap nbmp, ImageSplitterItem targetItem, int topMargin, List<NOcrChar> nOcrChars)
        {
            //            var nbmp = new NikseBitmap(parentBitmap);
            int w = targetItem.NikseBitmap.Width;
            int index = 0;
            foreach (NOcrChar oc in nOcrChars)
            {
                    if (oc.ExpandCount > 1 && oc.Width > w && targetItem.X + oc.Width < nbmp.Width)
                    {
                        bool ok = true;
                        index = 0;
                        while (index < oc.LinesForeground.Count && ok)
                        {
                            NOcrPoint op = oc.LinesForeground[index];
                            foreach (Point point in op.GetPoints(oc.Width, oc.Height))
                            {
                                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(oc.Width, oc.Height))
                            {
                                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;
        }
Example #12
0
 private static unsafe int CalculateNumberOfForegroundColors(NikseBitmap nikseBitmap)
 {
     int count = 0;
     for (int y = 0; y < nikseBitmap.Height; y++)
     {
         for (int x = 0; x < nikseBitmap.Width; x++)
         {
             Color c = nikseBitmap.GetPixel(x, y);
             if (c.A > 100 && c.R + c.G + c.B > 200)
                 count++;
         }
     }
     return count;
 }
        private static bool IsCursiveVerticalLineTransparent(NikseBitmap bmp, int size, int y, int x, List<Point> cursivePoints)
        {
            bool cursiveOk = true;
            int newY = y;
            int newX = x;
            while (cursiveOk && newY < bmp.Height - 1)
            {
                Color c0 = bmp.GetPixel(newX, newY);
                if (c0.A == 0 || IsColorClose(c0, Color.Black, 280))
                {
                    newY++;
                }
                else
                {
                    byte[] c1 = bmp.GetPixelColors(newX - 1, newY - 1);
                    byte[] c2 = bmp.GetPixelColors(newX - 1, newY);
                    if ((c1[0] == 0 || IsColorClose(c1[0], c1[1], c1[2], c1[3], Color.Black, 280)) && // still dark color...
                        (c2[0] == 0 || IsColorClose(c2[0], c2[1], c2[2], c2[3], Color.Black, 280)))
                    {
                        cursivePoints.Add(new Point(newX, newY));
                        if (newX > 1)
                            newX--;
                        else
                            cursiveOk = false;

                        newY++;
                    }
                    else
                    {
                        cursiveOk = false;
                    }
                }

                if (newX < x - size)
                    cursiveOk = false;
            }
            return cursiveOk;
        }
 private static void RemoveBlackBarRight(NikseBitmap bmp)
 {
     int xRemoveBlackBar = bmp.Width-1;
     for (int yRemoveBlackBar = 0; yRemoveBlackBar < bmp.Height; yRemoveBlackBar++)
     {
         Color c = bmp.GetPixel(xRemoveBlackBar, yRemoveBlackBar);
         if (c.A == 0 || IsColorClose(c, Color.Black, 280))
         {
             if (bmp.GetPixel(xRemoveBlackBar - 1, yRemoveBlackBar).A == 0)
                 bmp.SetPixel(xRemoveBlackBar, yRemoveBlackBar, Color.Transparent);
         }
     }
 }
 private static bool IsVerticalLineTransparent(NikseBitmap bmp, ref int y, int x)
 {
     bool allTransparent = true;
     for (y = 0; y < bmp.Height - 1; y++)
     {
         Color c = bmp.GetPixel(x, y);
         if (c.A == 0 || IsColorClose(c, Color.Black, 280)) // still dark color...
         {
         }
         else
         {
             allTransparent = false;
             break;
         }
     }
     return allTransparent;
 }
        private static bool IsMatchPointBackGround(NOcrPoint op, bool loose, NikseBitmap nbmp, NOcrChar nOcrChar)
        {
            foreach (Point point in op.ScaledGetPoints(nOcrChar, nbmp.Width, nbmp.Height))
            {
                if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
                {
                    Color c = nbmp.GetPixel(point.X, point.Y);
                    if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                    {
                        return false;
                    }

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

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

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

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

                }
            }
            return true;
        }
 private bool IsMatch()
 {
     NikseBitmap nbmp = new NikseBitmap(pictureBoxCharacter.Image as Bitmap);
     foreach (NOcrPoint op in _nocrChar.LinesForeground)
     {
         foreach (Point point in op.ScaledGetPoints(_nocrChar, nbmp.Width, nbmp.Height))
         {
             if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
             {
                 Color c = nbmp.GetPixel(point.X, point.Y);
                 if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                 {
                 }
                 else
                 {
                     return false;
                 }
             }
         }
     }
     foreach (NOcrPoint op in _nocrChar.LinesBackground)
     {
         foreach (Point point in op.GetPoints())
         {
             if (point.X >= 0 && point.Y >= 0 && point.X < nbmp.Width && point.Y < nbmp.Height)
             {
                 Color c = nbmp.GetPixel(point.X, point.Y);
                 if (c.A > 150 && c.R + 100 + c.G + c.B > VobSubOcr.NocrMinColor)
                 {
                     return false;
                 }
             }
         }
     }
     return true;
 }
        internal static unsafe int IsBitmapsAlike(NikseBitmap bmp1, ManagedBitmap 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 (!IsColorClose(bmp1.GetPixel(x, y), bmp2.GetPixel(x, y), 20))
                        different++;
                }
                if (different > maxDiff)
                    return different + 10;
            }
            return different;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ManagedBitmap"/> class.
 /// </summary>
 /// <param name="oldBitmap">
 /// The old bitmap.
 /// </param>
 public ManagedBitmap(Bitmap oldBitmap)
 {
     NikseBitmap nbmp = new NikseBitmap(oldBitmap);
     this.Width = nbmp.Width;
     this.Height = nbmp.Height;
     this._colors = new Color[this.Width * this.Height];
     for (int y = 0; y < this.Height; y++)
     {
         for (int x = 0; x < this.Width; x++)
         {
             this.SetPixel(x, y, nbmp.GetPixel(x, y));
         }
     }
 }