找回密码
 立即注册

QQ登录

只需一步,快速开始

Marco

中级会员

6

主题

11

帖子

832

积分

中级会员

积分
832
Marco
中级会员   /  发表于:2016-10-6 16:02  /   查看:3420  /  回复:1
本帖最后由 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类中定义如下变量
  1. RasterImage Image;
  2. RasterCodecs codecs;
  3.   bool result;
  4.    bool back;
  5.    int count = 0;
复制代码
7.在Form1类中添加如下方法
  1. //判断是否用户已选择目标图片
  2.         private bool checking()
  3.         {
  4.             if (imageViewer1.Image == null)
  5.             {
  6.                 MessageBox.Show("Please load the Image");
  7.                 return false;
  8.             }
  9.             
  10.             else
  11.             {
  12.                 return true;
  13.             }
  14.         }
  15.         //判断是否需要重新选择图片
  16.          private bool  checkingcount()
  17.          {
  18.              if (count>=1)
  19.              {
  20.                   MessageBox.Show("Please load the Image agian");
  21.                   imageViewer1.Image = null;
  22.                   count = 0;
  23.                  return true;
  24.              }
  25.              else
  26.              {
  27.                  return false;
  28.              }
  29.          }
复制代码
8. 切换回窗体设计页面,双击”File”,在代码页面中会出现click事件,在事件中添加如下代码
  1. imageViewer1.Image = null;
  2.             // 显示打开文件对话框
  3.             OpenFileDialog dlg = new OpenFileDialog();
  4.             dlg.Filter = "All Files|*.*";
  5.             if (dlg.ShowDialog(this) == DialogResult.OK)
  6.             {
  7.                 try
  8.                 {
  9.                     // 载入图像
  10.                     Image = codecs.Load(dlg.FileName);
  11.                     imageViewer1.Image = Image;    //iamgeViewer1控件显示原始图像



  12.                 }
  13.                 catch (Exception ex)
  14.                 {
  15.                     MessageBox.Show(this, ex.Message);
  16.                 }
  17.             }
复制代码
9.切换回窗体设计页面分别双击四个子选项,在代码页面会出现四个click事件,分别在事件中添加如下代码:
直方图均衡:
  1. private void histogramEqualizeToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             result = checking();
  4.             if (result == false)
  5.             {
  6.                 return;
  7.             }

  8.             back= checkingcount();
  9.             if (back==true)
  10.             {
  11.                 return;
  12.             }
  13.             count++;
  14.             // Prepare the command
  15.             HistogramEqualizeCommand command = new HistogramEqualizeCommand();
  16.             // Histogram equalize the image.
  17.             command.Type = HistogramEqualizeType.Yuv;
  18.             command.Run(Image);

  19.            
  20.             imageViewer1.Image = Image;
  21.         }
复制代码
局部直方图均衡:
  1.   private void histogramEqualizeAlgorithmToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             result = checking();
  4.             if (result == false)
  5.             {
  6.                 return;
  7.             }


  8.             back = checkingcount();
  9.             if (back == true)
  10.             {
  11.                 return;
  12.             }
  13.             count++;
  14.         

  15.                 // Prepare the command
  16.                 LocalHistogramEqualizeCommand command = new LocalHistogramEqualizeCommand();
  17.                 command.Width = 15;                     //局部矩形参数,可根据需要调整
  18.                 command.Height = 15;                  //局部矩形参数,可根据需要调整
  19.                 command.WidthExtension = 20;          //局部矩形参数,可根据需要调整
  20.                 command.HeightExtension = 20;         //局部矩形参数,可根据需要调整
  21.                 command.Smooth = 0;
  22.                 command.Type = HistogramEqualizeType.Yuv;
  23.                 // Local Histogram equalize the image.
  24.                 command.Run(Image);
  25.                 imageViewer1.Image = Image;
  26.             
  27.      
  28.         }
复制代码
LightControl:
  1. private void lightControlToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             result = checking();
  4.             if (result == false)
  5.             {
  6.                 return;
  7.             }

  8.             back = checkingcount();
  9.             if (back == true)
  10.             {
  11.                 return;
  12.             }
  13.             count++;
  14.            
  15.             // Prepare the command
  16.             int[] LowerAverage = new int[3];
  17.             int[] Average = new int[3];
  18.             int[] UpperAverage = new int[3];
  19.             LowerAverage[0] = 20;  //for blue, gray or yuv         //***********************
  20.             LowerAverage[1] = 20;  //for green                     
  21.             LowerAverage[2] = 20;   //for red                     
  22.             Average[0] = 80;       //for blue, gray or yuv      
  23.             Average[1] = 80;       //for green                              //像素平均值参数,可根据需要调整
  24.             Average[2] = 80;       //for red                  
  25.             UpperAverage[0] = 150;  //for blue, gray or yuv   
  26.             UpperAverage[1] = 150;  //for green               
  27.             UpperAverage[2] = 150;  //for red                         //****************************
  28.             LightControlCommand command = new LightControlCommand(LowerAverage, Average, UpperAverage, LightControlCommandType.Yuv);
  29.             // change the lightness of the image.
  30.             command.Run(Image);
  31.            
  32.             imageViewer1.Image = Image;
  33.         }
复制代码
Mathematical Logic:
  1. private void mathematicalLogicToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             result = checking();

  4.             if (result == false)
  5.             {
  6.                 return;
  7.             }
  8.             back = checkingcount();
  9.             if (back == true)
  10.             {
  11.                 return;
  12.             }
  13.             count++;
  14.            
  15.    
  16.             // Prepare the command
  17.          ApplyMathematicalLogicCommand command = new ApplyMathematicalLogicCommand();
  18.          command.Factor = 300;   //相乘系数,可根据需要调整
  19.          command.Flags = ApplyMathematicalLogicCommandFlags.OperationMultiply |               ApplyMathematicalLogicCommandFlags.ValueDoNothing | ApplyMathematicalLogicCommandFlags.ResultDoNothing;
  20.          // Multiply the colors of the bitmap using a factor of 1.51 .
  21.          command.Run(Image);
  22.          imageViewer1.Image = Image;
  23.         }
复制代码
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,有兴趣可以下载尝试:







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

评分

参与人数 1金币 +1000 收起 理由
gw0506 + 1000 很给力!

查看全部评分

1 个回复

倒序浏览
gw0506
超级版主   /  发表于:2016-10-8 15:32:29
沙发
俨然LeadTools图像处理专家的节奏啊~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部