zoukankan      html  css  js  c++  java
  • 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
     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
     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
     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.
    

    代码输出
    代码输出

  • 相关阅读:
    poj 3590 The shuffle Problem——DP+置换
    poj 3128 Leonardo's Notebook——思路(置换)
    bzoj 1004 [HNOI2008]Cards && poj 2409 Let it Bead ——置换群
    bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路(置换)
    bzoj 3944 Sum —— 杜教筛
    bzoj 1367 [ Baltic 2004 ] sequence —— 左偏树
    bzoj 2093 [ Poi 2010 ] Frog —— 滑动窗口 + 倍增
    bzoj 2276 [ Poi 2011 ] Temperature —— 单调队列
    bzoj 2069 [ POI 2004 ] ZAW —— 多起点最短路 + 二进制划分
    NOIP2007普及 守望者的逃离
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12642045.html
Copyright © 2011-2022 走看看