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

本项目利用OpenCvSharp实现BGR、HSV、Lab颜色空间的灰度直方图绘制,并提供了HSV和Lab颜色空间转换的功能。支持通过Chart控件展示不同颜色通道的直方图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

**

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值