Gamma校正
Gamma校正是對輸入圖像灰度值進(jìn)行的非線性操作,使輸出圖像灰度值與輸入圖像灰度值呈指數(shù)關(guān)系:
這個指數(shù)即為Gamma。
Gamma校正的原理很簡單昔馋,就一個很簡單的表達(dá)式沦寂,如下圖所示:
伽馬校正公式
其中V_in的取值范圍是0~1谐丢,最重要的參數(shù)就是公式中的γ參數(shù)杨伙!
γ的值決定了輸入圖像和輸出圖像之間的灰度映射方式其监,即決定了是增強(qiáng)低灰度值區(qū)域還是增高灰度值區(qū)域。
γ>1時限匣,圖像的高灰度區(qū)域?qū)Ρ榷鹊玫皆鰪?qiáng)抖苦。
γ<1時,圖像的低灰度區(qū)域?qū)Ρ榷鹊玫皆鰪?qiáng)米死。
γ=1時锌历,不改變原圖像。
伽馬變換對于圖像對比度偏低峦筒,并且整體亮度值偏高(對于于相機(jī)過曝)情況下的圖像增強(qiáng)效果明顯究西。
對數(shù)log變換
log 函數(shù)的表達(dá)式:
y=alog(1+x), a 是一個放大系數(shù),x 同樣是輸入的像素值物喷,取值范圍為 [0?1], y 是輸出的像素值卤材。
對數(shù)變換對于整體對比度偏低并且灰度值偏低的圖像增強(qiáng)效果較好。
skimage庫實(shí)現(xiàn)gamam校正和log校正
函數(shù):
Gamma:
gamma_corrected = exposure.adjust_gamma(img, 2)
Logarithmic:
logarithmic_corrected = exposure.adjust_log(img, 1)
"""
=================================
Gamma and log contrast adjustment
=================================
This example adjusts image contrast by performing a Gamma and a Logarithmic
correction on the input 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'] = 8
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
ax_cdf = ax_hist.twinx()
# Display image
ax_img.imshow(image, cmap=plt.cm.gray)
ax_img.set_axis_off()
# Display histogram
ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black')
ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
ax_hist.set_xlabel('Pixel intensity')
ax_hist.set_xlim(0, 1)
ax_hist.set_yticks([])
# Display cumulative distribution
img_cdf, bins = exposure.cumulative_distribution(image, bins)
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)
# Logarithmic
logarithmic_corrected = exposure.adjust_log(img, 1)
# Display results
fig = plt.figure(figsize=(8, 5))
axes = np.zeros((2, 3), dtype=np.object)
axes[0, 0] = plt.subplot(2, 3, 1)
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))
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()
plt.show()
實(shí)驗(yàn)結(jié)果
實(shí)驗(yàn)結(jié)果