python庫skimage 圖像直方圖均衡化爬骤、自適應均衡化充石、對比度拉伸實現(xiàn)

直方圖全局均衡化

from skimage import exposure
# Equalization
img_eq = exposure.equalize_hist(img)

直方圖自適應均衡化

# Adaptive Equalization
# 參數(shù)2:Clipping limit, normalized between 0 and 1 (higher values give more contrast).
img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)

直方圖對比度拉伸

# Contrast stretching
p2, p98 = np.percentile(img, (2, 98))
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))

實驗:直方圖全局均衡化、自適應均衡化霞玄、對比度拉伸效果對比

"""
======================
Histogram Equalization
======================

This examples enhances an image with low contrast, using a method called
*histogram equalization*, which "spreads out the most frequent intensity
values" in an image. The equalized image has a roughly linear cumulative
distribution function.

While histogram equalization has the advantage that it requires no parameters,
it sometimes yields unnatural looking images.  An alternative method is
*contrast stretching*, where the image is rescaled to include all intensities
that fall within the 2nd and 98th percentiles.

"""

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
    # 共用x軸
    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()

# Contrast stretching
p2, p98 = np.percentile(img, (2, 98))
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))

# Equalization
img_eq = exposure.equalize_hist(img)

# Adaptive Equalization
# 參數(shù)2:Clipping limit, normalized between 0 and 1 (higher values give more contrast).
img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)

# Display results
fig = plt.figure(figsize=(8, 5))
axes = np.zeros((2, 4), dtype=np.object)
axes[0, 0] = fig.add_subplot(2, 4, 1)
for i in range(1, 4):
    axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0])
for i in range(0, 4):
    axes[1, i] = fig.add_subplot(2, 4, 5+i)

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')
# 左側y軸范圍為0到y(tǒng)_max骤铃,5個刻度
ax_hist.set_yticks(np.linspace(0, y_max, 5))

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])
ax_img.set_title('Contrast stretching')

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])
ax_img.set_title('Histogram equalization')

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 3])
ax_img.set_title('Adaptive equalization')

ax_cdf.set_ylabel('Fraction of total intensity')
# 右側y軸范圍為0到1,5個刻度
ax_cdf.set_yticks(np.linspace(0, 1, 5))

# prevent overlap of y-axis labels
fig.tight_layout()
plt.show()
對比度增強效果對比圖
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市坷剧,隨后出現(xiàn)的幾起案子惰爬,更是在濱河造成了極大的恐慌,老刑警劉巖惫企,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件撕瞧,死亡現(xiàn)場離奇詭異,居然都是意外死亡狞尔,警方通過查閱死者的電腦和手機丛版,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來偏序,“玉大人页畦,你說我怎么就攤上這事⊙腥澹” “怎么了豫缨?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵独令,是天一觀的道長。 經(jīng)常有香客問我好芭,道長燃箭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任舍败,我火速辦了婚禮遍膜,結果婚禮上,老公的妹妹穿的比我還像新娘瓤湘。我一直安慰自己,他們只是感情好恩尾,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布弛说。 她就那樣靜靜地躺著,像睡著了一般翰意。 火紅的嫁衣襯著肌膚如雪木人。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天冀偶,我揣著相機與錄音醒第,去河邊找鬼。 笑死进鸠,一個胖子當著我的面吹牛稠曼,可吹牛的內容都是我干的。 我是一名探鬼主播客年,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼霞幅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了量瓜?” 一聲冷哼從身側響起司恳,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绍傲,沒想到半個月后扔傅,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡烫饼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年猎塞,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片杠纵。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡邢享,死狀恐怖,靈堂內的尸體忽然破棺而出淡诗,到底是詐尸還是另有隱情骇塘,我是刑警寧澤伊履,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站款违,受9級特大地震影響唐瀑,放射性物質發(fā)生泄漏。R本人自食惡果不足惜插爹,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一哄辣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赠尾,春花似錦力穗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至寸宵,卻和暖如春崖面,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背梯影。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工巫员, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人甲棍。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓简识,卻偏偏與公主長得像,于是被迫代替她去往敵國和親感猛。 傳聞我的和親對象是個殘疾皇子财异,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內容