(转)数字图象处理

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ShowImage
{
    
/// <SUMMARY>
    
/// Form1 的摘要说明。
    
/// </SUMMARY>

    public partial class main : System.Windows.Forms.Form
    
{
        
public string CurPath;
        
private System.Windows.Forms.Button Open;
        
private System.Windows.Forms.PictureBox pictureBox1;
        
private System.Windows.Forms.Button btnAntiColor;
        
private System.Windows.Forms.Button btnCancel;
        
private System.Windows.Forms.Button btnSave;
        
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        
private System.Windows.Forms.Button btnSharp;
        
private System.Windows.Forms.Button btnNormalShow;
        
private System.Windows.Forms.Button btnProfile;
        
private System.Windows.Forms.Button btnerzhihua;
        
private System.Windows.Forms.Button btnMidfiltrate;
        
private System.Windows.Forms.Button btnSplit;
        
private System.Windows.Forms.Button btnSoftHandle;
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.Button button2;
        
private System.Windows.Forms.Button button3;
        
private System.Windows.Forms.Button btnFuDiao;
        
private System.Windows.Forms.Button btnFogHandle;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
/// <SUMMARY>
        
/// 必需的设计器变量。
        
/// </SUMMARY>

        private System.ComponentModel.Container components = null;

        
/// <SUMMARY>
        
/// 清理所有正在使用的资源。
        
/// </SUMMARY>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }

        
public main()
        
{
            InitializeComponent();
        }

        
Windows 窗体设计器生成的代码

        
public void turnEnable()
        
{
            
this.btnAntiColor.Enabled = true;
            
this.btnCancel.Enabled = true;
            
this.btnerzhihua.Enabled = true;
            
this.btnFogHandle.Enabled = true;
            
this.btnFuDiao.Enabled = true;
            
this.btnMidfiltrate.Enabled = true;
            
this.btnNormalShow.Enabled = true;
            
this.btnProfile.Enabled = true;
            
this.btnSave.Enabled = true;
            
this.btnSharp.Enabled = true;
            
this.btnSoftHandle.Enabled = true;
            
this.btnSplit.Enabled = true;
            
this.button1.Enabled = true;
            
this.button2.Enabled = true;
            
this.button3.Enabled = true;
            
this.Open.Enabled = true;
        }

        
public void turnUnenable()
        
{
            
this.btnAntiColor.Enabled = false;
            
this.btnCancel.Enabled = false;
            
this.btnerzhihua.Enabled = false;
            
this.btnFogHandle.Enabled = false;
            
this.btnFuDiao.Enabled = false;
            
this.btnMidfiltrate.Enabled = false;
            
this.btnNormalShow.Enabled = false;
            
this.btnProfile.Enabled = false;
            
this.btnSave.Enabled = false;
            
this.btnSharp.Enabled = false;
            
this.btnSoftHandle.Enabled = false;
            
this.btnSplit.Enabled = false;
            
this.button1.Enabled = false;
            
this.button2.Enabled = false;
            
this.button3.Enabled = false;
            
this.Open.Enabled = false;
        }


        
private void Open_Click(object sender, System.EventArgs e)//打开图片
        {
            
this.label1.Visible = false;
            
this.label2.Visible = false;
            OpenFileDialog myDlg 
= new OpenFileDialog();
            myDlg.Filter 
= "所有图像格式(*.bmp,*jpg,*.jpeg,*.gif,*,png)|*.bmp;*jpg;*.jepg;*.gif;*.png"//设置文件过滤器
            if (myDlg.ShowDialog() == DialogResult.OK)
            
{
                CurPath 
= myDlg.FileName;
                
this.pictureBox1.Image = Bitmap.FromFile(CurPath);
            }

        }



        
private void btnAntiColor_Click(object sender, System.EventArgs e) //反色处理
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                
//以反色方式显示图像            
                int Height, Width;
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    Width 
= this.pictureBox1.Image.Width - 1;     //获取当前图像象素高度
                    Height = this.pictureBox1.Image.Height - 1;     //获取当前图像象素宽度
                }

                
else
                
{
                    Width 
= this.pictureBox1.Width - 1;
                    Height 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap 
= new Bitmap(this.pictureBox1.Image);   //以当前的图像大小令创一个空图像
                Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;
                Color pixel;  
//申请一个颜色(用ARGB表示)
                for (int x = 1; x < Width; x++)
                
{
                    
for (int y = 1; y < Height; y++)
                    
{
                        
int r, g, b;
                        pixel 
= MyBitmap.GetPixel(x, y);
                        r 
= 255 - pixel.R;  //对当前r颜色取反
                        g = 255 - pixel.G;  //对当前g颜色取反
                        b = 255 - pixel.B;  //对当前b颜色取反
                        bitmap.SetPixel(x, y, Color.FromArgb(r, g, b));        //用取反后的argb值图像创建一个图像    
                    }

                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap;
                }

                
this.turnEnable();
            }

        }



        
