图像简单处理

public class ImageDistinguish
    {
        public string imgName { get; set;}
        private Bitmap bitmap { get; set; }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="img">包含二维码的发票</param>
        public ImageDistinguish(Bitmap img)
        {
            this.bitmap = img;
        }
        /// <summary>
        /// 构造图像识别
        /// </summary>
        /// <param name="img">包含二维码的发票</param>
        public ImageDistinguish(string path)
        {
            this.bitmap = new Bitmap(path);
            this.imgName = path;
        }
        /// <summary>
        /// 截图,只截取(0,0)到(400,400)位置的图片,包含了二维码了
        /// </summary>
        /// <returns></returns>
        public ImageDistinguish ScreenShot()
        {
            Bitmap destBitmap = new Bitmap(400, 400);//目标图
            Rectangle destRect = new Rectangle(0, 0, 400, 400);//矩形容器
            Rectangle srcRect = new Rectangle(0, 0, 400, 400);
            Graphics gh = Graphics.FromImage(destBitmap);
            gh.DrawImage(this.bitmap, destRect, srcRect, GraphicsUnit.Pixel);
            this.bitmap = destBitmap;
            return this;
        }
        /// <summary>
        /// 二值化处理
        /// </summary>
        /// <returns></returns>
        public ImageDistinguish TwoValued()
        {
            for (int i = 0; i < this.bitmap.Width; i++)
            {
                for (int j = 0; j < this.bitmap.Height; j++)
                {
                    Color pixelColor = this.bitmap.GetPixel(i, j);
                    if (pixelColor.R < 127.5 || pixelColor.G < 127.5 && pixelColor.B < 127.5)
                    {
                        this.bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
                    }
                    else
                    {
                        this.bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                    }
                }
            }
            return this;
        }
        public ImageDistinguish TwoValued(int Level)
        {
            if (Level < 0)
            {
                Level = 0;
            }
            if (Level > 10)
            {
                Level = 10;
            }
            for (int i = 0; i < this.bitmap.Width; i++)
            {
                for (int j = 0; j < this.bitmap.Height; j++)
                {
                    Color pixelColor = this.bitmap.GetPixel(i, j);
                    if (pixelColor.R < Level*20 || pixelColor.G < Level * 20 && pixelColor.B < Level * 20)
                    {
                        this.bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
                    }
                    else
                    {
                        this.bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                    }
                }
            }
            return this;
        }
        /// <summary>
        /// 柔化处理
        /// </summary>
        /// <returns></returns>
        public ImageDistinguish SoftenImage()
        {
            int height = this.bitmap.Height;
            int width = this.bitmap.Width;
            Bitmap newbmp = new Bitmap(width, height);
            LockBitmap lbmp = new LockBitmap(this.bitmap);
            LockBitmap newlbmp = new LockBitmap(newbmp);
            lbmp.LockBits();
            newlbmp.LockBits();

            Color pixel;
            //高斯模板
            int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                    {
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = lbmp.GetPixel(x + row, y + col);
                            r += pixel.R * Gauss[Index];
                            g += pixel.G * Gauss[Index];
                            b += pixel.B * Gauss[Index];
                            Index++;
                        }
                    }
                    r /= 16;
                    g /= 16;
                    b /= 16;
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            }
            lbmp.UnlockBits();
            newlbmp.UnlockBits();
            this.bitmap = newbmp;
            return this;
        }

        /// <summary>
        /// 图像锐化处理
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public ImageDistinguish SharpenImage()
        {
            int height = this.bitmap.Height;
            int width = this.bitmap.Width;
            Bitmap newbmp = new Bitmap(width, height);

            LockBitmap lbmp = new LockBitmap(this.bitmap);
            LockBitmap newlbmp = new LockBitmap(newbmp);
            lbmp.LockBits();
            newlbmp.LockBits();

            Color pixel;
            //拉普拉斯模板
            int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                    {
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = lbmp.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                            g += pixel.G * Laplacian[Index];
                            b += pixel.B * Laplacian[Index];
                            Index++;
                        }
                    }
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            }
            lbmp.UnlockBits();
            newlbmp.UnlockBits();
            return this;
        }
        /// <summary>
        /// 获取处理完成后的图片
        /// </summary>
        /// <returns></returns>
        public Bitmap Result()
        {
            return this.bitmap;
        }
        /// <summary>
        /// 识别二维码
        /// </summary>
        /// <returns></returns>
        public string Distinguish()
        {
            BarcodeReader reader = new BarcodeReader();
            //设置读取的格式(一般为UTF-8)  
            reader.Options.CharacterSet = "UTF-8";
            Result result = reader.Decode(this.bitmap);
            if (result != null)
            {
                return result.Text;
            }
            else
            {
                return "";
            }

        }
        /// <summary>
        /// 3×3中值滤波除杂,yuanbao,2007.10
        /// </summary>
        /// <param name="dgGrayValue"></param>
        public ImageDistinguish ClearNoise()
        {
            int x, y;
            byte[] p = new byte[9]; //最小处理窗口3*3
            byte s;
            int i, j;
            for (y = 1; y < this.bitmap.Height - 1; y++) //--第一行和最后一行无法取窗口
            {
                for (x = 1; x < this.bitmap.Width - 1; x++)
                {
                    //取9个点的值
                    p[0] = this.bitmap.GetPixel(x - 1, y - 1).R;
                    p[1] = this.bitmap.GetPixel(x, y - 1).R;
                    p[2] = this.bitmap.GetPixel(x + 1, y - 1).R;
                    p[3] = this.bitmap.GetPixel(x - 1, y).R;
                    p[4] = this.bitmap.GetPixel(x, y).R;
                    p[5] = this.bitmap.GetPixel(x + 1, y).R;
                    p[6] = this.bitmap.GetPixel(x - 1, y + 1).R;
                    p[7] = this.bitmap.GetPixel(x, y + 1).R;
                    p[8] = this.bitmap.GetPixel(x + 1, y + 1).R;
                    //计算中值
                    for (j = 0; j < 5; j++)
                    {
                        for (i = j + 1; i < 9; i++)
                        {
                            if (p[j] > p[i])
                            {
                                s = p[j];
                                p[j] = p[i];
                                p[i] = s;
                            }
                        }
                    }
                    this.bitmap.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4]));    //给有效值付中值
                }
            }
            return this;
        }
    }
