C#绘制数字图像灰度直方图
灰度直方图是灰度的函数,描述的是图像中具有该灰度级的像素的个数。如果用直角坐标系来表示,则它的横坐标是灰度级,纵坐标是该灰度出现的概率(像素的个数)。
灰度直方图的分布函数:
其中,K是指第k个灰度级,如果是8位灰度图像,k=0、1、……、255。
处理图像生成直方图数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //将图像数据复制到byte中 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 * 3; byte [] grayValues = new byte [bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); //统计直方图信息 byte temp = 0; maxPixel = 0; Array.Clear(countPixel, 0, 256); for ( int i = 0; i < bytes; i++) { temp = grayValues[i]; countPixel[temp]++; if (countPixel[temp] > maxPixel) { maxPixel = countPixel[temp]; } } System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes); bmpHist.UnlockBits(bmpdata); |
绘制直方图信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //画出坐标系 Graphics g = e.Graphics; 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(); |
全局变量定义及赋值
1 2 3 4 5 6 7 8 9 10 | private System.Drawing.Bitmap bmpHist; private int [] countPixel; private int maxPixel; public Grey_ScaleMapForm(Bitmap bmp) { InitializeComponent(); bmpHist = bmp; countPixel = new int [256]; } |
下载:DEMO
作者: 初行
Q Q: 121866673
来源: http://zxlovenet.cnblogs.com
声明: 本文原创发表于博客园,作者为初行本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
消息:此博客已停止更新,想了解最新博客更新请关注我的新博客 Noogel's notes
推荐: 推荐使用为知笔记(WizNote),它是电脑、手机、平板上都能用的云笔记软件,还可以分类管理和共享资料,使用我的邀请码注册:https://note.wiz.cn/i/06102d9e
打赏: 如果您觉得文章对您的工作有帮助,请小额打赏我一些,鼓励我写出更好的文章!
Q Q: 121866673
来源: http://zxlovenet.cnblogs.com
声明: 本文原创发表于博客园,作者为初行本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
消息:此博客已停止更新,想了解最新博客更新请关注我的新博客 Noogel's notes
推荐: 推荐使用为知笔记(WizNote),它是电脑、手机、平板上都能用的云笔记软件,还可以分类管理和共享资料,使用我的邀请码注册:https://note.wiz.cn/i/06102d9e
打赏: 如果您觉得文章对您的工作有帮助,请小额打赏我一些,鼓励我写出更好的文章!

微信打赏

支付宝打赏
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库