private void btnCancel_Click(object sender, System.EventArgs e) //退出
        {
            Application.Exit();
        }



        
private void btnSave_Click(object sender, System.EventArgs e) //保存图片
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                Bitmap newmap 
= new Bitmap(this.pictureBox1.Image);
                
this.saveFileDialog1.Filter = "All Files(*.*)|*.*";
                
this.saveFileDialog1.ShowDialog();
                
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    
this.pictureBox1.Image.Save(saveFileDialog1.FileName);

            }

        }



        
/// <summary>
        
/// 锐化算法:运用拉普拉斯算子,计算该图像f(i,j)这个点的像素值,再减去f(i+1,j),f(i-1,j),f(i,j+1)和f(i,j-1)的象素值,
        
/// 最后加上4倍f(i,j),即为处理后的图像在这一点的像素值。
        
/// </summary>

        private void btnSharp_Click(object sender, System.EventArgs e) //锐化处理
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                Color c1 
= new Color();
                Color c2 
= new Color();
                Color c3 
= new Color();
                Color c4 
= new Color();
                Color c5 
= new Color();

                
int rr, r1, r2, r3, r4, r5, fxr;
                
int WIDTH, HEIGH;

                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 2;
                    HEIGH 
= this.pictureBox1.Image.Height - 2;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }


                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);

                
for (int ii = 1; ii < WIDTH; ii++)
                
{
                    
for (int jj = 1; jj < HEIGH; jj++)
                    
{
                        c1 
= bitmap1.GetPixel(ii, jj - 1);
                        c2 
= bitmap1.GetPixel(ii - 1, jj);
                        c3 
= bitmap1.GetPixel(ii, jj);
                        c4 
= bitmap1.GetPixel(ii + 1, jj);
                        c5 
= bitmap1.GetPixel(ii, jj + 1);

                        r1 
= c1.R;
                        r2 
= c2.R;
                        r3 
= c3.R;
                        r4 
= c4.R;
                        r5 
= c5.R;


                        fxr 
= 5 * r3 - (r4 + r2 + r5 + r1);
                        rr 
= Math.Abs(fxr);
                        
if (rr < 0) rr = 0;
                        
if (rr > 255) rr = 255;

                        Color cc 
= Color.FromArgb(rr, rr, rr);
                        bitmap2.SetPixel(ii, jj, cc);

                    }

                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }



        
private void btnNormalShow_Click(object sender, System.EventArgs e) //正常显示
        {
            
this.label1.Visible = false;
            
this.label2.Visible = false;
            
if (this.pictureBox1.Image != null)
                
this.pictureBox1.Image = Bitmap.FromFile(CurPath);
        }



        
/// <summary>
        
/// 8领域微分拉普拉斯算子算法如下:假设原图像为f(i,j),处理后的图像为g(i,j),则
        
/// g(i,j) = abs( 8 * f(i,j) - f(i,j-1) - f(i-1,j) - f(i+1,j) - f(i,j+1) - f(i-1,j-1) 
        
///                   - f(i-1,j+1) - f(i+1,j-1) - f(i+1,j+1) )
        
/// </summary>

        private void btnProfile_Click(object sender, System.EventArgs e) //提取轮廓,8领域微分拉普拉斯算子
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                Color c1 
= new Color();
                Color c2 
= new Color();
                Color c3 
