{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline" ], "metadata": {} }, { "source": "
It can be useful to artificially tint an image with some color, either to\nhighlight particular regions of an image or maybe just to liven up a grayscale\nimage. This example demonstrates image-tinting by scaling RGB values and by\nadjusting colors in the HSV color-space.
\nIn 2D, color images are often represented in RGB---3 layers of 2D arrays, where\nthe 3 layers represent (R)ed, (G)reen and (B)lue channels of the image. The\nsimplest way of getting a tinted image is to set each RGB channel to the\ngrayscale image scaled by a different multiplier for each channel. For example,\nmultiplying the green and blue channels by 0 leaves only the red channel and\nproduces a bright red image. Similarly, zeroing-out the blue channel leaves\nonly the red and green channels, which combine to form yellow.
\nIn many cases, dealing with RGB values may not be ideal. Because of that, there\nare many other `color spaces`_ in which you can represent a color image. One\npopular color space is called HSV_, which represents hue (~the color),\nsaturation (~colorfulness), and value (~brightness). For example, a color\n(hue) might be green, but its saturation is how intense that green is---where\nolive is on the low end and neon on the high end.
\nIn some implementations, the hue in HSV goes from 0 to 360, since hues wrap\naround in a circle. In scikit-image, however, hues are float values from 0 to\n1, so that hue, saturation, and value all share the same scale.
\nBelow, we plot a linear gradient in the hue, with the saturation and value\nturned all the way up:
\n \nNotice how the colors at the far left and far right are the same. That reflects\nthe fact that the hues wrap around like the color wheel (see HSV_ for more\ninfo).
\nNow, let's create a little utility function to take an RGB image and:
\nNotice that we need to bump up the saturation; images with zero saturation are\ngrayscale, so we need to a non-zero value to actually see the color we've set.
\nUsing the function above, we plot six images with a linear gradient in the hue\nand a non-zero saturation:
\nYou can combine this tinting effect with numpy slicing and fancy-indexing to\nselectively tint your images. In the example below, we set the hue of some\nrectangles using slicing and scale the RGB values of some pixels found by\nthresholding. In practice, you might want to define a region for tinting based\non segmentation results or blob detection methods.
\nFor coloring multiple regions, you may also be interested in\nskimage.color.label2rgb.
\n