• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

《OpenCv视觉之眼》Python图像处理四 :Opencv图像灰度处理的四种方法及原理

人工智能 陈一月的编程岁月 1601次浏览 0个评论

本专栏主要介绍如果通过OpenCv-Python进行图像处理,通过原理理解OpenCv-Python的函数处理原型,在具体情况中,针对不同的图像进行不同等级的、不同方法的处理,以达到对图像进行去噪、锐化等一系列的操作。同时,希望观看本专栏的小伙伴可以理解到OpenCv进行图像处理的强大哦,如有转载,请注明出处(原文链接和作者署名),感谢各位小伙伴啦!

  前文参考:   《OpenCv视觉之眼》Python图像处理一 :Opencv-python的简介及Python环境搭建 《OpenCv视觉之眼》Python图像处理二 :Opencv图像读取、显示、保存基本函数原型及使用 《OpenCv视觉之眼》Python图像处理三 :Opencv图像属性、ROI区域获取及通道处理   上次博客我们讲解了图像属性获取图像ROI兴趣区域获取以及图像3通道的获取及原理理解,特别是对于图像3通道的原理的理解,是接下来学习OpneCV的重要基础,只有理解B\G\R三通道如何进行像素比例混合进行颜色显示,才能进行后面通过原理,书写代码,完成相关OpenCV函数功能的目的!   本次博客,我们将介绍OpenCV中对图像灰度化处理的函数、以及它实现的原理,通过原理,自己书写代码,完成OpenCV函数库中对于灰度函数的功能;除此之外,林君学长还将给大家介绍几种程序世界中,常用到的另外几种图像灰度处理的原理,并且编写相关Python代码进行对应理解,一起学习吧!

[Python图像处理四 ]:Opencv图像灰度处理的四种方法及原理

 

  • 一、基于OpenCV库函数的图像灰度化
  • 二、最大值灰度化
    • 1、最大值灰度化原理
    • 2、最大值灰度化实现
  • 三、平均值灰度化
    • 1、平均值灰度化原理
    • 2、平均值灰度化实现
  • 四、Gamma校正灰度化
    • 1、Gamma校正简介
    • 2、Gamma校正灰度化原理
    • 3、Gamma校正灰度化实现

  图像灰度处理是OpenCV图像处理中非常重要的关键一步,例如提取图像轮廓,识别图像中需要的位置(这些后面都会讲),都会经历图像灰度处理的重要一步,下面我们会接触到图像灰度处理的各种方法,既然做图像灰度处理,首先,我们得了解图像灰度处理的基本原理,因此,首先,林君学长将带大家介绍灰度处理的基本原理。  

  • 图像灰度处理:将彩色图像转化成为灰度图像的过程成为图像的灰度化处理。彩色图像中的每个像素的颜色有R、G、B三个分量决定,而每个分量有255中值可取,这样一个像素点可以有1600多(255255255)的颜色的变化范围。而灰度图像是R、G、B三个分量相同的一种特殊的彩色图像,其一个像素点的变化范围为255种,所以在数字图像处理种一般先将各种格式的图像转变成灰度图像以使后续的图像的计算量变得少一些。灰度图像的描述与彩色图像一样仍然反映了整幅图像的整体和局部的色度和亮度等级的分布和特征。
  • 简单概括: 将3通道图像的R、G、B通道的像素从不相等设置为相等的过程,这个过程称为图像灰度处理,而一般的图像灰度处理的方法有以下四种,也就是本次博客讲解得四种,如下所示:

  接下来,一起学习吧!  

一、基于OpenCV库函数的图像灰度化

  在OpenCV对图像的处理之中,提供了图像处理的灰度化函数cv2.cvtColor(),该函数包括了很多功能,对图像灰度化只是其中的一种,其他功能本次博客暂不介绍,后面会用到,本次博客只介绍该函数的图像灰度处理功能。  

