scikit-image库-- 图像伽马和对数对比度调整(二)

图像伽马和对数对比度调整:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

from skimage import data, img_as_float
from skimage import exposure

matplotlib.rcParams['font.size'] = 10


def plot_img_and_hist(image, axes, bins=256):
    """Plot an image along with its histogram and cumulative histogram.

    """
    image = img_as_float(image)  #将图像转为浮点数
    ax_img, ax_hist = axes   # axes为2行3列图块,第一行为ax_img ,第二行为ax_hist
    ax_cdf = ax_hist.twinx()       #twinx()函数表示共享x轴  twiny()表示共享y轴  共享表示的就是x轴使用同一刻度线

    # Display image
    ax_img.imshow(image, cmap=plt.cm.gray)
    '''
    Cmap是MATLAB里面用来设定和获取当前色图的函数,可以设置如下色图:
    hot 从黑平滑过度到红、橙色和黄色的背景色,然后到白色。
    cool 包含青绿色和品红色的阴影色。从青绿色平滑变化到品红色。
    gray 返回线性灰度色图。
    bone 具有较高的蓝色成分的灰度色图。该色图用于对灰度图添加电子的视图。
    white 全白的单色色图。 
    spring 包含品红和黄的阴影颜色。 
    summer 包含绿和黄的阴影颜色。
    autumn 从红色平滑变化到橙色,然后到黄色。 
    winter 包含蓝和绿的阴影色。
    '''
    ax_img.set_axis_off()  #axis off对当前坐标系关闭操作。

    # Display histogram
    ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black')  
    #bins 为直方图的柱数 image.ravel() 将矩阵拉伸成一维数组 
    # histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
    
    ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
    #设置y轴  
    #sytle: sci或scientific 科学计数法   plain:自然数
    #sclimits (m, n)  轴的值范围 10的m次方 to 10的n次方 (0,0)表示无限制
    ax_hist.set_xlabel('Pixel intensity') 
    #设置 x轴标签
    ax_hist.set_xlim(0, 1)
    #设置x轴范围0到1
    ax_hist.set_yticks([])
    #set_xticks 与 set_yticks 方法可以显示地设置标号的位置
    #ax.set_xticks([0.25, 0.5, 0.75])
    #ax.set_xticklabels(['a', 'b', 'c'], fontsize=18)

    # Display cumulative distribution
    img_cdf, bins = exposure.cumulative_distribution(image, bins)
    #返回给定图像的累积分布函数(cdf)  mg_cdf:数组累积分布函数的值。bin_centers:数组中心。
    ax_cdf.plot(bins, img_cdf, 'r')
    ax_cdf.set_yticks([])

    return ax_img, ax_hist, ax_cdf


# Load an example image
img = data.moon()

# Gamma
gamma_corrected = exposure.adjust_gamma(img, 2)
#skimage.exposure.adjust_gamma(image, gamma=1, gain=1)
#也称为幂律变换。该函数在将out = I**gamma每个像素缩放到范围0到1之后根据等式按照像素方式转换输入图像。
# Logarithmic
logarithmic_corrected = exposure.adjust_log(img, 1)
#skimage.exposure.adjust_log(image, gain=1, inv=False)
#out= gain*log(1 + I)在将每个像素缩放到0到1范围之后,该函数按照公式将输入图像按像素方式进行变换。


# Display results
fig = plt.figure(figsize=(8, 5))
#画个8*5大小的画布

axes = np.zeros((2, 3), dtype=np.object)
axes[0, 0] = plt.subplot(2, 3, 1) #将画布分为2*3等分,第一份为axes[0, 0],依次内推,
axes[0, 1] = plt.subplot(2, 3, 2, sharex=axes[0, 0], sharey=axes[0, 0])
axes[0, 2] = plt.subplot(2, 3, 3, sharex=axes[0, 0], sharey=axes[0, 0])
axes[1, 0] = plt.subplot(2, 3, 4)
axes[1, 1] = plt.subplot(2, 3, 5)
axes[1, 2] = plt.subplot(2, 3, 6)

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
ax_img.set_title('Low contrast image')

y_min, y_max = ax_hist.get_ylim()
ax_hist.set_ylabel('Number of pixels')
ax_hist.set_yticks(np.linspace(0, y_max, 5)) #y轴且分为5等分,标记等分数值

ax_img, ax_hist, ax_cdf = plot_img_and_hist(gamma_corrected, axes[:, 1])
ax_img.set_title('Gamma correction')

ax_img, ax_hist, ax_cdf = plot_img_and_hist(logarithmic_corrected, axes[:, 2])
ax_img.set_title('Logarithmic correction')

ax_cdf.set_ylabel('Fraction of total intensity')
ax_cdf.set_yticks(np.linspace(0, 1, 5))

# prevent overlap of y-axis labels
fig.tight_layout()        #tight_layout会自动调整子图参数,使之填充整个图像区域  
plt.show()
原图:

在这里插入图片描述

cmap=plt.cm.gray 改为hot:

在这里插入图片描述

设置ax.set_xticks([0.25, 0.5, 0.75]) ax.set_xticklabels([‘a’, ‘b’, ‘c’], fontsize=18)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值