Color

In color images, we have many different methods to store the underlying information. The most common method, the one that also provides the easiest computational structures for creating algorithms, is the RGB color space. In this method, an image representation contains at least three layers. For each pixel, we assess the combined information of the amounts of red, green, and blue necessary to achieve the desired color and intensity at the corresponding location. The first layer indicates the intensities of underlying reds. The second and third layers indicate, respectively, the intensities of greens and blues:

In [12]: from skimage.data import coffee
In [13]: coffee().shape
Out[13]: (400, 600, 3)
In [14]: coffee()
Out[14]:
array([[[ 21,  13,   8],
        [ 21,  13,   9],
        [ 20,  11,   8],
        ...,
        [228, 182, 138],
        [231, 185, 142],
        [228, 184, 140]],

       ...,

       [[197, 141, 100],
        [195, 137,  99],
        [193, 138,  98],
        ...,
        [158,  73,  38],
        [144,  64,  30],
        [143,  60,  29]]], dtype=uint8)
Color

This photograph, taken by Rachel Michetti, is courtesy of Pikolo Espresso Bar.

To collect the data corresponding to each of the layers, we issue simple slicing operations:

In [15]: plt.figure(); 
   ....: plt.subplot(131); 
   ....: plt.imshow(coffee()[:,:,0], cmap=plt.cm.Reds); 
   ....: plt.subplot(132); 
   ....: plt.imshow(coffee()[:,:,1], cmap=plt.cm.Greens); 
   ....: plt.subplot(133); 
   ....: plt.imshow(coffee()[:,:,2], cmap=plt.cm.Blues); 
   ....: plt.show()
Color

All functions in the libraries of the SciPy stack described in this chapter assume that any color image is represented in this scheme. There are other color schemes, which are designed to address other fundamental questions and properties of images. A process to transform among most of the frequent color spaces is available in the toolkit scikit-image, as the function convert-colorspace in the submodule skimage.color. For example, consider the Hue-Saturation-Value (HSV) color space. This is a cylindrical-coordinate representation of points from an RGB color space, where the angle around the central vertical axis corresponds to hue (H) and the distance from the axis corresponds to saturation (S). The height corresponds to a third value (V), the system's representation of the perceived luminance (think brightness of the underlying combination of colors) in relation to the saturation:

In [16]: from skimage.color import convert_colorspace
In [17]: convert_colorspace(coffee(), 'RGB', 'HSV')
Out[17]:
array([[[ 0.06410256,  0.61904762,  0.08235294],
        [ 0.05555556,  0.57142857,  0.08235294],
        [ 0.04166667,  0.6       ,  0.07843137],
        ...,
        [ 0.08148148,  0.39473684,  0.89411765],
        [ 0.08052434,  0.38528139,  0.90588235],
        [ 0.08333333,  0.38596491,  0.89411765]],

       ...,
  
       [[ 0.07044674,  0.49238579,  0.77254902],
        [ 0.06597222,  0.49230769,  0.76470588],
        [ 0.07017544,  0.49222798,  0.75686275],
        ...,
        [ 0.04861111,  0.75949367,  0.61960784],
        [ 0.0497076 ,  0.79166667,  0.56470588],
        [ 0.04532164,  0.7972028 ,  0.56078431]]])

Tip

Among the other color spaces available are the CIE XYZ method of measuring tristimulus values, or the CIE-LUB and CIE-LAB color spaces. The best resource to learn how to access and use them in the SciPy stack environment is the documentation of the module skimage.color in the pages of their official documentation at http://scikit-image.org/docs/stable/api/skimage.color.html.

It is also possible to produce a gray-scale version of any color image provided in the RGB color space, by adding the three layers with appropriate weights. In the skimage.color module, we have, for instance, the functions rgb2gray or rgb2grey, that employ the formula output = 0.2125*red + 0.7154*green + 0.0721*blue.

Alpha channels

In either gray-scale or color images, we sometimes indicate an extra layer that gives us information about the opacity of each pixel. This is referred to as the alpha channel. Traditionally, we incorporate this property of our images as an additional layer of an RGB—the so-called RGBA color space. In that case, images represented by this scheme have four layers instead of three:

In [18]: from skimage.data import horse
In [19]: horse().shape
Out[19]: (328, 400, 4)
In [20]: horse()
Out[20]:
array([[[255, 255, 255, 110],
        [255, 255, 255, 217],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 217],
        [255, 255, 255, 110]],

       ...,

       [[255, 255, 255, 110],
        [255, 255, 255, 217],
        [255, 255, 255, 255],
        ...,
        [255, 255, 255, 255],
        [255, 255, 255, 217],
        [255, 255, 255, 110]]], dtype=uint8)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.123.155