1、cv2.cvtColor()函数灰度处理

  1)、函数原型:img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  

  • 参数img:需要灰度处理的图像
  • 参数cv2.COLOR_BGR2GRAY:该参数为固定参数,对图像进行灰度处理固定用该参数,OpenCV读取图像通过BGR空间进行读取,因此,需要通过BGR转为GRAY进行灰度化。

  2)、cv2.cvtColor()函数使用,使用方法  

<code class="prism language-python has-numbering"><span class="token comment">#导入图像处理库opencv</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
gray<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span><span class="token comment">#BGR转换为灰度</span>
img<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span><span class="token comment">#BGR转换为RGB显示格式</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>  
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'灰度处理'</span><span class="token punctuation">)</span>    
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述  

2、cv2.cvtColor()灰度处理原理

  1)、OpenCv提供的函数进行灰度处理的原理是根据YUV的颜色空间中,Y的分量的物理意义是点的亮度,由该值反映亮度等级,根据RGB和YUV颜色空间的变化关系可建立亮度Y与R、G、B三个颜色分量的对应:Y=0.3R+0.59G+0.11B,以这个亮度值表达图像的灰度值。   2)、根据以上原理,我们便可以自己编写函数,通过原理实现Y亮度灰度图像处理,参考如下代码:  

<code class="prism language-python has-numbering"><span class="token comment">#图像灰度处理   YUV亮度灰度处理 opencv函数处理</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
<span class="token comment">#获取图像尺寸</span>
h<span class="token punctuation">,</span>w<span class="token operator">=</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token comment">#自定义空白单通道图像,用于存放灰度图</span>
gray<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
<span class="token comment">#对原图像进行遍历,然后分别对B\G\R按比例灰度化</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        gray<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token number">0.11</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">0.59</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">0.3</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment">#Y=0.3R+0.59G+0.11B </span>
gray<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span><span class="token comment">#BGR转换为RGB显示格式,方便通过matplotlib进行图像显示</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>gray<span class="token punctuation">)</span>  
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Y-亮度:灰度处理'</span><span class="token punctuation">)</span>    
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述   编写该函数其实很简单,只需要通过原理进行理解就好,接下来,林君学长带大家介绍其它几种图像灰度处理!

二、最大值灰度化

 

1、最大值灰度化原理

  最大值灰度化原理:最大值灰度化原理其实就是以B、G、R通道中最大的像素作为整体像素,公式为:B=G=R=max([B,G,R])  

2、最大值灰度化实现

 

<code class="prism language-python has-numbering"><span class="token comment">#图像灰度处理   最大值灰度</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
<span class="token comment">#获取图像尺寸</span>
h<span class="token punctuation">,</span>w<span class="token operator">=</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token comment">#自定义空白单通道图像,用于存放灰度图</span>
gray<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
<span class="token comment">#对原图像进行遍历,然后分别对B\G\R按比例灰度化</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        gray<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token builtin">max</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment">#求3通道中最大的值</span>
gray<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span><span class="token comment">#BGR转换为RGB显示格式,方便通过matplotlib进行图像显示</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>gray<span class="token punctuation">)</span>  
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'最大值灰度'</span><span class="token punctuation">)</span>    
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述  

三、平均值灰度化

 

1、平均值灰度化原理

  平均值灰度化原理: 平均值灰度化原理就对B、G、R三通道像素求取平均值作为灰度值,公式为:gray=(B+G+R)/3  

2、平均值灰度化实现

 

<code class="prism language-python has-numbering"><span class="token comment">#图像灰度处理   平均灰度处理</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
<span class="token comment">#获取图像尺寸</span>
h<span class="token punctuation">,</span>w<span class="token operator">=</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token comment">#自定义空白单通道图像,用于存放灰度图</span>
gray<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
<span class="token comment">#对原图像进行遍历,然后分别对B\G\R按比例灰度化</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        gray<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">3</span> <span class="token comment">#求3通道像素的平均值作为灰度值</span>
gray<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span><span class="token comment">#BGR转换为RGB显示格式,方便通过matplotlib进行图像显示</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>gray<span class="token punctuation">)</span>  
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'平均值灰度化'</span><span class="token punctuation">)</span>    
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述  

