文章目錄
- 1.灰度直方圖
- 1.1 灰度直方圖定義
- 1.2 灰度直方圖編程實例
- 2.線性點運算
- 2.1線性點運算定義
- 2.2 線性點運算編程實例
- 3.全等級直方圖灰度拉伸
- 3.1 灰度拉伸定義
- 3.2 灰度拉伸編程實例
- 4.直方圖均衡化
- 4.1 直方圖均衡化定義
- 4.2 直方圖均衡化編程實例
- 5.直方圖匹配
- 5.1 直方圖匹配定義
- 5.2 直方圖匹配編程實例
- 6.附件鏈接
- 6.1 解決VS2022 中沒有.net4.0
- 6.1.1 解措施
1.灰度直方圖
??任何一幅圖像的直方圖都包括了可觀的信息,某些類型的圖像還可以由其直方圖完全描述。
1.1 灰度直方圖定義
??灰度直方圖是灰度的函數,描述的是圖像中具有該灰度級的像素的個數。如果用直角坐標系來表示,則它的橫坐標是灰度級,縱坐標是該灰度出現的概率(像素的個數)。
1.2 灰度直方圖編程實例
private void histogram_Click(object sender, EventArgs e)
{if (curBitmap != null){histForm histoGram = new histForm(curBitmap);histoGram.ShowDialog();histoGram.Close();}
}
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];//計數加1countPixel[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();}}
}
2.線性點運算
??灰度圖像的點運算可分為線性點運算和非線性點運算兩種。
2.1線性點運算定義
??線性點運算就是輸出灰度級與輸入灰度級呈線性關系的點運算。在這種情況下,灰度變換函數的形式為:
g(x, y)=pf(x,y)+L
??其中 f(x,y) 為輸入圖像在點 (x,y) 的灰度值, g(x,y) 為相應的輸出點的灰度值。顯然,如果P=1和L=0,g(x,y)就是f(x,y)的復制;如果P<1,輸出圖像的對比度將增大;如果P>1,則對比度將減少;如果P=1而L≠0,該操作僅使所有像素的灰度值上移或下移,其效果是使整個圖像在顯示時更暗或更亮;如果P為負值,暗區域將變亮,亮區域將變暗,該操作完成了圖像求補。
2.2 線性點運算編程實例
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 linearPOForm : Form{public linearPOForm(){InitializeComponent();}private void startLinear_Click(object sender, EventArgs e){//設置DialogResult屬性this.DialogResult = DialogResult.OK;}private void close_Click(object sender, EventArgs e){this.Close();}//設置兩個get訪問器public string GetScaling{get{//得到斜率return scaling.Text;}}public string GetOffset{get{//得到偏移量return offset.Text;}}}
}
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 Form1 : Form{public Form1(){InitializeComponent();}//文件名private string curFileName;//圖像對象private System.Drawing.Bitmap curBitmpap;/// <summary>/// 打開圖像文件/// </summary>private void open_Click(object sender, EventArgs e){//創建OpenFileDialogOpenFileDialog opnDlg = new OpenFileDialog();//為圖像選擇一個篩選器opnDlg.Filter = "所有圖像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +"*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" +"位圖(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +"矢量圖(*.wmf;*.eps;*.emf;...)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf";//設置對話框標題opnDlg.Title = "打開圖像文件";//啟用“幫助”按鈕opnDlg.ShowHelp = true;//如果結果為“打開”,選定文件if (opnDlg.ShowDialog() == DialogResult.OK){//讀取當前選中的文件名curFileName = opnDlg.FileName;//使用Image.FromFile創建圖像對象try{curBitmpap = (Bitmap)Image.FromFile(curFileName);}catch (Exception exp){MessageBox.Show(exp.Message);}}//對窗體進行重新繪制,這將強制執行paint事件處理程序Invalidate();}/// <summary>/// 在控件需要重新繪制時發生/// </summary>private void Form1_Paint(object sender, PaintEventArgs e){//獲取Graphics對象Graphics g = e.Graphics;if (curBitmpap != null){//使用DrawImage方法繪制圖像//160,20:顯示在主窗體內,圖像左上角的坐標//curBitmpap.Width, curBitmpap.Height圖像的寬度和高度g.DrawImage(curBitmpap, 160, 20, curBitmpap.Width, curBitmpap.Height);}}/// <summary>/// 關閉窗體 /// </summary>private void close_Click(object sender, EventArgs e){this.Close();}private void histogram_Click(object sender, EventArgs e){if (curBitmpap != null){//定義并實例化新窗體,并把圖像數據傳遞給它histForm histoGram = new histForm(curBitmpap);histoGram.ShowDialog();}}private void linearPO_Click(object sender, EventArgs e){if (curBitmpap!=null){//實例化linearPOForm窗體linearPOForm linearForm = new linearPOForm();//點擊確定按鈕if (linearForm.ShowDialog()==DialogResult.OK){Rectangle rect = new Rectangle(0, 0, curBitmpap.Width, curBitmpap.Height);System.Drawing.Imaging.BitmapData bmpData = curBitmpap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmpap.PixelFormat);IntPtr ptr = bmpData.Scan0;int bytes = curBitmpap.Width * curBitmpap.Height;byte[] grayValues = new byte[bytes];System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);int temp = 0;//得到斜率double a = Convert.ToDouble(linearForm.GetScaling);//得到偏移量double b = Convert.ToDouble(linearForm.GetOffset);for (int i = 0; i < bytes; i++){//根據公式計算線性點運算//加0.5表示四舍五入temp = (int)(a * grayValues[i] + b + 0.5);//灰度值限制在0~255之間//大于255,則為255;小于0則為0if (temp>255){grayValues[i] = 255;}else if (temp<0){grayValues[i] = 0;}else{grayValues[i] = (byte)temp;}}System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);curBitmpap.UnlockBits(bmpData);}Invalidate();}}}
}
3.全等級直方圖灰度拉伸
??灰度拉伸也屬于線性點運算的一種,也可以通過上一節的程序得到。但由于它在點運算的特殊性,所以把它單獨列出來進行介紹。
3.1 灰度拉伸定義
??如果一幅圖像的灰度值分布在全等級灰度范圍內,即在0~255之間,那么它更容易被區別確認出來。
??灰度拉伸,也稱對比度拉伸,是一種簡單的線性點運算。它擴展圖像的直方圖,使其充滿整個灰度等級范圍內。
??設f(x,y)為輸入圖像,它的最小灰度級A和最大灰度級B的定義為:
A=min[f(x,y) B=max[f(x,y)]
??我們的目標是按照公式
g(x, y)=pf(x,y)+L
?? 把A和B分別線性映射到0和255,因此,最終的圖像g(x,y)為:
3.2 灰度拉伸編程實例
private void stretch_Click(object sender, EventArgs e){Stretch(curBitmpap, out curBitmpap);Invalidate();}/// <summary>/// 全等級灰度拉伸 (圖像增強)/// </summary>/// <param name="srcBmp">原圖像</param>/// <param name="dstBmp">處理后圖像</param>/// <returns>處理成功 true 失敗 false</returns>public static bool Stretch(Bitmap srcBmp, out Bitmap dstBmp){if (srcBmp == null){dstBmp = null;return false;}double pR = 0.0;//斜率double pG = 0.0;//斜率double pB = 0.0;//斜率byte minGrayDegree = 255;byte maxGrayDegree = 0;byte minGrayDegreeR = 255;byte maxGrayDegreeR = 0;byte minGrayDegreeG = 255;byte maxGrayDegreeG = 0;byte minGrayDegreeB = 255;byte maxGrayDegreeB = 0;dstBmp = new Bitmap(srcBmp);Rectangle rt = new Rectangle(0, 0, dstBmp.Width, dstBmp.Height);BitmapData bmpData = dstBmp.LockBits(rt, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);unsafe{for (int i = 0; i < bmpData.Height; i++){byte* ptr = (byte*)bmpData.Scan0 + i * bmpData.Stride;for (int j = 0; j < bmpData.Width; j++){if (minGrayDegreeR > *(ptr + j * 3 + 2))minGrayDegreeR = *(ptr + j * 3 + 2);if (maxGrayDegreeR < *(ptr + j * 3 + 2))maxGrayDegreeR = *(ptr + j * 3 + 2);if (minGrayDegreeG > *(ptr + j * 3 + 1))minGrayDegreeG = *(ptr + j * 3 + 1);if (maxGrayDegreeG < *(ptr + j * 3 + 1))maxGrayDegreeG = *(ptr + j * 3 + 1);if (minGrayDegreeB > *(ptr + j * 3))minGrayDegreeB = *(ptr + j * 3);if (maxGrayDegreeB < *(ptr + j * 3))maxGrayDegreeB = *(ptr + j * 3);}}pR = 255.0 / (maxGrayDegreeR - minGrayDegreeR);pG = 255.0 / (maxGrayDegreeG - minGrayDegreeG);pB = 255.0 / (maxGrayDegreeB - minGrayDegreeB);for (int i = 0; i < bmpData.Height; i++){byte* ptr1 = (byte*)bmpData.Scan0 + i * bmpData.Stride;for (int j = 0; j < bmpData.Width; j++){*(ptr1 + j * 3) = (byte)((*(ptr1 + j * 3) - minGrayDegreeB) * pB + 0.5);*(ptr1 + j * 3 + 1) = (byte)((*(ptr1 + j * 3 + 1) - minGrayDegreeG) * pG + 0.5);*(ptr1 + j * 3 + 2) = (byte)((*(ptr1 + j * 3 + 2) - minGrayDegreeR) * pR + 0.5);}}}dstBmp.UnlockBits(bmpData);return true;}
??增強后:
4.直方圖均衡化
4.1 直方圖均衡化定義
??直方圖均衡化,又稱直方圖修平,它是一種很重要的非線性點運算。該方法通常用來增加圖像的局部對比度,尤其是當圖像的有用數據的對比度相當接近的時候。通過這種方法,亮度可以更好地在直方圖上分布。
??直方圖均衡化的基本思想,是把原始圖像的直方圖變換為均勻分布的形式,這樣就增加了像素灰度值的動態范圍,從而可達到增強圖像整體對比度的效果。
4.2 直方圖均衡化編程實例
??為該控件添加Click事件,代碼如下:
private void equalization_Click(object sender, EventArgs e){if (curBitmap != null){Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);IntPtr ptr = bmpData.Scan0;int bytes = curBitmap.Width * curBitmap.Height;byte[] grayValues = new byte[bytes];System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);byte temp;int[] countPixel = new int[256];int[] tempArray = new int[256];//Array.Clear(tempArray, 0, 256);byte[] pixelMap = new byte[256];//計算各灰度級的像素個數for (int i = 0; i < bytes; i++){//灰度級temp = grayValues[i];//計數加1countPixel[temp]++;}for (int i = 0; i < 256; i++){if (i != 0){tempArray[i] = tempArray[i - 1] + countPixel[i];}else{tempArray[0] = countPixel[0];}pixelMap[i] = (byte)(255.0 * tempArray[i] / bytes + 0.5);}for (int i = 0; i < bytes; i++){temp = grayValues[i];grayValues[i] = pixelMap[temp];}System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);curBitmap.UnlockBits(bmpData);Invalidate();}}
5.直方圖匹配
5.1 直方圖匹配定義
??直方圖匹配,又稱直方圖規定化,即變換原圖的直方圖為規定的某種形式的直方圖,從而使兩幅圖像具有類似的色調和反差。直方圖匹配屬于非線性點運算。
??直方圖規定化的原理:對兩個直方圖都做均衡化,變成相同的歸一化的均勻直方圖,以此均勻直方圖為媒介,再對參考圖像做均衡化的逆運算。
5.2 直方圖匹配編程實例
private void shaping_Click(object sender, EventArgs e){if (curBitmap != null){shapingForm sForm = new shapingForm();if (sForm.ShowDialog() == DialogResult.OK){Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);IntPtr ptr = bmpData.Scan0;int bytes = curBitmap.Width * curBitmap.Height;byte[] grayValues = new byte[bytes];System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);byte temp = 0;double[] PPixel = new double[256];double[] QPixel = new double[256];int[] qPixel = new int[256];int[] tempArray = new int[256];for (int i = 0; i < grayValues.Length; i++){temp = grayValues[i];qPixel[temp]++;}for (int i = 0; i < 256; i++){if (i != 0){tempArray[i] = tempArray[i - 1] + qPixel[i];}else{tempArray[0] = qPixel[0];}QPixel[i] = (double)tempArray[i] / (double)bytes;}PPixel = sForm.ApplicationP;double diffA, diffB;byte k = 0;byte[] mapPixel = new byte[256];for (int i = 0; i < 256; i++){diffB = 1;for (int j = k; j < 256; j++){diffA = Math.Abs(QPixel[i] - PPixel[j]);if (diffA - diffB < 1.0E-08){diffB = diffA;k = (byte)j;}else{k = (byte)(j - 1);break;}}if (k == 255){for (int l = i; l < 256; l++){mapPixel[l] = k;}break;}mapPixel[i] = k;}for (int i = 0; i < bytes; i++){temp = grayValues[i];grayValues[i] = mapPixel[temp];}System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);curBitmap.UnlockBits(bmpData);}Invalidate();}}
6.附件鏈接
6.1 解決VS2022 中沒有.net4.0
我們在安裝了最新的Visual Studio 2022,在單個組件中沒有 .net4.0或者.net4.5的問題。
6.1.1 解措施
通過nuget 下載 4.0 安裝包
下載地址: .NETFramework,Version=v4.0 的引用程序集
得到安裝包.nupkg ,然后后綴名字,修改為.zip 解壓后
將v4.0 復制到 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework
直接替換文件
然后我們重啟 vs2022,問題解決。