= new Color();
                Color c4 
= new Color();
                Color c5 
= new Color();
                Color c6 
= new Color();
                Color c7 
= new Color();
                Color c8 
= new Color();
                Color c9 
= new Color();

                
int rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr;
                
int WIDTH, HEIGH;

                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 2;
                    HEIGH 
= this.pictureBox1.Image.Height - 2;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }


                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);

                
for (int ii = 1; ii < WIDTH; ii++)
                
{
                    
for (int jj = 1; jj < HEIGH; jj++)
                    
{
                        c1 
= bitmap1.GetPixel(ii, jj - 1);
                        c2 
= bitmap1.GetPixel(ii - 1, jj);
                        c3 
= bitmap1.GetPixel(ii, jj);
                        c4 
= bitmap1.GetPixel(ii + 1, jj);
                        c5 
= bitmap1.GetPixel(ii, jj + 1);
                        c6 
= bitmap1.GetPixel(ii - 1, jj - 1);
                        c7 
= bitmap1.GetPixel(ii - 1, jj + 1);
                        c8 
= bitmap1.GetPixel(ii + 1, jj - 1);
                        c9 
= bitmap1.GetPixel(ii + 1, jj + 1);

                        r1 
= c1.R;
                        r2 
= c2.R;
                        r3 
= c3.R;
                        r4 
= c4.R;
                        r5 
= c5.R;
                        r6 
= c6.R;
                        r7 
= c7.R;
                        r8 
= c8.R;
                        r9 
= c9.R;

                        fxr 
= 8 * r3 - r1 - r2 - r4 - r5 - r6 - r7 - r8 - r9;
                        rr 
= Math.Abs(fxr);
                        
if (rr < 0) rr = 0;
                        
if (rr > 255) rr = 255;

                        Color cc 
= Color.FromArgb(rr, rr, rr);
                        bitmap2.SetPixel(ii, jj, cc);

                    }

                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }



        
/// <summary>
        
/// 等灰度片二值化算法:将输入图像在某两个等宽的灰度级范围内的所有象素全部置为0(黑),其余
        
/// 灰度级的所有象素全部置为255(白),则生成黑白二值图像。本例中的灰度级范围是50和100
        
/// </summary>

        private void btnerzhihua_Click(object sender, System.EventArgs e)//等灰度片二值化
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                Color c 
= new Color();
                
int WIDTH, HEIGH, rr, lev, wid, x, y, m;
                
int[] lut = new int[256];
                
int[, ,] pic = new int[100010003];
                lev 
= 50;
                wid 
= 500;
                y 
= 0;

                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 2;
                    HEIGH 
= this.pictureBox1.Image.Height - 2;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }


                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);

                
