Visualizing the true-color palette

By performing the following steps, you will be able to visualize the true-color palette of a color image:

  1. Let's have a look at a particular image:
In [1]: import cv2
... import numpy as np
... lena = cv2.imread('data/lena.jpg', cv2.IMREAD_COLOR)
  1. By now, we know how to start Matplotlib in our sleep:
In [2]: import matplotlib.pyplot as plt
... %matplotlib inline
... plt.style.use('ggplot')
  1. However, this time, we want to disable the grid lines that the ggplot option typically displays over images:
In [3]: plt.rc('axes', **{'grid': False})
  1. Then, we can visualize Lena with the following command (don't forget to switch the BGR ordering of the color channels to RGB):
In [4]: plt.imshow(cv2.cvtColor(lena, cv2.COLOR_BGR2RGB))

This will produce the following output:

  1. The image is stored in a 3D array of the size (height x width x depth) containing BGR (blue, green, and red) contributions as integers from 0 to 255:
In [5]: lena.shape
Out[5]: (225, 225, 3)
  1. Since every color channel has 256 potential values, the number of possible colors can be 256 x 256 x 256 or 16, 777, 216. One of the ways to visualize the huge amount of different colors in the image is to reshape the data to a 3D color space. We also need to scale the colors to lie between 0 and 1:
In [6]: img_data = lena / 255.0
... img_data = img_data.reshape((-1, 3))
... img_data.shape
Out[6]: (50625, 3)
  1. In this 3D color space, every row of data is a data point. To visualize this data, we will write a function called plot_pixels, which takes as input the data matrix and a figure title. Optionally, we also let the user specify the colors to use. For the sake of efficiency, we can also limit the analysis to a subset of N pixels:
In [7]: def plot_pixels(data, title, colors=None, N=10000):
... if colors is None:
... colors = data
...
  1. If a number, N, is specified, we will randomly choose N data points from the list of all possible ones:
...         rng = np.random.RandomState(0)
... i = rng.permutation(data.shape[0])[:N]
... colors = colors[i]
  1. Because each data point has three dimensions, the plotting is a bit tricky. We could either create a 3D plot using Matplotlib's mplot3d toolkit, or we could produce two plots that visualize a subspace of the 3D point cloud. In this example, we will opt for the latter and generate two plots, one where we plot red versus green and another where we plot red versus blue. For this, we need to access the R, G, and B values of each data point as column vectors:
...         pixel = data[i].T
... R, G, B = pixel[0], pixel[1], pixel[2]

  1. The first plot then shows red on the x-axis and green on the y-axis:
...         fig, ax = plt.subplots(1, 2, figsize=(16, 6))
... ax[0].scatter(R, G, color=colors, marker='.')
... ax[0].set(xlabel='Red', ylabel='Green', xlim=(0, 1),
... ylim=(0, 1))
  1. Analogously, the second figure is a scatter plot of red on the x-axis and blue on the y-axis:
...         ax[1].scatter(R, B, color=colors, marker='.')
... ax[1].set(xlabel='Red', ylabel='Blue', xlim=(0, 1),
... ylim=(0, 1))
  1. As the last step, we will display the specified title centered over the two subplots:
...         fig.suptitle(title, size=20);
  1. We can then call the function with our data matrix (data) and an appropriate title:
In [8]: plot_pixels(img_data, title='Input color space: 16 million '
... 'possible colors')

This will produce the following output:

Let's see what more we can do with this algorithm.

..................Content has been hidden....................

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