From 379d8b1fb828918f8d77b0acf0e270eb94e650e5 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 20 Jul 2014 14:00:12 -0500 Subject: [PATCH 1/3] Add example for `adapt_rgb` --- doc/examples/plot_adapt_rgb.py | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 doc/examples/plot_adapt_rgb.py diff --git a/doc/examples/plot_adapt_rgb.py b/doc/examples/plot_adapt_rgb.py new file mode 100644 index 00000000..330e0e2e --- /dev/null +++ b/doc/examples/plot_adapt_rgb.py @@ -0,0 +1,107 @@ +""" +========================================= +Adapting gray-scale filters to RGB images +========================================= + +There are many filters that are designed work with gray-scale images but not +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 filter + +@adapt_rgb(each_channel) +def sobel_each(image): + return filter.sobel(image) + +@adapt_rgb(hsv_value) +def sobel_hsv(image): + return filter.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 +import matplotlib.pyplot as plt + +image = data.lena() + +fig, (ax_each, ax_hsv) = plt.subplots(ncols=2) +ax_each.imshow(sobel_each(image)) +ax_hsv.imshow(sobel_hsv(image)) + +""" +.. image:: PLOT2RST.current_figure + +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 filter.sobel(image) + +fig, ax = plt.subplots() +ax.imshow(sobel_gray(image)) + +plt.show() + +""" +.. image:: PLOT2RST.current_figure + +.. 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. + +""" From c109978c6148f11a9e283315ec9581d5ca5be6c9 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 2 Nov 2014 09:52:20 -0600 Subject: [PATCH 2/3] Set colormap to grayscale --- doc/examples/plot_adapt_rgb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/plot_adapt_rgb.py b/doc/examples/plot_adapt_rgb.py index 330e0e2e..35b86d29 100644 --- a/doc/examples/plot_adapt_rgb.py +++ b/doc/examples/plot_adapt_rgb.py @@ -91,7 +91,7 @@ def sobel_gray(image): return filter.sobel(image) fig, ax = plt.subplots() -ax.imshow(sobel_gray(image)) +ax.imshow(sobel_gray(image), cmap=plt.cm.gray) plt.show() From 56e4283df9f8ba521d8dded8b4a745afaf394ca7 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Tue, 11 Nov 2014 23:03:50 -0600 Subject: [PATCH 3/3] Update import to match rename of `filters` module --- doc/examples/plot_adapt_rgb.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/doc/examples/plot_adapt_rgb.py b/doc/examples/plot_adapt_rgb.py index 35b86d29..fce93245 100644 --- a/doc/examples/plot_adapt_rgb.py +++ b/doc/examples/plot_adapt_rgb.py @@ -23,15 +23,17 @@ 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 filter +from skimage import filters + @adapt_rgb(each_channel) def sobel_each(image): - return filter.sobel(image) + return filters.sobel(image) + @adapt_rgb(hsv_value) def sobel_hsv(image): - return filter.sobel(image) + return filters.sobel(image) """ We can use these functions as we would normally use them, but now they work @@ -74,6 +76,7 @@ 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) @@ -86,9 +89,10 @@ 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 filter.sobel(image) + return filters.sobel(image) fig, ax = plt.subplots() ax.imshow(sobel_gray(image), cmap=plt.cm.gray)