while (y <= 256)        //循环赋值,使lut在0~50和150~200之间为255,在50~150和200~256之间为0
                {
                    
for (x = 0; x <= lev - 1; x++)
                    
{
                        lut[y] 
= 255;
                        y 
= y + 1;
                        
if (y > 255goto loop;
                    }

                    
for (x = 0; x < wid - 1; x++)
                    
{
                        lut[y] 
= 0;
                        y 
= y + 1;
                        
if (y > 255goto loop;
                    }

                }




                 
/*分别将一个点的RGB值赋给3个二维数组,为以后判别象素在什么范围内和
                  生成图像做准备
*/

            loop:
                
for (int i = 0; i < WIDTH; i++)
                
{
                    
for (int j = 0; j < HEIGH; j++)
                    
{
                        c 
= bitmap1.GetPixel(i, j);
                        pic[i, j, 
0= c.R;
                        pic[i, j, 
1= c.G;
                        pic[i, j, 
2= c.B;
                    }

                }

                
for (int i = 0; i < WIDTH; i++)  //判断象素范围并生成新的图像
                {
                    
for (int j = 0; j < HEIGH; j++)
                    
{
                        m 
= pic[i, j, 2];  //这里取B值进行判断,依据其大小决定其范围并将数组中的值赋给象素点
                        rr = lut[m];

                        Color c1 
= Color.FromArgb(rr, rr, rr);
                        bitmap2.SetPixel(i, j, c1);
                    }

                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }




        
/// <summary>
        
/// 中值滤波算法:设当前图像为f(i,j),处理后的图像为g(i,j),以当前象素f(i,j)为中心切出一个N*N象素组成的图像块,
        
/// 则g(i,j)的像素值取这个图像块灰度值排序序列中的中间值
        
/// </summary>

        private void btnMidfiltrate_Click(object sender, System.EventArgs e)  //中值滤波
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                Color c 
= new Color();
                Color cc 
= new Color();
                
int WIDTH, HEIGH, rr, r1, g1, b1, k1, k2, i1, j1, dm, m;
                
int[] dt = new int[20];
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }


                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);

                
for (int i = 1; i < WIDTH; i++)
                
{
                    
for (int j = 1; j < HEIGH; j++)
                    
{
                        rr 
= 0; m = 0;
                        
for (k1 = -1; k1 <= 1; k1++)  //循环找出当前点的周围象素点,这里取此点周围的8个点
                        {
                            
for (k2 = -1; k2 <= 1; k2++)
                            
{
                                c 
= bitmap1.GetPixel(i + k1, j + k2);
                                r1 
= c.R;
                                g1 
= c.G;
                                b1 
= c.B;
                                dt[m] 
= r1;        //将8个点中的每个点的象素值存入数组

                                m 
= m + 1;
                            }

                        }

                        
for (i1 = m - 1; i1 >= 1; i1 = i1 - 1)   //对数组dt进行冒泡排序
                        {
                            
for (j1 = 1; j1 <= i1; j1 = j1 + 1)
                            
{
                                
if (dt[j1 - 1> dt[j1])
                                
{
                                    dm 
= dt[j1];
                                    dt[j1] 
= dt[j1 - 1];
                                    dt[j1 
- 1= dm;
                                }

                            }

                        }


                        rr 
= dt[(int)(m / 2)];   //取中间值,赋给输出点
                        Color c1 = Color.FromArgb(rr, rr, rr);

                        bitmap2.SetPixel(i, j, c1);
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }



        
/// <summary>
        
/// 柔化处理算法:设当前图像为f(i,j),处理后的图像为g(i-1,j-1),则g(i-1,j-1)的像素值是f(i,j)周围
        
/// 8个点的象素值分别乘以对应的高斯模板,然后再相加。
        
/// </summary>

        private void btnSoftHandle_Click(object sender, System.EventArgs e) //柔化处理
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                
int HEIGH, WIDTH;
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width;
                    HEIGH 
= this.pictureBox1.Image.Height;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width;
                    HEIGH 
= this.pictureBox1.Height;
                }

                Bitmap bitmap 
= new Bitmap(this.pictureBox1.Image);
                Bitmap MyBitmap 
= (Bitmap)this.pictureBox1.Image;
                Color pixel;
                
//高斯模板
                int[] Gauss =121242121 };
                
for (int x = 1; x < WIDTH - 1; x++)
                
{
                    
for (int y = 1; y < HEIGH - 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 
= MyBitmap.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;
                        bitmap.SetPixel(x 
- 1, y - 1, Color.FromArgb(r, g, b));
                    }

                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap;
                }

                
this.turnEnable();
            }

        }




        
/// <summary>
        
/// 图像分割算法:先求出合适的阈值,再根据这个阈值对图像进行二值化
        
/// </summary>

        private void btnSplit_Click(object sender, System.EventArgs e) //图像分割
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                
int HEIGH, WIDTH, Blue, temp0 = 0, temp1 = 0, temp2 = 0, temp3 = 0, T2, T1 = 127;
                Color c;
                
int[] gray = new int[256];
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);

                
for (int i = 0; i < WIDTH; i++)   //计算此图像的灰度值,这里以B灰度为例
                    for (int j = 0; j < HEIGH; j++)
                    
{
                        c 
= bitmap1.GetPixel(i, j);
                        Blue 
= c.B;
                        gray[Blue]
++;
                    }


                
while (true)  //循环算出合适的阈值
                {
                    
for (int i = 0; i < T1 + 1; i++)
                    
{
                        temp0 
+= gray[i] * i;
                        temp1 
+= gray[i];
                    }

                    
for (int i = T1 + 1; i < 256; i++)
                    
{
                        temp2 
+= gray[i] * i;
                        temp3 
+= gray[i];
                    }

                    T2 
= (temp0 / temp1 + temp2 / temp3) / 2;
                    
if (T1 == T2)
                        
break;
                    
else
                        T1 
= T2;
                }


                
for (int i = 0; i < WIDTH; i++)  //生成处理后的图像
                {
                    
for (int j = 0; j < HEIGH; j++)
                    
{
                        
int B = bitmap1.GetPixel(i, j).B;
                        
if (B < T1)
                            B 
= 0;
                        
else
                            B 
= 255;
                        bitmap2.SetPixel(i, j, Color.FromArgb(B, B, B));
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }


        }




        
/// <summary>
        
/// 直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
        
/// 然后用二维坐标表示出来。
        
/// </summary>

        private void button1_Click(object sender, System.EventArgs e) //B灰度直方图
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = true;
                
this.label2.Visible = true;

                
//                System.Random random = new Random();
                
//                int a = random.Next(255);

                
int HEIGH, WIDTH, Blue;
                Color c;
                
float[] gray = new float[256];
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(512512);
                
int sum = WIDTH * HEIGH;

                
for (int i = 0; i < 256; i++)
                    gray[i] 
= 0.0f;

                
for (int i = 0; i < WIDTH; i++)    //计算灰度值,此处以B灰度为例
                    for (int j = 0; j < HEIGH; j++)
                    
{
                        c 
= bitmap1.GetPixel(i, j);
                        Blue 
= c.B;
                        gray[Blue]
++;
                    }


                
for (int i = 0; i < 512; i = i + 2)
                
{
                    
for (int j = 0; j < 512; j++)
                    
{
                        
if (i == 0)
                            bitmap2.SetPixel(i, j, Color.FromArgb(
100100100));
                        
if (j <= ((gray[i / 2/ sum) * 10000)) //求出象素比例,再扩大10000倍
                            bitmap2.SetPixel(i, j, Color.FromArgb(00255));
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }


        }




        
/// <summary>
        
/// 直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
        
/// 然后用二维坐标表示出来。
        
/// </summary>

        private void button2_Click(object sender, System.EventArgs e) //R灰度直方图
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = true;
                
this.label2.Visible = true;
                
int HEIGH, WIDTH, Blue;
                Color c;
                
float[] gray = new float[256];
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }


                
int sum = WIDTH * HEIGH;
                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(512512);

                
for (int i = 0; i < 256; i++)
                    gray[i] 
= 0.0f;


                
for (int i = 0; i < WIDTH; i++)
                    
for (int j = 0; j < HEIGH; j++)
                    
{
                        c 
= bitmap1.GetPixel(i, j);
                        Blue 
= c.R;
                        gray[Blue]
++;
                    }


                
for (int i = 0; i < 512; i = i + 2)
                
{
                    
for (int j = 0; j < 512; j++)
                    
{
                        
if (i == 0)
                            bitmap2.SetPixel(i, j, Color.FromArgb(
100100100));
                        
if (j <= ((gray[i / 2/ sum) * 10000)) //求出象素比例,再扩大10000倍
                            bitmap2.SetPixel(i, j, Color.FromArgb(25500));
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }




        
/// <summary>
        
/// 直方图算法:计算整个图像的灰度值(例如 G灰度值从0~255分别在当前图像中所占的比例),
        
/// 然后用二维坐标表示出来。
        
/// </summary>

        private void button3_Click(object sender, System.EventArgs e) //G灰度直方图
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = true;
                
this.label2.Visible = true;
                
int HEIGH, WIDTH, Blue;
                Color c;
                
float[] gray = new float[256];
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap2 
= new Bitmap(512512);
                
int sum = WIDTH * HEIGH;

                
for (int i = 0; i < WIDTH; i++)
                    
for (int j = 0; j < HEIGH; j++)
                    
{
                        c 
= bitmap1.GetPixel(i, j);
                        Blue 
= c.G;
                        gray[Blue]
++;
                    }


                
for (int i = 0; i < 512; i = i + 2)
                
{
                    
for (int j = 0; j < 512; j++)
                    
{
                        
if (i == 0)
                            bitmap2.SetPixel(i, j, Color.FromArgb(
100100100));
                        
if (j <= ((gray[i / 2/ sum) * 10000))  //求出象素比例,再扩大10000倍 
                            bitmap2.SetPixel(i, j, Color.FromArgb(02550));
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }




        
/// <summary>
        
///浮雕处理算法: 设当前图像为f(i,j),处理后的图像为g(i,j),则g(i,j)的象素值为f(i,j)的像素
        
///值减去f(i+1,j+1)的像素值再加上128,最后取和的绝对值
        
/// </summary>

        private void btnFuDiao_Click(object sender, System.EventArgs e)  //浮雕处理
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                
int HEIGH, WIDTH;
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    WIDTH 
= this.pictureBox1.Image.Width - 1;
                    HEIGH 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    WIDTH 
= this.pictureBox1.Width - 1;
                    HEIGH 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap2 
= new Bitmap(this.pictureBox1.Image);
                Bitmap bitmap1 
= new Bitmap(this.pictureBox1.Image);
                Color pixel1, pixel2;
                
for (int x = 0; x < WIDTH; x++)
                
{
                    
for (int y = 0; y < HEIGH; y++)
                    
{
                        
int r = 0, g = 0, b = 0;
                        pixel1 
= bitmap1.GetPixel(x, y);
                        pixel2 
= bitmap1.GetPixel(x + 1, y + 1);
                        r 
= Math.Abs(pixel1.R - pixel2.R + 128);
                        g 
= Math.Abs(pixel1.G - pixel2.G + 128);
                        b 
= Math.Abs(pixel1.B - pixel2.B + 128);
                        
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;
                        bitmap2.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap2;
                }

                
this.turnEnable();
            }

        }



        
/// <summary>
        
/// 雾化处理算法: 设当前图像为f(i,j),处理后的图像为g(i,j),则g(i,j)的象素值其实是f(i,j)点
        
/// 按照某种规律对应的另外一个点的象素值。在此例中的另外一点,是f(i+random % 19,j+random % 19)
        
/// 其中random是随机数。
        
/// </summary>

        private void btnFogHandle_Click(object sender, System.EventArgs e) //雾化处理
        {
            
if (this.pictureBox1.Image != null)
            
{
                
this.turnUnenable();
                
this.label1.Visible = false;
                
this.label2.Visible = false;
                
int Height, Width;
                
if (this.pictureBox1.Image.Width < this.pictureBox1.Width ||
                    
this.pictureBox1.Image.Height < this.pictureBox1.Height)
                
{
                    Width 
= this.pictureBox1.Image.Width - 1;
                    Height 
= this.pictureBox1.Image.Height - 1;
                }

                
else
                
{
                    Width 
= this.pictureBox1.Width - 1;
                    Height 
= this.pictureBox1.Height - 1;
                }

                Bitmap bitmap 
= new Bitmap(this.pictureBox1.Image);
                Bitmap MyBitmap 
= (Bitmap)this.pictureBox1.Image;
                Color pixel;
                
for (int x = 1; x < Width; x++)
                
{
                    
for (int y = 1; y < Height; y++)
                    
{
                        System.Random MyRandom 
= new Random();
                        
int k = MyRandom.Next(123456);
                        
//像素块大小
                        int dx = x + k % 19;
                        
int dy = y + k % 19;
                        
if (dx >= Width)
                            dx 
= Width;
                        
if (dy >= Height)
                            dy 
= Height;
                        pixel 
= MyBitmap.GetPixel(dx, dy);
                        bitmap.SetPixel(x, y, pixel);
                    }


                    
this.pictureBox1.Refresh();
                    
this.pictureBox1.Image = bitmap;
                }

                
this.turnEnable();
            }

        }


        
private void main_Load(object sender, System.EventArgs e)
        
{
            
this.label1.Visible = false;
            
this.label2.Visible = false;
        }

    }

}
原文地址:https://www.cnblogs.com/gxh973121/p/798702.html