四、Gamma校正灰度化

 

1、Gamma校正简介

  1)、RGB值与功率并非简单的线性关系,而是幂函数关系,这个函数的指数称为Gamma值,一般为2.2,而这个换算过程,称为Gamma校正。   2)、为什么显示器要Gamma校正呢?因为人眼对亮度的感知和物理功率不成正比,而是幂函数的关系,这个函数的指数通常为2.2,称为Gamma值。   3)、打个比方,功率为50%的灰色,人眼实际感知亮度为:
《OpenCv视觉之眼》Python图像处理四 :Opencv图像灰度处理的四种方法及原理   而人眼认为的50%中灰色,实际功率为:
《OpenCv视觉之眼》Python图像处理四 :Opencv图像灰度处理的四种方法及原理   所以RGB中的灰度值,为了考虑到较小的存储范围(0~255)和较平衡的亮暗部比例,所以需要进行Gamma校正,而不是直接对应功率值,因此RGB值RGB颜色值不能简单直接相加,而是必须用2.2次方换算成物理光功率后才能进行下一步计算。这一点在下面的灰度计算公式中就有所体现。  

2、Gamma校正灰度化原理

  1)、原理如下计算公式:  
《OpenCv视觉之眼》Python图像处理四 :Opencv图像灰度处理的四种方法及原理   2)、注意这里的2.2次方和2.2次方根,RGB颜色值不能简单直接相加,而是必须用2.2次方换算成物理光功率。因为RGB值与功率并非简单的线性关系,而是幂函数关系,这个函数的指数称为Gamma值,一般为2.2,而这个换算过程,称为Gamma校正。  

3、Gamma校正灰度化实现

 

<code class="prism language-python has-numbering"><span class="token comment">#Gamma校正灰度处理</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
<span class="token comment">#获取图像尺寸</span>
h<span class="token punctuation">,</span>w<span class="token operator">=</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
<span class="token comment">#自定义空白单通道图像,用于存放灰度图</span>
gray<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span>
<span class="token comment">#对原图像进行遍历,然后分别对B\G\R按比例灰度化</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        a<span class="token operator">=</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1.5</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">0.6</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span> <span class="token comment">#分子</span>
        b<span class="token operator">=</span><span class="token number">1</span><span class="token operator">+</span><span class="token number">1.5</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">0.6</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span> <span class="token comment">#分母</span>
        gray<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token builtin">pow</span><span class="token punctuation">(</span>a<span class="token operator">/</span>b<span class="token punctuation">,</span><span class="token number">1.0</span><span class="token operator">/</span><span class="token number">2.2</span><span class="token punctuation">)</span>  <span class="token comment">#开2.2次方根</span>
gray<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span><span class="token comment">#BGR转换为RGB显示格式,方便通过matplotlib进行图像显示</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>gray<span class="token punctuation">)</span>  
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Gamma校正灰度化'</span><span class="token punctuation">)</span>    
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述 2)、一般来说,Gamma校正灰度化才是最完美的灰度化,因此,是最适合人眼观察的灰度化,但在OpenCV图像处理中,用得较多的是Y亮度灰度化,至于具体原因,林君学长暂未可知,可能是Y亮度灰度化比较大众,而且已经满足实际图像处理中的需求分析了吧! 最后进行代码整合,附上一张以上灰度处理的对比图,不同的灰度处理一眼便知:  

