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

有许多滤波器设计用于灰度图像但是不能用于彩色图像。为了简化创建函数,使其能够用于RGB图像,scikit-image图像处理库提供了adapt_rgb装饰器。

实际使用adapt_rgb装饰器,你必须决定如何调整RGB图像以使灰度滤波器能够用于RGB图像。有两个预定义的处理方式:

“每个通道”:

传输RGB的每个通道给滤波器,处理后,将它们按照rgb顺序整合到RGB图像。

“hsv_value”:

转换RGB图像到HSV图像并传输明度通道的值给滤波器。滤波的结果被插回到HSV图像的明度通道,然后HSV图像转换为RGB图像。

我们发现,value-filtered 的图像保存了原始图像的颜色。但是在图像平滑中,逐通道滤波将会产生一个比hsv_value滤波更好的结果。

"""
=========================================
Adapting gray-scale filters to RGB images
=========================================

There are many filters that are designed to work with gray-scale images but not
with color images. To simplify the process of creating functions that can adapt
to RGB images, scikit-image provides the ``adapt_rgb`` decorator.

To actually use the ``adapt_rgb`` decorator, you have to decide how you want to
adapt the RGB image for use with the gray-scale filter. There are two
pre-defined handlers:

``each_channel``
    Pass each of the RGB channels to the filter one-by-one, and stitch the
    results back into an RGB image.
``hsv_value``
    Convert the RGB image to HSV and pass the value channel to the filter.
    The filtered result is inserted back into the HSV image and converted
    back to RGB.

Below, we demonstrate the use of ``adapt_rgb`` on a couple of gray-scale
filters:
"""
from skimage.color.adapt_rgb import adapt_rgb, each_channel, hsv_value
from skimage import filters

@adapt_rgb(each_channel)
def sobel_each(image):
    return filters.sobel(image)

@adapt_rgb(hsv_value)
def sobel_hsv(image):
    return filters.sobel(image)

######################################################################
# We can use these functions as we would normally use them, but now they work
# with both gray-scale and color images. Let‘s plot the results with a color
# image:

from skimage import data
from skimage.exposure import rescale_intensity
import matplotlib.pyplot as plt

image = data.astronaut()

fig, (ax_each, ax_hsv) = plt.subplots(ncols=2, figsize=(14, 7))

# We use 1 - sobel_each(image) but this won‘t work if image is not normalized
ax_each.imshow(rescale_intensity(1 - sobel_each(image)))
ax_each.set_xticks([]), ax_each.set_yticks([])
ax_each.set_title("Sobel filter computed\n on individual RGB channels")

# We use 1 - sobel_hsv(image) but this won‘t work if image is not normalized
ax_hsv.imshow(rescale_intensity(1 - sobel_hsv(image)))
ax_hsv.set_xticks([]), ax_hsv.set_yticks([])
ax_hsv.set_title("Sobel filter computed\n on (V)alue converted image (HSV)")

######################################################################
# Notice that the result for the value-filtered image preserves the color of
# the original image, but channel filtered image combines in a more
# surprising way. In other common cases, smoothing for example, the channel
# filtered image will produce a better result than the value-filtered image.
#
# You can also create your own handler functions for ``adapt_rgb``. To do so,
# just create a function with the following signature::
#
#     def handler(image_filter, image, *args, **kwargs):
#         # Manipulate RGB image here...
#         image = image_filter(image, *args, **kwargs)
#         # Manipulate filtered image here...
#         return image
#
# Note that ``adapt_rgb`` handlers are written for filters where the image is
# the first argument.
#
# As a very simple example, we can just convert any RGB image to grayscale
# and then return the filtered result:

from skimage.color import rgb2gray

def as_gray(image_filter, image, *args, **kwargs):
    gray_image = rgb2gray(image)
    return image_filter(gray_image, *args, **kwargs)

