Пример #1
0
        private static Dictionary <string, Point> GetBlock(Bitmap bm, int x, int y)
        {
            Dictionary <string, Point> dictionary = new Dictionary <string, Point>();
            Stack <Point> stack = new Stack <Point>();
            Color         pixel = bm.GetPixel(x, y);
            bool          flag  = ImageHelper.ArgbEqual(pixel, Color.White);
            Dictionary <string, Point> result;

            if (flag)
            {
                result = dictionary;
            }
            else
            {
                stack.Push(new Point(x, y));
                while (stack.Count != 0)
                {
                    Point  point = stack.Pop();
                    string key   = point.X + "#" + point.Y;
                    dictionary[key] = new Point(point.X, point.Y);
                    List <Point> list  = new List <Point>();
                    Point        item  = new Point(point.X + 1, point.Y);
                    bool         flag2 = item.X < bm.Width;
                    if (flag2)
                    {
                        Color pixel2 = bm.GetPixel(item.X, item.Y);
                        bool  flag3  = ImageHelper.ArgbEqual(pixel2, Color.Black);
                        if (flag3)
                        {
                            list.Add(item);
                        }
                    }
                    item = new Point(point.X - 1, point.Y);
                    bool flag4 = item.X >= 0;
                    if (flag4)
                    {
                        Color pixel3 = bm.GetPixel(item.X, item.Y);
                        bool  flag5  = ImageHelper.ArgbEqual(pixel3, Color.Black);
                        if (flag5)
                        {
                            list.Add(item);
                        }
                    }
                    item = new Point(point.X, point.Y + 1);
                    bool flag6 = item.Y < bm.Height;
                    if (flag6)
                    {
                        Color pixel4 = bm.GetPixel(item.X, item.Y);
                        bool  flag7  = ImageHelper.ArgbEqual(pixel4, Color.Black);
                        if (flag7)
                        {
                            list.Add(item);
                        }
                    }
                    item = new Point(point.X, point.Y - 1);
                    bool flag8 = item.Y >= 0;
                    if (flag8)
                    {
                        Color pixel5 = bm.GetPixel(item.X, item.Y);
                        bool  flag9  = ImageHelper.ArgbEqual(pixel5, Color.Black);
                        if (flag9)
                        {
                            list.Add(item);
                        }
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        Point item2 = list[i];
                        key = item2.X + "#" + item2.Y;
                        bool flag10 = !dictionary.ContainsKey(key);
                        if (flag10)
                        {
                            stack.Push(item2);
                        }
                    }
                }
                result = dictionary;
            }
            return(result);
        }
Пример #2
0
 public static Bitmap Rotate(this Image im, int angle)
 {
     return(ImageHelper.Rotate(new Bitmap(im), angle));
 }
