python图像局部特点_python库skimage 实现图像直方图全局均衡化、局部均衡化

该博客介绍如何使用Python的skimage库实现图像的全局和局部均衡化。通过示例展示了低对比度图像如何经过全局均衡化和局部均衡化处理,以增强其对比度。实验中应用了`exposure.equalize_hist`进行全局均衡化和`rank.equalize`进行局部均衡化,使用disk结构元素来定义局部区域。
摘要由CSDN通过智能技术生成

函数

from skimage import exposure

from skimage.morphology import disk

from skimage.filters import rank

# Global equalize

img_rescale = exposure.equalize_hist(img)

# Local Equalization

selem = disk(30)

img_eq = rank.equalize(img, selem=selem)

实验:低对比度图像全局均衡化和局部均衡化

"""

============================

Local Histogram Equalization

============================

This example enhances an image with low contrast, using a method called *local

histogram equalization*, which spreads out the most frequent intensity values

in an image.

The equalized image has a roughly linear cumulative distribution function

for each pixel neighborhood.

The local version of the histogram equalization emphasized every local

graylevel variations.

"""

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

from skimage import data

from skimage.util.dtype import dtype_range

from skimage.util import img_as_ubyte

from skimage import exposure

from skimage.morphology import disk

from skimage.filters import rank

matplotlib.rcParams[‘font.size‘] = 9

def plot_img_and_hist(image, axes, bins=256):

"""Plot an image along with its histogram and cumulative histogram.

"""

ax_img, ax_hist = axes

# Make and return a second axes that shares the x-axis.

# The new axes will overlay ax (or the current axes if ax is None), and its ticks will be on the right.

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)

# Change the ScalarFormatter used by default for linear axes.

ax_hist.ticklabel_format(axis=‘y‘, style=‘scientific‘, scilimits=(0, 0))

ax_hist.set_xlabel(‘Pixel intensity‘)

xmin, xmax = dtype_range[image.dtype.type]

ax_hist.set_xlim(xmin, xmax)

# Display cumulative distribution

img_cdf, bins = exposure.cumulative_distribution(image, bins)

ax_cdf.plot(bins, img_cdf, ‘r‘)

return ax_img, ax_hist, ax_cdf

# Load an example image

img = img_as_ubyte(data.moon())

# Global equalize

img_rescale = exposure.equalize_hist(img)

# Equalization

selem = disk(30)

img_eq = rank.equalize(img, selem=selem)

# 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‘)

ax_hist.set_ylabel(‘Number of pixels‘)

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])

ax_img.set_title(‘Global equalise‘)

ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])

ax_img.set_title(‘Local equalize‘)

ax_cdf.set_ylabel(‘Fraction of total intensity‘)

# prevent overlap of y-axis labels

fig.tight_layout()

plt.show()

实验结果

098c322718dcb134e4faa0aafb429b97.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值