• C#绘制数字图像灰度直方图


    灰度直方图是灰度的函数,描述的是图像中具有该灰度级的像素的个数。如果用直角坐标系来表示,则它的横坐标是灰度级,纵坐标是该灰度出现的概率(像素的个数)。

    灰度直方图的分布函数:

    其中,K是指第k个灰度级,如果是8位灰度图像,k=0、1、……、255。

     

    处理图像生成直方图数据

                //将图像数据复制到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);
    

    绘制直方图信息

                //画出坐标系
                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();
    

    全局变量定义及赋值

            private System.Drawing.Bitmap bmpHist;
            private int[] countPixel;
            private int maxPixel;
    
            public Grey_ScaleMapForm(Bitmap bmp)
            {
                InitializeComponent();
                bmpHist = bmp;
                countPixel = new int[256];
            }
    

    下载DEMO

  • 相关阅读:
    HackingLab-再加密一次你就得到key啦~
    操作系统理论细节 2
    操作系统理论细节 1
    各种调度算法均衡利弊
    Github实现代码高亮
    Oracle 生成一张测试表并插入随机的个人基本信息数据
    一,Spring Boot 入门
    关于IDEA2020年1月新版MAVEN无法自动导入依赖
    操作系统存储管理--虚拟内存地址和物理内存地址
    apache2.4 tomcat 出现502 Proxy Error错误
  • 原文地址:https://www.cnblogs.com/zxlovenet/p/3535712.html
Copyright © 2020-2023  润新知