OpenCvSharp image grayscale histogram (BGR, HSV, Lab multi-channel and single-channel grayscale histogram
)
Project Overview
The project realizes BGR, HSV, Lab multi-channel and single-channel grayscale histograms based on OpenCvSharp, including the conversion of HSV and Lab, and uses the chart control to draw histograms. The opened pictures can be processed and saved to Local Disk.
Source code is at the bottom of this article
1 Basic steps and interface design
1.1 Reference using OpenCvSharp; using OpenCvSharp.Extensions;
1.2 Layout, rename, adjust shape and font of Picturebox, Label, Button and other controls to form the following interface:
2 function realization
2.1 Initialize variables
Mat Img1 = new Mat();//用 Mat类定义图片1
Mat Img2 = new Mat();//用 Mat类定义图片2
Mat ImgCvt = new Mat();
Bitmap bitmap;//Bitmap类定义picturebox2要显示的图片
string pathname;//定义图片打开路径
2.2 Chart parameter settings
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 or Lab conversion and three-channel histogram
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 Single channel method
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);
}
**
Source code: https://download.csdn.net/download/sunsoldeir1/87254081
**