######################################################################
# It‘s important to create a signature that uses ``*args`` and ``**kwargs``
# to pass arguments along to the filter so that the decorated function is
# allowed to have any number of positional and keyword arguments.
#
# Finally, we can use this handler with ``adapt_rgb`` just as before:

@adapt_rgb(as_gray)
def sobel_gray(image):
    return filters.sobel(image)

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(7, 7))

# We use 1 - sobel_gray(image) but this won‘t work if image is not normalized
ax.imshow(rescale_intensity(1 - sobel_gray(image)), cmap=plt.cm.gray)
ax.set_xticks([]), ax.set_yticks([])
ax.set_title("Sobel filter computed\n on the converted grayscale image")

plt.show()

######################################################################
#
# .. note::
#
#     A very simple check of the array shape is used for detecting RGB
#     images, so ``adapt_rgb`` is not recommended for functions that support
#     3D volumes or color images in non-RGB spaces.

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

时间: 2024-11-01 21:36:18

python库skimage 将针对灰度图像的滤波器用于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 实现图像直方图全局均衡化、局部均衡化

函数 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) 实验:低对比度图像全

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 库大全

作者:Lingfeng Ai链接:http://www.zhihu.com/question/24590883/answer/92420471来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Awesome Python中文版来啦! 本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:github.com.欢迎加入翻译组. 原文链接:Python 资源大全 1200+收藏,600+赞,别只顾着自己私藏呀朋友们 ------------

推荐一些相见恨晚的 Python 库 「一」

原创 2017-08-14 马超 DeveloperPython 扯淡 首先说明下,这篇文章篇幅过长并且大部分是链接,因此非常适合在电脑端打开访问. 本文内容摘自 Github 上有名的 Awesome Python.这是由 vinta 在 14 年发起并持续维护的一个项目. Awesome Python 涵盖了 Python 的方方面面,主要有 Web框架.网络爬虫.网络内容提取.模板引擎.数据库.图片处理.数据可视化.文本处理.自然语言处理.机器学习.日志.代码分析等.学会这些库,保证你在

11个并不广为人知,但值得了解的Python库

这是一篇译文,文中提及了一些不常见但是有用的Python库 原文地址:http://blog.yhathq.com/posts/11-python-libraries-you-might-not-know.html Python的库多如牛毛.再见多识广的人也无法知晓全部.光PyPi的网站上就列出了超过47000个Python库. 本文由博客园zer0black撰写/翻译,未经允许,禁止转载 近来,越来越多的数据科学家开始使用Python,我不由得想到,尽管他们从pandas.scikit-lea

Python程序员们使用频率最高的十五个Python库!

当下最火的,使用频率最高的十五个Python库!各位都有用过吗? Scrapy Scrapy库是用于从网络结构化检索数据,可以用来设计crawling程序. NumPy Pandas 库中有两个主要的数据结构: "系列"(Series),一维 "数据帧"(Data Frames),二维 例如,当您要从这两种类型的结构中接收到一个新的Dataframe时,通过传递一个Series,您将收到一个单独的行到DataFrame的DF: Matplotlib 只要付出一点你就

Python干货大派送!一千个Python库,只有你想不到,没有查不到!

环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. virtualenv – 创建独立 Python 环境的工具. virtualenvwrapper- virtualenv 的一组扩展. 包管理 管理包和依赖的工具. pip – Python 包和依赖关系管理工具. pip-tools – 保证 Python 包依赖关系更新的一组工具. conda

Python 库打包分发简易指南

Python 库打包分发(setup.py 编写)简易指南 Python 有非常丰富的第三方库可以使用,很多开发者会向 pypi 上提交自己的 Python 包.要想向 pypi 包仓库提交自己开发的包,首先要将自己的代码打包,才能上传分发. distutils 简介 distutils 是标准库中负责建立 Python 第三方库的安装器,使用它能够进行 Python 模块的安装和发布.distutils 对于简单的分发很有用,但功能缺少.大部分Python用户会使用更先进的setuptools