• C#数字图像处理(摘录)


    namespace ImageProcessor
    {
        class RGBImage
        {
            private Bitmap bitmap;
            public RGBImage(Bitmap bitmap)
            {
                this.bitmap = bitmap;
            }
            private int GetWidth()
            {
                return this.bitmap.Size.Width;
            }
            private int GetHeight()
            {
                return this.bitmap.Size.Height;
            }
            private Color RuleRGBValue(int r,int g,int b)
            {
                Color c = new Color();
                if (r > 255) r = 255;
                if (r < 0) r = 0;
                if (g > 255) g = 255;
                if (g < 0) g = 0;
                if (b > 255) b = 255;
                if (b < 0) b = 0;
                c = Color.FromArgb(r, g, b);
                return c;
            }
            /// <summary>
            /// 单色蓝图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToBlueImage()
            {
                Color c = new Color();
                Color c1 = new Color();
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        c1 = Color.FromArgb(c.B, c.B, c.B);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 单色绿图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToGreenImage()
            {
                Color c = new Color();
                Color c1 = new Color();
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        c1 = Color.FromArgb(c.R, c.R, c.R);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 单色红图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToRedImage()
            {
                Color c = new Color();
                Color c1 = new Color();
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        c1 = Color.FromArgb(255-c.B, 255-c.B, 255-c.B);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 单亮度图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToBrightnessImage()
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                int y;
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        y = (int)(0.31 * c.R + 0.59 * c.G + 0.11 * c.B);
                        c1 = RuleRGBValue(y, y, y);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 灰度图片
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public Bitmap ToGrayImage(int n)
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                int y;
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        y = (int)((c.R + c.G + c.B)/n);
                        c1 = RuleRGBValue(y, y, y);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap; 
            }
            /// <summary>
            /// 逆反图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToInverseImage()
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        c1 = Color.FromArgb(255-c.R, 255-c.G, 255-c.B);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 平滑图片
            /// </summary>
            /// <param name="nn"></param>
            /// <returns></returns>
            public Bitmap ToSmoothImage(int nn)
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                int mm = (nn - 1) / 2;
                for (int i = mm; i < bitmap.Size.Width - mm; i++)
                {
                    for (int j = mm; j < bitmap.Size.Height - mm; j++)
                    {
                        int r = 0, g = 0, b = 0;
                        for (int m = i - mm; m <= i + mm; m++)
                        {
                            for (int n = j - mm; n <= j + mm; n++)
                            {
                                c = bitmap.GetPixel(m, n);
                                r = r + c.R;
                                g = g + c.G;
                                b = b + c.B;
                            }
                        }
                        r = r / (nn*nn);
                        g = g / (nn * nn);
                        b = b / (nn * nn);
                        c1 = RuleRGBValue(r, g, b);
                        rtBitmap.SetPixel(i, j, c1);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 霓虹图片
            /// </summary>
            /// <returns></returns>
            public Bitmap ToNeonImage()
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                Color c2 = new Color();
                Color c3 = new Color();
                int r1, r2, g1, g2, b1, b2;
                int r,g,b;
                for (int i = 0; i < bitmap.Size.Width-1; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height-1; j++)
                    {
                        c1 = bitmap.GetPixel(i, j);
                        c2 = bitmap.GetPixel(i + 1, j);
                        c3 = bitmap.GetPixel(i, j + 1);
                        r1 = (c1.R - c2.R) ^ 2;
                        g1 = (c1.G - c2.G) ^ 2;
                        b1 = (c1.B - c2.B) ^ 2;
                        r2 = (c1.R - c3.R) ^ 2;
                        g2 = (c1.G - c3.G) ^ 2;
                        b2 = (c1.B - c3.B) ^ 2;
                        r = (int)(2 * Math.Sqrt(r1 +r2));
                        g = (int)(2 * Math.Sqrt(g1 + g2));
                        b = (int)(2 * Math.Sqrt(b1 + b2));
                        c = RuleRGBValue(r, g, b);
                        rtBitmap.SetPixel(i, j, c);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 锐化图片
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public Bitmap ToSharpeningImage(double n)
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                Color c2 = new Color();
                int r, g, b;
                for (int i = 1; i < bitmap.Size.Width; i++)
                {
                    for (int j = 1; j < bitmap.Size.Height; j++)
                    {
                        c1 = bitmap.GetPixel(i, j);
                        c2 = bitmap.GetPixel(i-1,j-1);
                        r = (int)(c1.R + n * Math.Abs(c1.R - c2.R));
                        g = (int)(c1.G + n * Math.Abs(c1.G - c2.G));
                        b = (int)(c1.B + n * Math.Abs(c1.B - c2.B));
                        c = RuleRGBValue(r,g,b);
                        rtBitmap.SetPixel(i, j, c);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 浮雕图片
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public Bitmap ToReliefImage(int n)
            {
                if (n < 0) n = 0;
                if (n > 255) n = 255;
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                Color c2 = new Color();
                int r, g, b;
                for (int i = 1; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c1 = bitmap.GetPixel(i, j);
                        c2 = bitmap.GetPixel(i - 1, j);
                        r = c1.R - c2.R + n;
                        g = c1.G - c2.G + n;
                        b = c1.B - c2.B + n;
                        c = RuleRGBValue(r, g, b);
                        rtBitmap.SetPixel(i, j, c);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 镶嵌图片
            /// </summary>
            /// <param name="nn"></param>
            /// <returns></returns>
            public Bitmap ToMosaicImage(int nn)
            {
                if (nn < 0) nn = 3;
                int mm = (nn - 1) / 2;
                Bitmap rtBitmap = new Bitmap(bitmap.Size.Width, bitmap.Size.Height);
                Color c = new Color();
                Color c1 = new Color();
                for (int i = mm; i < bitmap.Size.Width - mm; i++)
                {
                    for (int j = mm; j < bitmap.Size.Height - mm; j++)
                    {
                        int r = 0, g = 0, b = 0;
                        for (int m = i - mm; m <= i + mm; m++)
                        {
                            for (int n = j - mm; n <= j + mm; n++)
                            {
                                c = bitmap.GetPixel(m, n);
                                r = r + c.R;
                                g = g + c.G;
                                b = b + c.B;
                            }
                        }
                        r = r / (nn*nn);
                        g = g / (nn * nn);
                        b = b / (nn * nn);
                        c1 = RuleRGBValue(r, g, b);
                        for (int m = i - mm; m <= i + mm; m++)
                        {
                            for (int n = j - mm; n <= j + mm; n++)
                            {
                                rtBitmap.SetPixel(m, n, c1);
                            }
                        }
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 平移图片
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public Bitmap TranslationImage(int n)
            {
                if (n < 0|| n>255) n = 20;
                Bitmap rtBitmap=new Bitmap (bitmap.Width,bitmap.Height);
                Color c=new Color ();
                for(int i=0;i<bitmap.Size.Width-n;i++)
                {
                    for(int j=0;j<bitmap.Size.Height-n;j++)
                    {
                        c=bitmap.GetPixel(i,j);
                        rtBitmap.SetPixel(i + n, j + n, c);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="n"></param>
            /// <returns></returns>
            public Bitmap ResizeImage(double n)
            {
                Bitmap rtBitmap = new Bitmap((int)(bitmap.Size.Width*n),(int)(bitmap.Size.Height*n));
                Color c = new Color();
                int x, y; 
                for (int i = 0; i < bitmap.Size.Width; i++)
                {
                    for (int j = 0; j < bitmap.Size.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        x = Convert.ToInt16(i * n);
                        y = Convert.ToInt16(j * n);
                        if (x < 0) x = 0;
                        if (x >= rtBitmap.Size.Width) x = rtBitmap.Size.Width-1;
                        if (y < 0) y = 0;
                        if (y >= rtBitmap.Size.Height) y = rtBitmap.Size.Height-1;
                        rtBitmap.SetPixel(x,y, c);
                    }
                }
                return rtBitmap;
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public Bitmap LRFlipImage()
            {
                Bitmap rtBitmap =new Bitmap (bitmap.Width,bitmap.Height );
                Color c = new Color();
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        rtBitmap.SetPixel(bitmap.Width - 1 - i, j, c);
                    }
                }
                return rtBitmap;
            }
            public Bitmap UDFlipImage()
            {
                Bitmap rtBitmap = new Bitmap(bitmap.Width, bitmap.Height);
                Color c = new Color();
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        c = bitmap.GetPixel(i, j);
                        rtBitmap.SetPixel(i, bitmap.Height-1-j, c);
                    }
                }
                return rtBitmap;
            }
            public Bitmap RotateImage(double n)
            {
                if (n < 0) n = 1;
                if (n > 360) n = 360;
                double nn = 180 / n;
                Bitmap rtBitmap = new Bitmap(bitmap.Width,bitmap.Height);
                int x,y;
                double pi = Math.PI;
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        x =(int)( i * Math.Cos(pi / nn) - j * Math.Sin(pi / nn));
                        y=(int)(i*Math.Sin(pi/6)+j*Math.Cos(pi/6));
                        if(x>0&& x<bitmap.Width&&y>0&&y<bitmap.Height)
                        rtBitmap.SetPixel(x, y, bitmap.GetPixel(i, j));
                    }
                }
                return rtBitmap;
            }
            public Bitmap HistImage()
            {
                RGBImage rgb = new RGBImage(this.bitmap);
                Bitmap tempBitmap = rgb.ToGrayImage(3);
                Color c =Color.FromA#ffffff;
                int[] times = new int[256];
                for (int i = 0; i < tempBitmap.Width; i++)
                {
                    for (int j = 0; j < tempBitmap.Height; j++)
                    {
                        c = tempBitmap.GetPixel(i, j);
                        times[c.R]++;
                    }
                }
                int max = times[0];
                for (int i = 1; i < 256; i++)
                {
                    if (times[i] > max) max = times[i];
                }
                for (int i = 0; i < 256; i++)
                {
                    times[i] = Convert.ToInt16(((double)(times[i]) /(double)( max))*100);
                }
                Bitmap rtBitmap = new Bitmap(256, 110);
                for (int i = 0; i < rtBitmap.Width; i++)
                {
                    for (int j = 0; j < rtBitmap.Height; j++)
                    {
                        for (int m = 0; m <= times[i]; m++)
                        {
                            rtBitmap.SetPixel(i, m, c);
                        }
                    }
                }
                RGBImage rimage = new RGBImage(rtBitmap);
                Bitmap s=rimage.UDFlipImage();
                return s;
            }
        }
    }

            private void btnToBlueImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bBitmap = rgbBitmap.ToBlueImage();
                ShowImage showImage = new ShowImage(bBitmap,"单色蓝图片");
                showImage.Show();
            }
            private void btnToGreenImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bBitmap = rgbBitmap.ToGreenImage();
                ShowImage showImage = new ShowImage(bBitmap, "单色绿图片");
                showImage.Show();
            }
            private void btnToRedImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bBitmap = rgbBitmap.ToRedImage();
                ShowImage showImage = new ShowImage(bBitmap, "单色红图片");
                showImage.Show();
            }
            private void btnToBrightnessImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.ToBrightnessImage();
                ShowImage s = new ShowImage(bitmap, "单亮度图片");
                s.Show();
            }
            private void btnToGrayImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog inputDialog = new InputDialog("请输入采用的模板大小:", "模板大小","3");
                inputDialog.ShowDialog();
                if (inputDialog.result != "")
                {
                    int n = Convert.ToInt16(inputDialog.result);
                    Bitmap bitmap = rgbBitmap.ToGrayImage(n);
                    ShowImage s = new ShowImage(bitmap, "灰度图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnInverseImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.ToInverseImage();
                ShowImage s = new ShowImage(bitmap, "逆反图片");
                s.Show();
            }
            private void btnSmoothImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog inputDialog = new InputDialog("请输入采用的模板大小:", "模板大小", "3");
                inputDialog.ShowDialog();
                if (inputDialog.result != "")
                {
                    int n = Convert.ToInt16(inputDialog.result);
                    Bitmap bitmap = rgbBitmap.ToSmoothImage(n);
                    ShowImage s = new ShowImage(bitmap, "平滑图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnNeonImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.ToNeonImage();
                ShowImage s = new ShowImage(bitmap, "霓虹图片");
                s.Show();
            }
            private void btnSharpenImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog inputDialog = new InputDialog("请输入锐化系数:", "输入参数", "0.25");
                inputDialog.ShowDialog();
                if (inputDialog.result != "")
                {
                    double n = Convert.ToDouble(inputDialog.result);
                    Bitmap bitmap = rgbBitmap.ToSharpeningImage(n);
                    ShowImage s = new ShowImage(bitmap, "锐化图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnReliefImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入浮雕值:","输入参数","128");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    int n = Convert.ToInt16(nn.result);
                    Bitmap bitmap = rgbBitmap.ToReliefImage(n);
                    ShowImage s = new ShowImage(bitmap, "浮雕图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnMosaicImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入模板大小:","输入参数","3");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    int n = Convert.ToInt16(nn.result);
                    Bitmap bitmap = rgbBitmap.ToMosaicImage(n);
                    ShowImage s = new ShowImage(bitmap, "镶嵌图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnTranslationImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入平移的像素数:","输入参数","20");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    int n = Convert.ToInt16(nn.result);
                    Bitmap bitmap = rgbBitmap.TranslationImage(n);
                    ShowImage s = new ShowImage(bitmap, "平移图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnLargerImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入放大倍数:","输入参数","2");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    double n = Convert.ToDouble(nn.result);
                    Bitmap bitmap = rgbBitmap.ResizeImage(n);
                    ShowImage s = new ShowImage(bitmap, "放大图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnSmallerImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入缩小倍数:","输入参数","0.5");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    double n = Convert.ToDouble(nn.result);
                    Bitmap bitmap = rgbBitmap.ResizeImage(n);
                    ShowImage s = new ShowImage(bitmap, "缩小图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnLRFlipImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.LRFlipImage();
                ShowImage s = new ShowImage(bitmap, "左右翻转");
                s.Show();
            }
            private void btnUDFlipImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.UDFlipImage();
                ShowImage s = new ShowImage(bitmap, "上下翻转");
                s.Show();
            }
            private void ImageProcessor_Load(object sender, EventArgs e)
            {
                this.MaximumSize = new Size(this.Width, this.Height);
                this.MinimumSize = this.MaximumSize;
            }
            private void btnRotateImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                InputDialog nn=new InputDialog ("输入旋转度数:","输入参数","30");
                nn.ShowDialog();
                if (nn.result != "")
                {
                    double n = Convert.ToDouble(nn.result);
                    Bitmap bitmap = rgbBitmap.RotateImage(n);
                    ShowImage s = new ShowImage(bitmap, "旋转图片");
                    s.Show();
                }
                else
                {
                    MessageBox.Show("对不起,你的输入参数错误!");
                }
            }
            private void btnHistImage_Click(object sender, EventArgs e)
            {
                RGBImage rgbBitmap = new RGBImage(bmp);
                Bitmap bitmap = rgbBitmap.HistImage();
                ShowImage s = new ShowImage(bitmap, "直方图");
                s.Show();
            }

  • 相关阅读:
    T-SQL取时间的不同方法
    JavaScript获取地址栏中的参数值并存入一个对象
    jquery.ocupload上传文件到指定目录
    Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(三)
    Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)
    Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(一)
    Python2.x和3.x主要差异总结
    python学习视频整理
    Django-Rest-Framework 教程: 快速入门
    Django RESTful API 设计指南
  • 原文地址:https://www.cnblogs.com/Yjianyong/p/2743002.html
Copyright © 2020-2023  润新知