View Code

 

public class LockBitmap
    {
        Bitmap source = null;
        IntPtr Iptr = IntPtr.Zero;
        BitmapData bitmapData = null;

        public byte[] Pixels { get; set; }
        public int Depth { get; private set; }
        public int Width { get; private set; }
        public int Height { get; private set; }

        public LockBitmap(Bitmap source)
        {
            this.source = source;
        }

        /// <summary>
        /// Lock bitmap data
        /// </summary>
        public void LockBits()
        {
            try
            {
                // Get width and height of bitmap
                Width = source.Width;
                Height = source.Height;

                // get total locked pixels count
                int PixelCount = Width * Height;

                // Create rectangle to lock
                Rectangle rect = new Rectangle(0, 0, Width, Height);

                // get source bitmap pixel format size
                Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

                // Check if bpp (Bits Per Pixel) is 8, 24, or 32
                if (Depth != 8 && Depth != 24 && Depth != 32)
                {
                    throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
                }

                // Lock bitmap and return bitmap data
                bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                             source.PixelFormat);

                // create byte array to copy pixel values
                int step = Depth / 8;
                Pixels = new byte[PixelCount * step];
                Iptr = bitmapData.Scan0;

                // Copy data from pointer to array
                Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Unlock bitmap data
        /// </summary>
        public void UnlockBits()
        {
            try
            {
                // Copy data from byte array to pointer
                Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);

                // Unlock bitmap data
                source.UnlockBits(bitmapData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Color GetPixel(int x, int y)
        {
            Color clr = Color.Empty;

            // Get color components count
            int cCount = Depth / 8;

            // Get start index of the specified pixel
            int i = ((y * Width) + x) * cCount;

            if (i > Pixels.Length - cCount)
                throw new IndexOutOfRangeException();

            if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                byte a = Pixels[i + 3]; // a
                clr = Color.FromArgb(a, r, g, b);
            }
            if (Depth == 24) // For 24 bpp get Red, Green and Blue
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                clr = Color.FromArgb(r, g, b);
            }
            if (Depth == 8)
            // For 8 bpp get color value (Red, Green and Blue values are the same)
            {
                byte c = Pixels[i];
                clr = Color.FromArgb(c, c, c);
            }
            return clr;
        }

        /// <summary>
        /// Set the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="color"></param>
        public void SetPixel(int x, int y, Color color)
        {
            // Get color components count
            int cCount = Depth / 8;

            // Get start index of the specified pixel
            int i = ((y * Width) + x) * cCount;

            if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
                Pixels[i + 3] = color.A;
            }
            if (Depth == 24) // For 24 bpp set Red, Green and Blue
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
            }
            if (Depth == 8)
            // For 8 bpp set color value (Red, Green and Blue values are the same)
            {
                Pixels[i] = color.B;
            }
        }
    }
View Code

 

posted @ 2017-10-31 09:53  FreeTimeWorker  阅读(254)  评论(0编辑  收藏  举报