<code class="prism language-python has-numbering"><span class="token comment">#整体显示</span>
<span class="token keyword">import</span> cv2
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
plt<span class="token punctuation">.</span>rcParams<span class="token punctuation">[</span><span class="token string">'font.sans-serif'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'SimHei'</span><span class="token punctuation">]</span> <span class="token comment">#显示中文</span>
<span class="token comment">#读取第一张图像</span>
img<span class="token operator">=</span>cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">"my.jpg"</span><span class="token punctuation">)</span>
<span class="token comment">#获取图像尺寸</span>
h<span class="token punctuation">,</span>w<span class="token operator">=</span>img<span class="token punctuation">.</span>shape<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span>
gray1<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span><span class="token comment">#BGR转换为灰度显示格式   #库函数,Y亮度</span>
<span class="token comment">#自定义空白单通道图像,用于存放灰度图</span>
gray2<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span><span class="token comment">#最大值</span>
gray3<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span><span class="token comment">#平均值</span>
gray4<span class="token operator">=</span>np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span>h<span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">,</span>dtype<span class="token operator">=</span>img<span class="token punctuation">.</span>dtype<span class="token punctuation">)</span><span class="token comment">#Gamma校正灰度处理</span>
<span class="token comment">#对原图像进行遍历,然后分别对B\G\R按比例灰度化</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        gray2<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token builtin">max</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">#最大值</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        gray3<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token builtin">int</span><span class="token punctuation">(</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">3</span> <span class="token comment">#平均值</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> j <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span>
        a<span class="token operator">=</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1.5</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">0.6</span><span class="token operator">*</span>img<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span> <span class="token comment">#分子</span>
        b<span class="token operator">=</span><span class="token number">1</span><span class="token operator">+</span><span class="token number">1.5</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">0.6</span><span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">2.2</span><span class="token punctuation">)</span> <span class="token comment">#分母</span>
        gray4<span class="token punctuation">[</span>i<span class="token punctuation">,</span>j<span class="token punctuation">]</span><span class="token operator">=</span><span class="token builtin">pow</span><span class="token punctuation">(</span>a<span class="token operator">/</span>b<span class="token punctuation">,</span><span class="token number">1.0</span><span class="token operator">/</span><span class="token number">2.2</span><span class="token punctuation">)</span>  <span class="token comment">#开2.2次方根  #Gamma校正灰度处理</span>
<span class="token comment">#BGR转换为RGB显示格式,方便通过matplotlib进行图像显示</span>
gray1<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray1<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray2<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray2<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray3<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray3<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray4<span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>gray4<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
<span class="token comment">#显示图像</span>
titles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'cv2.cvtColor()'</span><span class="token punctuation">,</span> <span class="token string">'最大值灰度化'</span><span class="token punctuation">,</span><span class="token string">'平均值灰度化'</span><span class="token punctuation">,</span><span class="token string">'Gamma校正灰度化'</span><span class="token punctuation">]</span>  <span class="token comment">#标题</span>
images <span class="token operator">=</span> <span class="token punctuation">[</span>gray1<span class="token punctuation">,</span> gray2<span class="token punctuation">,</span>gray3<span class="token punctuation">,</span>gray4<span class="token punctuation">]</span>   <span class="token comment">#图像对比显示</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    plt<span class="token punctuation">.</span>subplot<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>images<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>  
    plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span>titles<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>    
    plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span><span class="token comment">#关闭坐标轴  设置为on则表示开启坐标轴</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#显示图像
</span>
</code>

 
在这里插入图片描述 以上就是本次博客的全部内容,遇到问题的小伙伴记得留言评论,学长看到会为大家进行解答的,这个学长不太冷  

在青春的列车上,如果你要提前下车,请别推醒装睡的我,这样我可以沉睡到终点,假装不知道你已经离开 ——《从你的全世界路过》  

陈一月的又一天编程岁月^ _ ^


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明《OpenCv视觉之眼》Python图像处理四 :Opencv图像灰度处理的四种方法及原理
喜欢 (0)

您必须 登录 才能发表评论!

加载中……