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

函数

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()

实验结果

原文地址:https://www.cnblogs.com/wojianxin/p/12642859.html

时间: 2024-11-01 23:56:50

python库skimage 实现图像直方图全局均衡化、局部均衡化的相关文章

python库skimage 将针对灰度图像的滤波器用于RGB图像 逐通道滤波;转换为HSV图像滤波

有许多滤波器设计用于灰度图像但是不能用于彩色图像.为了简化创建函数,使其能够用于RGB图像,scikit-image图像处理库提供了adapt_rgb装饰器. 实际使用adapt_rgb装饰器,你必须决定如何调整RGB图像以使灰度滤波器能够用于RGB图像.有两个预定义的处理方式: "每个通道": 传输RGB的每个通道给滤波器,处理后,将它们按照rgb顺序整合到RGB图像. "hsv_value": 转换RGB图像到HSV图像并传输明度通道的值给滤波器.滤波的结果被插

python库skimage 应用canny边缘探测算法

Canny算法 请参考:Canny算法python手动实现 请参考:Canny边缘检测算法原理及opencv实现 skimage库中函数 skimage.feature.canny(image, sigma=1.0, low_threshold=None, high_threshold=None, mask=None, use_quantiles=False) sigma:高斯滤波器的标准差 low_threshold:Canny算法最后一步中,小于该阈值的像素直接置为0 high_thresh

python库skimage 绘制二值图像的凸壳(convex hull)

二值图像的凸壳指的是包围输入二值图像白色区域的最小的凸多边形的像素集合. skimage中的函数 from skimage.morphology import convex_hull_image chull = convex_hull_image(image) 完整代码: """ =========== Convex Hull =========== The convex hull of a binary image is the set of pixels included

Python实现图像直方图均衡化算法

title: "Python实现图像直方图均衡化算法" date: 2018-06-12T17:10:48+08:00 tags: [""] categories: ["python"] 效果图 代码 #!/usr/bin/env python3 # coding=utf-8 import matplotlib.image as mpimg from matplotlib import pyplot as plt import sys impor

图像直方图与直方图均衡化

图像直方图与直方图均衡化 图像直方图以及灰度与彩色图像的直方图均衡化 图像直方图: 概述: 图像的直方图用来表征该图像像素值的分布情况.用一定数目的小区间(bin)来指定表征像素值的范围,每个小区间会得到落入该小区间表示范围的像素数目. 图像直方图图形化显示不同的像素值在不同的强度值上的出现频率,对于灰度图像来说强度范围为[0~255]之间,对于RGB的彩色图像可以独立显示三种颜色的图像直方图. 同时直方图是用来寻找灰度图像二值化阈值常用而且是有效的手段之一,如果一幅灰度图像的直方图显示为两个波

图像直方图均衡化增强opencv与C语言版

本文实现彩色图像的全局直方图均衡.分别对R/G/B三通道均衡,读写图片采用OpenCV.代码如下: #include <opencv2/opencv.hpp> //#include <cv.h> //#include <cxcore.h> //#include <highgui.h> #include <time.h> #include <stdio.h> #include <math.h> #include "

opencv图像直方图均衡化及其原理

直方图均衡化是什么有什么用 先说什么是直方图均衡化,通俗的说,以灰度图为例,原图的某一个像素为x,经过某个函数变为y.形成新的图.新的图的灰度值的分布是均匀的,这个过程就叫直方图均衡化. 图像直方图均衡化作用:用来增强对比度. 这种方法通常用来增加许多图像的全局对比度,尤其是当图像的有用数据的对比度相当接近的时候.通过这种方法,亮度可以更好地在直方图上分布.这样就可以用于增强局部的对比度而不影响整体的对比度,直方图均衡化通过有效地扩展常用的亮度来实现这种功能. 这种方法对于背景和前景都太亮或者太

opencv python:图像直方图 histogram

直接用matplotlib画出直方图 def plot_demo(image): plt.hist(image.ravel(), 256, [0, 256]) # image.ravel()将图像展开,256为bins数量,[0, 256]为范围 plt.show() 图像直方图 def image_hist(image): color = ('blue', 'green', 'red') for i, color in enumerate(color): # 计算出直方图,calcHist(i

图像直方图均衡化(C#)

关于图像直方图均衡化的原理和步骤先不作讨论,我就看看代码吧. private Bitmap picequalization(Bitmap basemap, int width, int height) { Bitmap retmap = new Bitmap(basemap, width, height); int size = width * height; int[] gray = new int[256]; double[] graydense = new double[256]; for