本帖最后由 Marco 于 2016-10-6 16:02 编辑
在这篇文章中,主要想与大家一起分享一下增强图像对比度的方法。LeadTools中提供了很多种增强图像对比度的方法,这里只介绍了其中的几种,包括直方图均衡,局部直方图均衡,lightControl,以及mathematical Logic。其中直方图均衡是图像处理中最为常见的一种增强图像对比度的算法,这里不再进行介绍。根据官方文档的描述,局部直方图是将图像分成局部的矩形部分,对每一部分做直方图均衡;LightControl是对原图的像素值进行重新的测量与分配,这样的图像预处理对于二维码的识别很有效;mathematical Logic是对图像的颜色值乘以一定的系数,从而达到增强的效果。本例对这四种方法分别做出了demo,具体步骤如下: 1.建立Windows 窗体应用程序,并命名为Enhance Image Contrast 2.Form1_Load事件中添加的代码与之前教程相同,这里不再描述 3.打开窗体设计页面,拖入一个Image Viewer 控件 4.拖入菜单控件,并建立如下选项。其中”File”用于用户选择需要处理的目标图片,”Function”用于用户选择不同的方法 5.在”Function”选项中建立如下四种子选项分别命名为”HistogramEqualize”,”Local HistogramEqualize”,” Light Control” 以及 ” MathematicalLogic”
6.在Form1类中定义如下变量 - RasterImage Image;
- RasterCodecs codecs;
- bool result;
- bool back;
- int count = 0;
复制代码7.在Form1类中添加如下方法 - //判断是否用户已选择目标图片
- private bool checking()
- {
- if (imageViewer1.Image == null)
- {
- MessageBox.Show("Please load the Image");
- return false;
- }
-
- else
- {
- return true;
- }
- }
- //判断是否需要重新选择图片
- private bool checkingcount()
- {
- if (count>=1)
- {
- MessageBox.Show("Please load the Image agian");
- imageViewer1.Image = null;
- count = 0;
- return true;
- }
- else
- {
- return false;
- }
- }
复制代码8. 切换回窗体设计页面,双击”File”,在代码页面中会出现click事件,在事件中添加如下代码 - imageViewer1.Image = null;
- // 显示打开文件对话框
- OpenFileDialog dlg = new OpenFileDialog();
- dlg.Filter = "All Files|*.*";
- if (dlg.ShowDialog(this) == DialogResult.OK)
- {
- try
- {
- // 载入图像
- Image = codecs.Load(dlg.FileName);
- imageViewer1.Image = Image; //iamgeViewer1控件显示原始图像
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, ex.Message);
- }
- }
复制代码9.切换回窗体设计页面分别双击四个子选项,在代码页面会出现四个click事件,分别在事件中添加如下代码: 直方图均衡: - private void histogramEqualizeToolStripMenuItem_Click(object sender, EventArgs e)
- {
- result = checking();
- if (result == false)
- {
- return;
- }
- back= checkingcount();
- if (back==true)
- {
- return;
- }
- count++;
- // Prepare the command
- HistogramEqualizeCommand command = new HistogramEqualizeCommand();
- // Histogram equalize the image.
- command.Type = HistogramEqualizeType.Yuv;
- command.Run(Image);
-
- imageViewer1.Image = Image;
- }
复制代码局部直方图均衡: - private void histogramEqualizeAlgorithmToolStripMenuItem_Click(object sender, EventArgs e)
- {
- result = checking();
- if (result == false)
- {
- return;
- }
- back = checkingcount();
- if (back == true)
- {
- return;
- }
- count++;
-
- // Prepare the command
- LocalHistogramEqualizeCommand command = new LocalHistogramEqualizeCommand();
- command.Width = 15; //局部矩形参数,可根据需要调整
- command.Height = 15; //局部矩形参数,可根据需要调整
- command.WidthExtension = 20; //局部矩形参数,可根据需要调整
- command.HeightExtension = 20; //局部矩形参数,可根据需要调整
- command.Smooth = 0;
- command.Type = HistogramEqualizeType.Yuv;
- // Local Histogram equalize the image.
- command.Run(Image);
- imageViewer1.Image = Image;
-
-
- }
复制代码LightControl: - private void lightControlToolStripMenuItem_Click(object sender, EventArgs e)
- {
- result = checking();
- if (result == false)
- {
- return;
- }
- back = checkingcount();
- if (back == true)
- {
- return;
- }
- count++;
-
- // Prepare the command
- int[] LowerAverage = new int[3];
- int[] Average = new int[3];
- int[] UpperAverage = new int[3];
- LowerAverage[0] = 20; //for blue, gray or yuv //***********************
- LowerAverage[1] = 20; //for green
- LowerAverage[2] = 20; //for red
- Average[0] = 80; //for blue, gray or yuv
- Average[1] = 80; //for green //像素平均值参数,可根据需要调整
- Average[2] = 80; //for red
- UpperAverage[0] = 150; //for blue, gray or yuv
- UpperAverage[1] = 150; //for green
- UpperAverage[2] = 150; //for red //****************************
- LightControlCommand command = new LightControlCommand(LowerAverage, Average, UpperAverage, LightControlCommandType.Yuv);
- // change the lightness of the image.
- command.Run(Image);
-
- imageViewer1.Image = Image;
- }
复制代码Mathematical Logic: - private void mathematicalLogicToolStripMenuItem_Click(object sender, EventArgs e)
- {
- result = checking();
- if (result == false)
- {
- return;
- }
- back = checkingcount();
- if (back == true)
- {
- return;
- }
- count++;
-
-
- // Prepare the command
- ApplyMathematicalLogicCommand command = new ApplyMathematicalLogicCommand();
- command.Factor = 300; //相乘系数,可根据需要调整
- command.Flags = ApplyMathematicalLogicCommandFlags.OperationMultiply | ApplyMathematicalLogicCommandFlags.ValueDoNothing | ApplyMathematicalLogicCommandFlags.ResultDoNothing;
- // Multiply the colors of the bitmap using a factor of 1.51 .
- command.Run(Image);
- imageViewer1.Image = Image;
- }
复制代码 10.生成解决方案并运行程序,结果如下:原图:
直方图均衡:
局部直方图均衡:
Light Control:
Mathematical Logic:
LeadTools为我们提供的增强图像对比度的方法还是非常强大的,大家可以根据自己的需求选择不同的方法。更多详细情况可以查看官方文档中Image Processing——Changing Brightness and Contrast,链接如下:https://www.leadtools.com/help/leadtools/v19/dh/to/webframe.html?platform=dotnet
另附程序Demo,有兴趣可以下载尝试:
|