c#的图像处理(直方图;反差,密度的四边圆补偿示例)

模拟Photo的直方图,不过只是显示;
有对图像四角的密度和反差进行补偿,这是现在LDD(LCD+LED)数码裁放机上常用的补偿方法!
主要还是对GDI+的应用,大家不妨看看原码!

http://www.nxxn.net/soft/dreign.rar
 //图像处理--------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace zft

 public class BitmapFilter
 {  
  public static bool modu1(Bitmap image,int r,int g,int b)
  { 
   if(r==0) r=1;
   if(g==0) g=1;
   if(b==0) b=1;
   BitmapData bmData =  image.LockBits(new Rectangle(0, 0,image.Width , image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
   unsafe
   {
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;   
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - image.Width*3;    
    
    int nWidth=image.Width;
    int nHeight=image.Height;
    int red,green,blue;

    int w0=(int)(nWidth/2);
    int h0=(int)(nHeight/2);
    int r0,t1,t2,kr=r,kg=g,kb=b;
    double z,z0;

    int k1r=r,k1g=g,k1b=b,
     k2r=r,k2g=g,k2b=b,
     k3r=r,k3g=g,k3b=b,
     k4r=r,k4g=g,k4b=b;
    
    if(w0>h0)
     r0=w0;
    else
     r0=h0;
    z0=Math.Sqrt(w0*w0+h0*h0);       
    z0=z0/1.5;

    for(int y=0;y<nHeight;++y)   
    {
     for(int x=0; x < nWidth; ++x )
     {
      red=p[2];
      green=p[1];
      blue=p[0];
      
      t1=x-w0;
      t2=y-h0;

      if(t1<0) t1=0-t1;      
      if(t2<0) t2=0-t2;
      
      z=Math.Sqrt(t1*t1+t2*t2);
      if(z<z0)
      {
       z=z0;
       kr=0;
       kg=0;
       kb=0;
      }
      else
      {
       double xx=(z-z0)/z;
       kr=(int)(15*r*xx*xx);
       kg=(int)(15*g*xx*xx);
       kb=(int)(15*b*xx*xx);
      } 
      red+=kr;
      green+=kg;
      blue+=kb; 

      if(red>255) red=255;
      if(red<0)   red=0;
      if(green>255) green=255;
      if(green<0)   green=0;
      if(blue>255) blue=255;
      if(blue<0)   blue=0;
      
      p[2]=(byte)red;
      p[1]=(byte)green;
      p[0]=(byte)blue;
      
      p+=3;      
     }
     p += nOffset; 
     
    }
   }   
   image.UnlockBits(bmData); 
   return true;
  }

  /// <summary>
  /// 反差圆补偿
  /// </summary>
  /// <param name="image">图像</param>
  /// <param name="con">反差系数</param>
  /// <param name="k">圆半径系数</param>
  /// <returns></returns>
  public static bool ccon(Bitmap image,float con,float k)
  {    
   BitmapData bmData =  image.LockBits(new Rectangle(0, 0,image.Width , image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
   unsafe
   {
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;   
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - image.Width*3;    
    
    int nWidth=image.Width;
    int nHeight=image.Height;
    int red,green,blue;

    int w0=(int)(nWidth/2);
    int h0=(int)(nHeight/2);
    int r0,t1,t2;
    double z,z0,pixel,contrast;
    
    if(w0>h0)
     r0=w0;
    else
     r0=h0;
    z0=Math.Sqrt(w0*w0+h0*h0);       
    z0=z0/k;

    for(int y=0;y<nHeight;++y)   
    {
     for(int x=0; x < nWidth; ++x )
     {
      red=p[2];
      green=p[1];
      blue=p[0];
      
      t1=x-w0;
      t2=y-h0;

      if(t1<0) t1=0-t1;      
      if(t2<0) t2=0-t2;
      
      z=Math.Sqrt(t1*t1+t2*t2);
      if(z>z0)
      {      
       contrast = (z-z0)/z * con ;

       pixel = red-(127-red) * contrast;
       if (pixel < 0) pixel = 0;
       if (pixel > 255) pixel = 255;
       p[2] = (byte) pixel;

       pixel = green-(127-green) * contrast;
       if (pixel < 0) pixel = 0;
       if (pixel > 255) pixel = 255;
       p[1] = (byte) pixel;

       pixel = blue-(127-blue) * contrast;
       if (pixel < 0) pixel = 0;
       if (pixel > 255) pixel = 255;
       p[0] = (byte) pixel;       
      }      
      p+=3;      
     }
     p += nOffset;     
    }
   }   
   image.UnlockBits(bmData); 
   return true;
  }
  /// <summary>
  /// 基本反差调整
  /// </summary>
  /// <param name="b"></param>
  /// <param name="nContrast"></param>
  /// <returns></returns>
  public static bool Contrast(Bitmap b, int nContrast)
  {
   if (nContrast < -100) return false;
   if (nContrast >  100) return false;

   double pixel = 0, contrast = (100.0+nContrast)/100.0;

   contrast *= contrast;

   int red, green, blue;
   
   // GDI+ still lies to us - the return format is BGR, NOT RGB.
   BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

   int stride = bmData.Stride;
   System.IntPtr Scan0 = bmData.Scan0;
   int h=b.Height,w=b.Width;

   unsafe
   {
    byte * p = (byte *)(void *)Scan0;

    int nOffset = stride - b.Width*3;

    for(int y=0;y<h;++y)
    {
     for(int x=0; x < w; ++x )
     {
      blue = p[0];
      green = p[1];
      red = p[2];
    
      pixel = red/255.0;
      pixel -= 0.5;
      pixel *= contrast;
      pixel += 0.5;
      pixel *= 255;
      if (pixel < 0) pixel = 0;
      if (pixel > 255) pixel = 255;
      p[2] = (byte) pixel;

      pixel = green/255.0;
      pixel -= 0.5;
      pixel *= contrast;
      pixel += 0.5;
      pixel *= 255;
      if (pixel < 0) pixel = 0;
      if (pixel > 255) pixel = 255;
      p[1] = (byte) pixel;

      pixel = blue/255.0;
      pixel -= 0.5;
      pixel *= contrast;
      pixel += 0.5;
      pixel *= 255;
      if (pixel < 0) pixel = 0;
      if (pixel > 255) pixel = 255;
      p[0] = (byte) pixel;     

      p += 3;
     }
     p += nOffset;
    }
   }

   b.UnlockBits(bmData);

   return true;
  }
 
  public static bool zft(Bitmap b,out int[] all,out int[] rhow, out int[] ghow,out int[] bhow)
  {
   int [] gray=new int[256];
   int [] rr = new int[256];
   int [] gg = new int[256];
   int [] bb = new int[256];
   int tt=0;
   foreach(int i in gray)
   {
    gray[i]=0;
   }
   foreach(int i in rr)
   {
    rr[i]=0;
   }
   foreach(int i in gg)
   {
    gg[i]=0;
   }
   foreach(int i in bb)
   {
    bb[i]=0;
   }

   // GDI+ still lies to us - the return format is BGR, NOT RGB.
   BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

   int stride = bmData.Stride;
   System.IntPtr Scan0 = bmData.Scan0;

   unsafe
   {
    byte * p = (byte *)(void *)Scan0;

    int nOffset = stride - b.Width*3;

    byte red, green, blue;
    int nWidth = b.Width;
    int nHeight= b.Height; 
 
    for(int y=0;y<nHeight;++y)
    {
     for(int x=0; x < nWidth; ++x )
     {
      blue = p[0];
      green = p[1];
      red = p[2];

      tt = (byte)(.299 * red + .587 * green + .114 * blue);
      rr[red]++;
      gg[green]++;
      bb[blue]++;
      gray[tt]++;     
      p += 3;
     }
     p += nOffset;
    }
   }
   all=gray;
   rhow=rr;
   ghow=gg;
   bhow=bb;

   b.UnlockBits(bmData);

   return true;
  }

 }
}

 

//直方图--------------------------------------------------------------------------

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

namespace zft
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private System.Windows.Forms.Button button1;

  int [] gray=new int[256];
  int [] rr = new int[256];
  int [] gg = new int[256];
  int [] bb = new int[256];
  public Bitmap a=new Bitmap(500,500);
  public Bitmap b=new Bitmap(500,500);
  Pen pen1=new Pen(Color.Black);
  int flag=0;    

  float count ;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.Label label9;
  private System.Windows.Forms.Label label10;
  private System.Windows.Forms.Label l_sejie;
  private System.Windows.Forms.Label l_pinjun;
  private System.Windows.Forms.Label l_pixel;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox3;

  float [] gl=new float[256];
  private System.Windows.Forms.Label l_bfb;
  private System.Windows.Forms.Label l_how;

  private bool aline=false;
  private System.Windows.Forms.Label lcolor;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Label label11;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.GroupBox groupBox4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button undo;
  private System.Windows.Forms.Button refresh;

  int xx=-1;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.button5 = new System.Windows.Forms.Button();
   this.l_pinjun = new System.Windows.Forms.Label();
   this.l_pixel = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.groupBox4 = new System.Windows.Forms.GroupBox();
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.label11 = new System.Windows.Forms.Label();
   this.label6 = new System.Windows.Forms.Label();
   this.label5 = new System.Windows.Forms.Label();
   this.lcolor = new System.Windows.Forms.Label();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.l_how = new System.Windows.Forms.Label();
   this.l_bfb = new System.Windows.Forms.Label();
   this.label10 = new System.Windows.Forms.Label();
   this.label9 = new System.Windows.Forms.Label();
   this.label8 = new System.Windows.Forms.Label();
   this.label7 = new System.Windows.Forms.Label();
   this.l_sejie = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.undo = new System.Windows.Forms.Button();
   this.refresh = new System.Windows.Forms.Button();
   this.groupBox1.SuspendLayout();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(276, 56);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(64, 28);
   this.button1.TabIndex = 0;
   this.button1.Text = "灰度";
   this.button1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(276, 92);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(64, 28);
   this.button2.TabIndex = 1;
   this.button2.Text = "红";
   this.button2.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(276, 124);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(64, 28);
   this.button3.TabIndex = 2;
   this.button3.Text = "绿";
   this.button3.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(276, 156);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(64, 28);
   this.button4.TabIndex = 3;
   this.button4.Text = "蓝";
   this.button4.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // label1
   //
   this.label1.BackColor = System.Drawing.Color.White;
   this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.label1.Cursor = System.Windows.Forms.Cursors.Cross;
   this.label1.Location = new System.Drawing.Point(12, 56);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(258, 129);
   this.label1.TabIndex = 4;
   this.label1.Paint += new System.Windows.Forms.PaintEventHandler(this.label1_Paint);
   this.label1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.label1_MouseUp);
   this.label1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label1_MouseMove);
   this.label1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.label1_MouseDown);
   //
   // groupBox1
   //
   this.groupBox1.Controls.Add(this.button5);
   this.groupBox1.Controls.Add(this.l_pinjun);
   this.groupBox1.Controls.Add(this.l_pixel);
   this.groupBox1.Controls.Add(this.textBox1);
   this.groupBox1.Controls.Add(this.groupBox4);
   this.groupBox1.Controls.Add(this.groupBox2);
   this.groupBox1.Controls.Add(this.label11);
   this.groupBox1.Controls.Add(this.label6);
   this.groupBox1.Controls.Add(this.label5);
   this.groupBox1.Controls.Add(this.lcolor);
   this.groupBox1.Controls.Add(this.textBox3);
   this.groupBox1.Controls.Add(this.l_how);
   this.groupBox1.Controls.Add(this.l_bfb);
   this.groupBox1.Controls.Add(this.label10);
   this.groupBox1.Controls.Add(this.label9);
   this.groupBox1.Controls.Add(this.label8);
   this.groupBox1.Controls.Add(this.label7);
   this.groupBox1.Controls.Add(this.l_sejie);
   this.groupBox1.Controls.Add(this.label3);
   this.groupBox1.Controls.Add(this.label1);
   this.groupBox1.Controls.Add(this.button2);
   this.groupBox1.Controls.Add(this.button3);
   this.groupBox1.Controls.Add(this.button1);
   this.groupBox1.Controls.Add(this.button4);
   this.groupBox1.Controls.Add(this.label2);
   this.groupBox1.Font = new System.Drawing.Font("宋体", 12F);
   this.groupBox1.Location = new System.Drawing.Point(8, 4);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(352, 340);
   this.groupBox1.TabIndex = 5;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "直方图";
   this.groupBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.groupBox1_Paint);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(276, 224);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(64, 24);
   this.button5.TabIndex = 26;
   this.button5.Text = "ok";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // l_pinjun
   //
   this.l_pinjun.Location = new System.Drawing.Point(76, 292);
   this.l_pinjun.Name = "l_pinjun";
   this.l_pinjun.Size = new System.Drawing.Size(88, 20);
   this.l_pinjun.TabIndex = 8;
   this.l_pinjun.Text = "0";
   this.l_pinjun.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // l_pixel
   //
   this.l_pixel.Location = new System.Drawing.Point(76, 272);
   this.l_pixel.Name = "l_pixel";
   this.l_pixel.Size = new System.Drawing.Size(88, 20);
   this.l_pixel.TabIndex = 9;
   this.l_pixel.Text = "0";
   this.l_pixel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(124, 224);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(48, 26);
   this.textBox1.TabIndex = 16;
   this.textBox1.Text = "0";
   //
   // groupBox4
   //
   this.groupBox4.Location = new System.Drawing.Point(176, 228);
   this.groupBox4.Name = "groupBox4";
   this.groupBox4.Size = new System.Drawing.Size(44, 8);
   this.groupBox4.TabIndex = 25;
   this.groupBox4.TabStop = false;
   //
   // groupBox2
   //
   this.groupBox2.Location = new System.Drawing.Point(176, 32);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(40, 8);
   this.groupBox2.TabIndex = 24;
   this.groupBox2.TabStop = false;
   //
   // label11
   //
   this.label11.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.label11.Location = new System.Drawing.Point(220, 28);
   this.label11.Name = "label11";
   this.label11.Size = new System.Drawing.Size(52, 23);
   this.label11.TabIndex = 23;
   this.label11.Text = "255";
   this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // label6
   //
   this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.label6.Location = new System.Drawing.Point(124, 28);
   this.label6.Name = "label6";
   this.label6.Size = new System.Drawing.Size(48, 23);
   this.label6.TabIndex = 22;
   this.label6.Text = "0";
   this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(8, 224);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(116, 23);
   this.label5.TabIndex = 21;
   this.label5.Text = "输出色阶范围:";
   //
   // lcolor
   //
   this.lcolor.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.lcolor.Location = new System.Drawing.Point(12, 192);
   this.lcolor.Name = "lcolor";
   this.lcolor.Size = new System.Drawing.Size(260, 12);
   this.lcolor.TabIndex = 19;
   this.lcolor.Paint += new System.Windows.Forms.PaintEventHandler(this.lcolor_Paint);
   //
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(224, 224);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(48, 26);
   this.textBox3.TabIndex = 18;
   this.textBox3.Text = "255";
   //
   // l_how
   //
   this.l_how.Location = new System.Drawing.Point(212, 292);
   this.l_how.Name = "l_how";
   this.l_how.Size = new System.Drawing.Size(120, 20);
   this.l_how.TabIndex = 15;
   this.l_how.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // l_bfb
   //
   this.l_bfb.Location = new System.Drawing.Point(208, 312);
   this.l_bfb.Name = "l_bfb";
   this.l_bfb.Size = new System.Drawing.Size(124, 20);
   this.l_bfb.TabIndex = 14;
   this.l_bfb.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // label10
   //
   this.label10.Location = new System.Drawing.Point(144, 312);
   this.label10.Name = "label10";
   this.label10.Size = new System.Drawing.Size(68, 20);
   this.label10.TabIndex = 13;
   this.label10.Text = "百分比:";
   this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // label9
   //
   this.label9.Location = new System.Drawing.Point(16, 28);
   this.label9.Name = "label9";
   this.label9.Size = new System.Drawing.Size(84, 23);
   this.label9.TabIndex = 12;
   this.label9.Text = "色阶范围:";
   //
   // label8
   //
   this.label8.Location = new System.Drawing.Point(4, 292);
   this.label8.Name = "label8";
   this.label8.Size = new System.Drawing.Size(68, 20);
   this.label8.TabIndex = 11;
   this.label8.Text = "平均值:";
   this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // label7
   //
   this.label7.Location = new System.Drawing.Point(144, 272);
   this.label7.Name = "label7";
   this.label7.Size = new System.Drawing.Size(68, 20);
   this.label7.TabIndex = 10;
   this.label7.Text = "色阶:";
   this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // l_sejie
   //
   this.l_sejie.Location = new System.Drawing.Point(212, 272);
   this.l_sejie.Name = "l_sejie";
   this.l_sejie.Size = new System.Drawing.Size(120, 20);
   this.l_sejie.TabIndex = 7;
   this.l_sejie.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(152, 292);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(60, 20);
   this.label3.TabIndex = 6;
   this.label3.Text = "数量:";
   this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(16, 272);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(56, 20);
   this.label2.TabIndex = 5;
   this.label2.Text = "像素:";
   this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
   //
   // undo
   //
   this.undo.Location = new System.Drawing.Point(368, 16);
   this.undo.Name = "undo";
   this.undo.TabIndex = 6;
   this.undo.Text = "undo";
   this.undo.Click += new System.EventHandler(this.undo_Click);
   //
   // refresh
   //
   this.refresh.Location = new System.Drawing.Point(368, 48);
   this.refresh.Name = "refresh";
   this.refresh.TabIndex = 7;
   this.refresh.Text = "refresh";
   this.refresh.Click += new System.EventHandler(this.refresh_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(450, 350);
   this.Controls.Add(this.refresh);
   this.Controls.Add(this.undo);
   this.Controls.Add(this.groupBox1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
   this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
   this.Name = "Form1";
   this.Text = "色阶和直方图";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.groupBox1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  

  private void Form1_Load(object sender, System.EventArgs e)
  {
            this.TopMost=true;
   this.Left=800;
   BitmapFilter.zft(a,out gray,out rr,out gg, out bb); 
   graydo();
   this.Invalidate();
  }
  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);
   Graphics g=e.Graphics;
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   graydo();
  }
  private void graydo()
  {
   this.flag=1;
   count = a.Width * a.Height;
   gl=new float[256];
   for(int i=0;i<256; i++)
    gl[i]= gray[i]/count *10000;
   pen1=Pens.Black;  
   this.label1.Invalidate();
   this.lcolor.Invalidate();
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   this.flag=2;
   count = a.Width * a.Height;
   gl=new float[256];
   for(int i=0;i<256; i++)
    gl[i]= rr[i]/count *10000;
   pen1=Pens.Red; 
   this.label1.Invalidate();
   this.lcolor.Invalidate();
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   this.flag=3;
   count = a.Width * a.Height;
   gl=new float[256];
   for(int i=0;i<256; i++)
    gl[i]= gg[i]/count *10000;
   pen1=Pens.Green; 
   this.label1.Invalidate();
   this.lcolor.Invalidate();
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   this.flag=4;
   count = a.Width * a.Height;
   gl=new float[256];
   for(int i=0;i<256; i++)
    gl[i]= bb[i]/count *10000;
   pen1=Pens.Blue; 
   this.label1.Invalidate();
   this.lcolor.Invalidate();
  }

  private void label1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   Graphics g=e.Graphics;
   int p;
   p= a.Width*a.Height;
   this.l_pixel.Text=p.ToString();

   int height =this.label1.Height;
   for(int j=0;j<256;j++)
   {
    if(gl[j]>height)
     gl[j]=height;
    g.DrawLine(pen1,j,height,j,height-gl[j]);
   }
   if(aline)
   {
    g.DrawLine(Pens.OrangeRed,xx,0,xx,height);   
   }
   if(xx!=-1)
   {
    this.l_sejie.Text =xx.ToString();
    this.l_how.Text =this.gray[xx].ToString();
    this.l_bfb.Text =this.gl[xx].ToString();
   }
   else
   {
    this.l_sejie.Text=" ";
    this.l_how.Text  =" ";
    this.l_bfb.Text  =" ";   
   }
  }

  private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   xx=e.X; 
   if(xx>255)
    xx=255;
   if(xx<=0)
    xx=0;
   
   aline=true;
   this.label1.Invalidate();
  }

  private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   /*
   if((e.X>255)&&(e.X<0)&&(e.Y<0)&&(e.Y>127))
   {
    this.xx=-1;
   }
   else
   {
    this.xx=e.X;
   }
   this.label4.Text=e.X.ToString()+" "+e.Y.ToString();
   */
   Point p=new Point(e.X,e.Y);
   if(new Rectangle(0,0,256,127).Contains(p))
   {
    this.xx=e.X;
   }
   else
   {
    this.xx=-1;
   }
   
   this.label1.Invalidate();
  }

  private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   aline=false;
  }

  private void lcolor_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   int width=this.lcolor.Width;
   int height=this.lcolor.Height;
   int j;
   Color c;
   Graphics g=e.Graphics;

   switch(flag)
   {
    case 1:
    {
     for(int i=width;i>=0;i--)
     {
      j=i;
      if(j>255) j=255;
      c=Color.FromArgb(j,j,j);
      Pen pen2=new Pen(c,1);   
      g.DrawLine(pen2,i,0,i,height);
     }
     break;
    }
    case 2:
    {
     for(int i=width;i>=0;i--)
     {
      j=i;
      if(j>255) j=255;
      c=Color.FromArgb(j,0,0);
      Pen pen2=new Pen(c,1);   
      g.DrawLine(pen2,i,0,i,height);
     }
     break;
    }
    case 3:
    {
     for(int i=width;i>=0;i--)
     {
      j=i;
      if(j>255) j=255;
      c=Color.FromArgb(0,j,0);
      Pen pen2=new Pen(c,1);   
      g.DrawLine(pen2,i,0,i,height);
     }
     break;
    }
    case 4:
    {
     for(int i=width;i>=0;i--)
     {
      j=i;
      if(j>255) j=255;
      c=Color.FromArgb(0,0,j);
      Pen pen2=new Pen(c,1);   
      g.DrawLine(pen2,i,0,i,height);
     }
     break;
    }
    default:
     break;

   }
  }

  private void groupBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   Graphics g=e.Graphics;
   int x=this.lcolor.Left;
   int y=this.lcolor.Top;
   int w=this.lcolor.Width;
   int h=this.lcolor.Height;

   PointF [] pp=new PointF [4];
   pp[0]=new PointF(x ,y+h+5);
   pp[1]=new PointF(x-5 ,y+h+15);
   pp[2]=new PointF(x+5 ,y+h+15);
   pp[3]=new PointF(x ,y+h+5);

   g.FillPolygon(Brushes.Black,pp);
   
   x+=w/2;

   pp[0]=new PointF(x ,y+h+5);
   pp[1]=new PointF(x-5 ,y+h+15);
   pp[2]=new PointF(x+5 ,y+h+15);
   pp[3]=new PointF(x ,y+h+5);

   g.FillPolygon(Brushes.Gray,pp);
  
   x+=w/2;

   pp[0]=new PointF(x ,y+h+5);
   pp[1]=new PointF(x-5 ,y+h+15);
   pp[2]=new PointF(x+5 ,y+h+15);
   pp[3]=new PointF(x ,y+h+5);

   g.FillPolygon(Brushes.Black,pp);
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   int a1=Convert.ToInt32(this.textBox1.Text);
   int a2=Convert.ToInt32(this.textBox3.Text);
     }

  private void undo_Click(object sender, System.EventArgs e)
  {
   this.a=(Bitmap)this.b.Clone();
  }

  private void refresh_Click(object sender, System.EventArgs e)
  {
   doing();
  }
  public void doing()
  {
   BitmapFilter.zft(a,out gray,out rr,out gg, out bb); 
   graydo();
   this.Invalidate();
  }
 }
}
//有点累,呵呵!

posted @ 2004-09-14 11:15  轮回  阅读(3504)  评论(5编辑  收藏  举报