灰度直方圖是灰度的函數,描述的是圖像中具有該灰度級的像素的個數。如果用直角坐標系來表示,則它的橫坐標是灰度級,縱坐標是該灰度出現的概率(像素的個數)。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace histogram { public partial class histForm : Form { //利用構造函數實現窗體之間的數據傳遞 public histForm(Bitmap bmp) { InitializeComponent(); //把主窗體的圖像數據傳遞給從窗體 bmpHist = bmp; //灰度級計數 countPixel = new int[256]; //8位可表示256個灰度級 } private void close_Click(object sender, EventArgs e) { this.Close(); } //圖像數據 private System.Drawing.Bitmap bmpHist; //灰度等級 private int[] countPixel; //記錄最大的灰度級個數 private int maxPixel; /// <summary> /// 計算各個灰度級所具有的像素個數 /// </summary> private void histForm_Load(object sender, EventArgs e) { //鎖定8位灰度位圖 Rectangle rect = new Rectangle(0, 0, bmpHist.Width, bmpHist.Height); System.Drawing.Imaging.BitmapData bmpData = bmpHist.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmpHist.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = bmpHist.Width * bmpHist.Height; byte[] grayValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);//灰度值數據存入grayValues中 byte temp = 0; maxPixel = 0; //灰度等級數組清零 Array.Clear(countPixel, 0, 256); //計算各個灰度級的像素個數 for (int i = 0; i < bytes; i++) { //灰度級 temp = grayValues[i]; //計數加1 countPixel[temp]++; if (countPixel[temp] > maxPixel) { //找到灰度頻率最大的像素數,用於繪制直方圖 maxPixel = countPixel[temp]; } } //解鎖 System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes); bmpHist.UnlockBits(bmpData); } /// <summary> /// 繪制直方圖 /// </summary> private void histForm_Paint(object sender, PaintEventArgs e) { //獲取Graphics對象 Graphics g = e.Graphics; //創建一個寬度為1的黑色鋼筆 Pen curPen = new Pen(Brushes.Black, 1); //繪制坐標軸 g.DrawLine(curPen, 50, 240, 320, 240);//橫坐標 g.DrawLine(curPen, 50, 240, 50, 30);//縱坐標 //繪制並標識坐標刻度 g.DrawLine(curPen, 100, 240, 100, 242); g.DrawLine(curPen, 150, 240, 150, 242); g.DrawLine(curPen, 200, 240, 200, 242); g.DrawLine(curPen, 250, 240, 250, 242); g.DrawLine(curPen, 300, 240, 300, 242); g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(46, 242)); g.DrawString("50", new Font("New Timer", 8), Brushes.Black, new PointF(92,242)); g.DrawString("100", new Font("New Timer", 8), Brushes.Black, new PointF(139, 242)); g.DrawString("150", new Font("New Timer", 8), Brushes.Black, new PointF(189, 242)); g.DrawString("200", new Font("New Timer", 8), Brushes.Black, new PointF(239, 242)); g.DrawString("250", new Font("New Timer", 8), Brushes.Black, new PointF(289, 242)); g.DrawLine(curPen, 48, 40, 50, 40); g.DrawString("0", new Font("New Timer", 8), Brushes.Black, new PointF(34, 234)); g.DrawString(maxPixel.ToString(), new Font("New Timer", 8), Brushes.Black, new PointF(18, 34)); //繪制直方圖 double temp = 0; for (int i = 0; i < 256; i++) { //縱坐標長度 temp = 200.0 * countPixel[i] / maxPixel; g.DrawLine(curPen, 50 + i, 240, 50 + i, 240 - (int)temp); } //釋放對象 curPen.Dispose(); } } }