9.OpenCvSharp图像灰度直方图(BGR、HSV、Lab多通道及单通道的灰度直方图)——c#OpenCvSharp学习笔记

OpenCvSharp图像灰度直方图(BGR、HSV、Lab多通道及单通道的灰度直方图

在这里插入图片描述
在这里插入图片描述

项目概述

项目实现了基于OpenCvSharp实现了BGR、HSV、Lab多通道及单通道的灰度直方图,包含了HSV和Lab的转换,及使用chart控件绘制直方图,可将打开后的图片处理后并保存到本地磁盘。

源代码在本文底部

1基础步骤和界面设计

1.1引用using OpenCvSharp;using OpenCvSharp.Extensions;
1.2将Picturebox、Label、Button等控件进行布局、改名、调整形状和字体,形成如下界面:
在这里插入图片描述

2功能实现

2.1初始化变量

        Mat Img1 = new Mat();//用 Mat类定义图片1
        Mat Img2 = new Mat();//用 Mat类定义图片2
        Mat ImgCvt = new Mat();
        Bitmap bitmap;//Bitmap类定义picturebox2要显示的图片
        string pathname;//定义图片打开路径

2.2图表参数设置

 private void chart_load()//设置图表参数
        {
    
    
            chart1.Series.Clear();
            chart1.ChartAreas[0].AxisX.Title = "灰度";
            chart1.ChartAreas[0].AxisX.TitleFont =new Font("宋体",15f) ;
            chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Red;
            chart1.ChartAreas[0].AxisX.TitleForeColor = System.Drawing.Color.Crimson;
            chart1.ChartAreas[0].AxisY.Title = "个数";
            chart1.ChartAreas[0].AxisY.TitleFont = new Font("宋体", 15f);
            chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue;
            chart1.ChartAreas[0].AxisY.TitleForeColor = System.Drawing.Color.Crimson;
            chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Horizontal;
         }

2.3 HSV或Lab转换及三通道的直方图

private void button20_Click(object sender, EventArgs e)
        {
    
    
            if (pictureBox1.Image == null)//判断图片是否已打开
            {
    
    
                MessageBox.Show("没有打开图片");
                return;
            }
            Mat src = Cv2.ImRead(pathname);//读取路径下的图片

            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2HSV);//

          

            bitmap = BitmapConverter.ToBitmap(src); //把Mat格式的图片转换成Bitmap
            pictureBox2.Image = bitmap;


            chart1.Series.Clear();//清空cShart

            Series H = new Series("H");//H通道
            Series S = new Series("S");//S通道
            Series V= new Series("V");//V通道

            Mat[] picturechannel = new Mat[3];
            Cv2.Split(src, out picturechannel);
            int[] histSize = {
    
     256 };
            Rangef[] histRange = {
    
     new Rangef(0, 256) }; //the upper boundary is exclusive               

            bool uniform = true, accumulate = false;
            Mat channel1 = new Mat();
            Mat channel2 = new Mat();
            Mat channel3 = new Mat();

            Cv2.CalcHist(picturechannel, new int[] {
    
     0 }, null, channel1, 1, histSize, histRange, uniform, accumulate);
            Cv2.CalcHist(picturechannel, new int[] {
    
     1 }, null, channel2, 1, histSize, histRange, uniform, accumulate);
            Cv2.CalcHist(picturechannel, new int[] {
    
     2 }, null, channel3, 1, histSize, histRange, uniform, accumulate);



            for (int i = 1; i < histSize[0]; i++)
            {
    
    


                H.Points.AddXY(i, Math.Round(channel1.At<float>(i - 1)));
                S.Points.AddXY(i, Math.Round(channel2.At<float>(i - 1)));
                V.Points.AddXY(i, Math.Round(channel3.At<float>(i - 1)));

            }

            //绘制曲线
            chart1.Series.Add(H);
            chart1.Series.Add(S);
            chart1.Series.Add(V);

        }

2.4单通道法

 private void button7_Click(object sender, EventArgs e)
        {
    
    

            if (pictureBox1.Image == null)//判断图片是否已打开
            {
    
    
                MessageBox.Show("没有打开图片");
                return;
            }
            Mat src = Cv2.ImRead(pathname);//读取路径下的图片

            bitmap = BitmapConverter.ToBitmap(src); //把Mat格式的图片转换成Bitmap
            Mat dst = new Mat(src.Size(), MatType.CV_8U);
   
            int height = src.Rows;
            int width = src.Cols;
            for (int row = 0; row < height; row++)
            {
    
    
                for (int col = 0; col < width; col++)
                {
    
    
                    dst.At<byte>(row, col) = (byte)src.At<Vec3b>(row, col)[0];
                }
            }
            bitmap = BitmapConverter.ToBitmap(dst); //把Mat格式的图片转换成Bitmap
            pictureBox2.Image = bitmap;


            chart1.Series.Clear();//清空chart

            Series B = new Series("B");//B通道
            B.Color=Color.Gray;

            Mat[] picturechannel = new Mat[3];
            Cv2.Split(src, out picturechannel);
            int[] histSize = {
    
     256 };
            Rangef[] histRange = {
    
     new Rangef(0, 256) }; //the upper boundary is exclusive               

            bool uniform = true, accumulate = false;
            Mat channel1 = new Mat();
       

            Cv2.CalcHist(picturechannel, new int[] {
    
     0 }, null, channel1, 1, histSize, histRange, uniform, accumulate);
   



            for (int i = 1; i < histSize[0]; i++)
            {
    
    


                B.Points.AddXY(i, Math.Round(channel1.At<float>(i - 1)));
              
            }

            //绘制曲线
            chart1.Series.Add(B);
       
        }

**

源代码:https://download.csdn.net/download/sunsoldeir1/87254081

**

猜你喜欢

转载自blog.csdn.net/sunsoldeir1/article/details/128222533
今日推荐