文章目录

  • 一、直方图
  • 1. 原理描述
  • 2. 代码
  • 3. 结果
  • 二、直方图均衡化
  • 1. 原理描述
  • 2. 代码
  • 3. 结果
  • 三、高斯滤波
  • 1. 原理描述
  • 2. 代码
  • 3. 结果



一、直方图

1. 原理描述

直方图是可以对整幅图的灰度分布进行整体了解的图示,通过直方图我们可以对图像的对比度、亮度和灰度分布等有一个直观了解。
图像的直方图用来表征该图像像素值的分布情况。用一定数目的小区间(bin)来指定表征像素值的范围,每个小区间会得到落入该小区间表示范围的像素数目。该(灰度)图像的直方图可以使用hist()函数绘制。

2. 代码

from PIL import Image
from pylab import *

# 添加中文字体支持
from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)


'''
Parameters:
    path - 文件路径
Returns:
    无
'''
# 函数说明:绘制图像灰度直方图
def histogram(path):
    img = array(Image.open(path))
    img_gray = array(Image.open(path).convert('L'))

    figure()
    subplot(221)
    axis('off')
    title(u'原图像', fontproperties=font)
    imshow(img)

    subplot(222)
    axis('off')
    gray()
    title(u'灰度图像', fontproperties=font)
    imshow(img_gray)

    subplot(212)
    title(u'灰度直方图', fontproperties=font)
    hist(img_gray.flatten(), 128)

    show()

if __name__ == '__main__':
    histogram('Image_JMU.jpg')

3. 结果

python 图像熵 python图像算法_直方图

二、直方图均衡化

1. 原理描述

一副效果好的图像通常在直方图上的分布比较均匀,直方图均衡化就是用来改善图像的全局亮度和对比度。

直方图均衡化是指将一幅图像的灰度直方图变平,使变换后的图像中每个灰度值的分布概率都相同。在对图像做进一步处理之前,直方图均衡化通常是对图像灰度值进行归一化的一个非常好的方法,并且可以增强图像的对比度。

直方图均衡化的变换函数是图像中像素值的累积分布函数(cumulative distribution function,简写为 cdf,将像素值的范围映射到目标范围的归一化操作)。

2. 代码

from PIL import Image
from pylab import *

# 添加中文字体支持
from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)


'''
Parameters:
    img:灰度图像
    nbr_bins=256:直方图使用小区间的数目
Returns:
    img2.reshape(img.shape):直方图均衡化后的图像
    cdf:用来做像素值映射的累积分布函数
'''
# 函数说明:直方图均衡化
def histeq(img, nbr_bins=256):
    # 计算图像的直方图
    imhist, bins = histogram(img.flatten(), nbr_bins)
    # 累计分布函数
    cdf = imhist.cumsum()
    # 归一化
    cdf = 255 * cdf / cdf[-1]
    # 使用累积分布函数的线性插值,计算新的像素值
    img2 = interp(img.flatten(), bins[:-1], cdf)

    return img2.reshape(img.shape), cdf


if __name__ == '__main__':
    img = array(Image.open('Image_JMU.jpg').convert('L'))
    img2, cdf = histeq(img)
    figure()
    subplot(2, 2, 1)
    axis('off')
    gray()
    title(u'原始图像', fontproperties=font)
    imshow(img)

    subplot(2, 2, 2)
    axis('off')
    title(u'直方图均衡化后的图像', fontproperties=font)
    imshow(img2)

    subplot(2, 2, 3)
    # axis('off')
    title(u'原始直方图', fontproperties=font)
    hist(img.flatten(), 128)

    subplot(2, 2, 4)
    # axis('off')
    title(u'均衡化后的直方图', fontproperties=font)
    hist(img2.flatten(), 128)

    show()

3. 结果

python 图像熵 python图像算法_直方图_02

三、高斯滤波

1. 原理描述

高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。

通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。

python 图像熵 python图像算法_opencv_03

2. 代码

from PIL import Image
from pylab import *
from scipy.ndimage import gaussian_filter

# 添加中文字体支持
from matplotlib.font_manager import FontProperties

font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=12)

"""
Parameters:
    path - 文件路径
Returns:
    无
"""
# 函数说明:高斯滤波
def gaussian(path):
    img = array(Image.open(path).convert('L'))

    figure()
    gray()
    axis('off')
    subplot(1, 4, 1)
    axis('off')
    title(u'原图', fontproperties=font)
    imshow(img)

    for bi, blur in enumerate([2, 5, 10]):
        img2 = zeros(img.shape)
        img2 = gaussian_filter(img, blur)
        img2 = np.uint8(img2)
        imNum=str(blur)
        subplot(1, 4, 2 + bi)
        axis('off')
        title(u'标准差为'+imNum, fontproperties=font)
        imshow(img2)
    show()


if __name__ == '__main__':
    gaussian('Image_JMU.jpg')

3. 结果

python 图像熵 python图像算法_python_04