博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python图像对比度拉伸_python库skimage 图像直方图均衡化、自适应均衡化、对比度拉伸实现...
阅读量:6268 次
发布时间:2019-06-22

本文共 3356 字,大约阅读时间需要 11 分钟。

直方图全局均衡化

from skimage import exposure

# Equalization

img_eq = exposure.equalize_hist(img)

直方图自适应均衡化

# Adaptive Equalization

# 参数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

# 参数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_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()

d49c76755902

对比度增强效果对比图

转载地址:http://oxspa.baihongyu.com/

你可能感兴趣的文章
XMPP协议、IM、客户端互联详解
查看>>
PHP写文件函数
查看>>
mysql的sql_mode合理设置
查看>>
函数连续性与可导性
查看>>
linux下libevent安装
查看>>
用ip来获得用户所在地区信息
查看>>
卡尔曼滤波
查看>>
linux下面覆盖文件,如何实现直接覆盖,不提示
查看>>
CSS3阴影 box-shadow的使用和技巧总结
查看>>
Linux下高cpu解决方案
查看>>
SQL事务用法begin tran,commit tran和rollback tran的用法
查看>>
centos7 crontab笔记
查看>>
.Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");
查看>>
【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D
查看>>
Android Mina框架的学习笔记
查看>>
合并两个排序的链表
查看>>
rtf格式的一些说明,转载的
查看>>
REST Security with JWT using Java and Spring Security
查看>>
echarts学习总结(二):一个页面存在多个echarts图形,图形自适应窗口大小
查看>>
IIS7显示ASP的详细错误信息到浏览器
查看>>