The Complete Guide Basic Color Histograms OpenCV RaspberryPi

Color histograms for each Red, Green, and Blue channel of the beach image.

In this tutorial, I will show you the  Basic Color Histograms OpenCV RaspberryPi Step By step Complet Process.


≡ Histograms


 A histogram represents the number of pixel intensities in an image. It can be visualized as a graph (or plot) that gives a high-level intuition of the intensity (pixel value) number. We are going to find an RGB color space in this example, so these pixel values will be in the range of 0 to 255.

If plotting the histogram, the X-axis works as our “bins”. If we create a histogram with 256 bins when we are effectively counting the number of times each pixel value occurs. In contrast, if we work only 2 bins, then we are including the number of times a pixel is in the range [0, 128) or [128, 255]. The number of pixels binned to the x-axis value is then plotted on the y-axis.

By simply analyzing the histogram of an image, you get a general understanding regarding the contrast, brightness, and intensity distribution.


≡ OpenCV Histograms function


We will be applying the cv2.calcHist function to build our histograms. Before we get into any code examples, let’s quickly review the function:

cv2.calcHist(images, channels, mask, histSize, ranges)
  1. images: This is the image that we need to compute a histogram for. Wrap it as a list: [myImage].
  2. channels: This is a list of indexes, where we define the index of the channel we require to compute a histogram. To compute a histogram of a grayscale image, the list would be [0]. To compute a histogram for all three red, green, and blue channels, the channels list would be [0, 1, 2].
  3. mask:  Well, here we can provide a mask. If a mask is provided, a histogram will be computed for masked pixels only. If we do not have a mask or do not require to apply one, we can only provide a value of None.
  4. histSize: This is the number of bins we want to work when computing a histogram. Again, this is a list, one for each channel we are computing a histogram for. The bin sizes do not all have to be the same. Here is an example of 32 bins for each channel: [32, 32, 32].
  5. ranges: Here we specify The range of possible pixel values. Normally, this is [0, 256] for each channel, but if you are applying a color space other than RGB (such as HSV), the ranges might be different.

≡ Color Histograms OpenCV RaspberryPi


This theory is better explained through some code for writing code with raspberry pi, Fast open your command terminal on your Raspberry Pi then write this Commend:

nano

you can see like this interface

Now we can start writing This Color Histograms OpenCV RaspberryPi on GNU nano:

from __future__ import print_function
from matplotlib import pyplot as plt
import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
    help = "Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)

chans = cv2.split(image)
colors = ("b", "g", "r")
plt.figure()
plt.title("'Flattened' Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")

for (chan, color) in zip(chans, colors):
    # Create a histogram for the current channel and plot it
    hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
    plt.plot(hist, color = color)
    plt.xlim([0, 256])


fig = plt.figure()

ax = fig.add_subplot(131)
hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and B")
plt.colorbar(p)

ax = fig.add_subplot(132)
hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and R")
plt.colorbar(p)