Пример #3
0
        public static Bitmap RemoveBlackEdge(this Image img)
        {
            Bitmap     bitmap      = new Bitmap(img);
            Rectangle  rect        = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData  = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
            int        width       = bitmapData.Width;
            int        height      = bitmapData.Height;
            int        stride      = bitmapData.Stride;
            double     picByteSize = ImageHelper.GetPicByteSize(bitmap.PixelFormat);
            int        num         = (int)Math.Ceiling(picByteSize * (double)width);
            int        num2        = stride - num;
            IntPtr     scan        = bitmapData.Scan0;
            int        num3        = stride * height;
            int        num4        = 0;

            ImageHelper.rgbValues = new byte[num3];
            Marshal.Copy(scan, ImageHelper.rgbValues, 0, num3);
            int num5 = (int)((double)num * 0.02);
            int num6 = (int)((double)height * 0.02);
            int num7 = (int)picByteSize;

            for (int i = 0; i < height; i++)
            {
                int j;
                for (j = 0; j < num; j += 4)
                {
                    bool flag  = true;
                    bool flag2 = ImageHelper.rgbValues[num4] <= 20;
                    if (flag2)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            bool flag3 = num4 + k >= num3;
                            if (flag3)
                            {
                                break;
                            }
                            bool flag4 = k < 3;
                            if (flag4)
                            {
                                ImageHelper.rgbValues[num4 + k] = 255;
                            }
                            else
                            {
                                ImageHelper.rgbValues[num4 + k] = 0;
                            }
                        }
                    }
                    bool flag5 = ImageHelper.rgbValues[num4] > 20;
                    if (flag5)
                    {
                        for (int l = 1; l <= num7; l++)
                        {
                            bool flag6 = num4 + l >= num3;
                            if (flag6)
                            {
                                break;
                            }
                            bool flag7 = ImageHelper.rgbValues[num4 + l] <= 15;
                            if (flag7)
                            {
                                flag = false;
                            }
                        }
                    }
                    bool flag8 = ImageHelper.rgbValues[num4] <= 20 || j >= num / 2;
                    if (flag8)
                    {
                        flag = false;
                    }
                    ImageHelper.recCheck(ref ImageHelper.rgbValues, num4, height, stride, true);
                    bool flag9 = num4 + 4 < num3;
                    if (!flag9)
                    {
                        break;
                    }
                    num4 += 4;
                    bool flag10 = j >= num5 & flag;
                    if (flag10)
                    {
                        break;
                    }
                }
                bool flag11 = j == num;
                if (flag11)
                {
                    num4 += num2;
                }
                else
                {
                    bool flag12 = num4 + num2 + num - j - 1 >= num3;
                    if (flag12)
                    {
                        break;
                    }
                    num4 += (num2 + num - j - 1) / 4 * 4;
                }
            }
            num4 = (num3 - 1) / 4 * 4;
            for (int i = height - 1; i >= 0; i--)
            {
                num4 -= num2;
                int j;
                for (j = (num - 1) / 4 * 4; j >= 0; j -= 4)
                {
                    bool flag   = true;
                    bool flag13 = ImageHelper.rgbValues[num4] <= 20;
                    if (flag13)
                    {
                        for (int m = 0; m < 4; m++)
                        {
                            bool flag14 = num4 + m >= num3;
                            if (flag14)
                            {
                                break;
                            }
                            bool flag15 = m < 3;
                            if (flag15)
                            {
                                ImageHelper.rgbValues[num4 + m] = 255;
                            }
                            else
                            {
                                ImageHelper.rgbValues[num4 + m] = 0;
                            }
                        }
                    }
                    bool flag16 = ImageHelper.rgbValues[num4] > 20;
                    if (flag16)
                    {
                        for (int n = 1; n <= num7; n++)
                        {
                            bool flag17 = num4 - n <= 0;
                            if (flag17)
                            {
                                break;
                            }
                            bool flag18 = ImageHelper.rgbValues[num4 - n] <= 20;
                            if (flag18)
                            {
                                flag = false;
                            }
                        }
                    }
                    bool flag19 = ImageHelper.rgbValues[num4] <= 20 || num / 2 > j;
                    if (flag19)
                    {
                        flag = false;
                    }
                    ImageHelper.recCheck(ref ImageHelper.rgbValues, num4, height, stride, false);
                    bool flag20 = num4 - 4 > 0;
                    if (!flag20)
                    {
                        break;
                    }
                    num4 -= 4;
                    bool flag21 = num6 < height - i;
                    if (flag21)
                    {
                        bool flag22 = j < num - num5 & flag;
                        if (flag22)
                        {
                            break;
                        }
                    }
                }
                bool flag23 = j != -1;
                if (flag23)
                {
                    num4 -= j;
                }
            }
            Marshal.Copy(ImageHelper.rgbValues, 0, scan, num3);
            bitmap.UnlockBits(bitmapData);
            return(bitmap);
        }
Пример #4
0
        public unsafe static Bitmap OtsuThreshold(this Image img)
        {
            Bitmap bitmap = new Bitmap(img);
            int    width  = bitmap.Width;
            int    height = bitmap.Height;
            byte   b      = 0;

            int[]      array      = new int[256];
            int        num        = 0;
            int        num2       = 0;
            double     num3       = 0.0;
            double     num4       = 0.0;
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            byte *     ptr        = (byte *)((void *)bitmapData.Scan0);
            int        num5       = bitmapData.Stride - width * 4;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    array[(int)(*ptr)]++;
                    ptr += 4;
                }
                ptr += num5;
            }
            bitmap.UnlockBits(bitmapData);
            for (int k = 0; k < 256; k++)
            {
                num3 += (double)(k * array[k]);
                num  += array[k];
            }
            double num6 = -1.0;

            for (int l = 0; l < 256; l++)
            {
                num2 += array[l];
                int  num7 = num - num2;
                bool flag = num7 == 0;
                if (flag)
                {
                    break;
                }
                num4 += (double)(l * array[l]);
                double num8  = num3 - num4;
                double num9  = num4 / (double)num2;
                double num10 = num8 / (double)num7;
                double num11 = (double)num2 * num9 * num9 + (double)num7 * num10 * num10;
                bool   flag2 = num11 > num6;
                if (flag2)
                {
                    num6 = num11;
                    b    = (byte)l;
                }
            }
            bool flag3 = b == 0;

            if (flag3)
            {
                b = 127;
            }
            return(ImageHelper.Threshoding(bitmap, b));
        }