日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

【转】c#数字图像处理(三)灰度直方图

發布時間:2023/12/10 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】c#数字图像处理(三)灰度直方图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:https://www.cnblogs.com/dearzhoubi/p/8621804.html

灰度直方圖是灰度的函數,描述的是圖像中具有該灰度級的像素的個數。如果用直角坐標系來表示,則它的橫坐標是灰度級,縱坐標是該灰度出現的概率(像素的個數)。

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();}} }

總結

以上是生活随笔為你收集整理的【转】c#数字图像处理(三)灰度直方图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。