ax = fig.add_subplot(133)
hist = cv2.calcHist([chans[0], chans[2]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for B and R")
plt.colorbar(p)

print("2D histogram shape: {}, with {} values".format(
    hist.shape, hist.flatten().shape[0]))

hist = cv2.calcHist([image], [0, 1, 2],
    None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
print("3D histogram shape: {}, with {} values".format(
    hist.shape, hist.flatten().shape[0]))

plt.show()

Now save this file named color_histograms.py and exit using ctrl + x, y, enter.  we simply open up a raspberry terminal window and execute the following command:

python color_histograms.py --image bay.jpg

see like this interface


≡ Analysis


from __future__ import print_function
from matplotlib import pyplot as plt
import numpy as np
import argparse
import cv2

In the tutorial, you’ll see importing the print_ function from the __future__ package. We’ll be using that because of the actual print() function rather than the print statement so that this code will work with both Python 2 and Python 3.

And using argparse to handle parsing our command-line arguments. Then, cv2 is imported – cv2 is our OpenCV library and contains our image processing functions. We’ll use the matplotlib package to make plotting our histograms easier.

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
    help = "Path to the image")
args = vars(ap.parse_args())

This code handle parsing the command line arguments. The single argument we need is –image: the path to our image on disk. Lastly, we parse the arguments and store them in a dictionary.

# Load the image and show some basic information on it
image = cv2.imread(args["image"])

Now that we have the path to the image, we can load it off the disk using the cv2.imread function. The cv2.imread function returns a NumPy array representing the image that you lode.

cv2.imshow("Original", image)

This line handle displaying the actual image on our screen. The first parameter is a string, the “name” of our window. The second parameter is a source of the image we loaded off disk.

chans = cv2.split(image)
colors = ("b", "g", "r")

In this section, The first point we are going to do is split the image into its three channels: blue, green, and red. Usually, we read this is red, green, blue (RGB). But, OpenCV stores the image as a NumPy array in reverse order: BGR. This is important to note. We then initialize a tuple of strings representing the colors.

plt.figure()
plt.title("'Flattened' Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")

In this section, we set up our PyPlot figure. We’ll plot the bins on the x-axis and the number of pixels placed into each bin on the y-axis.

for (chan, color) in zip(chans, colors):

We then enter a for loop where we start looping over every one of the channels in the image.

hist = cv2.calcHist([chan], [0], None, [256], [0, 256])

Now every channel, we compute a histogram Explain this in the OpenCV Histogram function section.  we are doing it for every Red, Green, and Blue channel, allowing us to characterize the distribution of pixel intensities.

plt.plot(hist, color = color)
plt.xlim([0, 256])

We add our histogram to the plot

Color histograms for each Red, Green, and Blue channel of the beach image.
Color histograms for each Red, Green, and Blue channel of the beach image.
fig = plt.figure()

ax = fig.add_subplot(131)
hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and B")
plt.colorbar(p)

ax = fig.add_subplot(132)
hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for G and R")
plt.colorbar(p)

ax = fig.add_subplot(133)
hist = cv2.calcHist([chans[0], chans[2]], [0, 1], None,
    [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation = "nearest")
ax.set_title("2D Color Histogram for B and R")
plt.colorbar(p)


print("2D histogram shape: {}, with {} values".format(
    hist.shape, hist.flatten().shape[0]))

This section,  we are measuring a 2D color histogram for each combination of RGB channels: Red and Green, Red and Blue, and Green and Blue.

Now that we are going with multi-dimensional histograms, we require to keep in mind the number of bins we are applying. if we used 256 bins for each dimension in a 2D histogram, our resulting histogram would have 256 × 256 = 65,536 separate pixel counts. Not only is this wasteful of resources, but it’s also not possible. Most applications use somewhere between 8 and 64 bins if measuring multi-dimensional histograms.

The most important takeaway from this code can be seen by inspecting the first arguments to the cv2.calcHist function. Here we see that we are passing in a list of two channels: the Green and Blue channels. And that’s all there is to it.

So, how  2D histogram stored in OpenCV? It’s really a 2D NumPy array.  I used 32 bins for each channel, I now have a 32 × 32 histogram.

Computing 2D color histograms for each combination of Red, Green, and Blue channels.
Computing 2D color histograms for each combination of Red, Green, and Blue channels.

The first is a 2D color histogram for the Green and Blue channels, and the second for Green and Red, and the third for Blue and Red. Shades of blue design low pixel count, whereas shades of red represent large pixel counts. We tend to see several peaks in the Green and Blue histogram, where x=22 and y=12. This corresponds to the green pixels of the vegetation and trees and the blue of the sky and ocean.

hist = cv2.calcHist([image], [0, 1, 2],
    None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
print("3D histogram shape: {}, with {} values".format(
    hist.shape, hist.flatten().shape[0]))

plt.show()

The code here is very simple – it’s just an extension of the code above. We are now measuring an 8 × 8 × 8 histogram for each of the RGB channels. We can’t visualize this histogram, however, we can see that the shape is indeed (8, 8, 8) with 512 values.

More Computer vision tutorial on raspberry pi click COMPUTER VISION BEGINNER

Visit My Blog

 137 total views,  1 views today