In this tutorial, I will show you the Basic Grayscale Histogram OpenCV RaspberryPi Step By step Complet Process.
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)
- images: This is the image that we need to compute a histogram for. Wrap it as a list: [myImage].
- 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 . To compute a histogram for all three red, green, and blue channels, the channels list would be [0, 1, 2].
- 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.
- 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].
- 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.
≡ Grayscale Histogram OpenCV RaspberryPi Code
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:
you can see like this interface
Now we can start writing This code on GNU nano:
from matplotlib import pyplot as plt 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"]) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Original", image) cv2.waitKey(0) hist = cv2.calcHist([image], , None, , [0, 256]) plt.figure() plt.title("Grayscale Histogram") plt.xlabel("Bins") plt.ylabel("# of Pixels") plt.plot(hist) plt.xlim([0, 256]) plt.show()
Now save this file named grayscale_histogram.py and exit using ctrl + x, y, enter. we simply open up a raspberry terminal window and execute the following command:
python grayscale_histogram.py --image bay.jpg
see like this interface
from matplotlib import pyplot as plt import argparse import cv2
We’ll apply argparse to handle parsing our command-line arguments. Then, cv2 is imported – cv2 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()) # Load the image and show it image = cv2.imread(args["image"]) cv2.imshow("Original", image)
In this section we have the necessary packages imported, we construct our argument parser and load our image. We only require one argument: the path to the image we are going to work. We then load our image off disk and present it.
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
This, line we convert the image from the RGB colorspace to gray
hist = cv2.calcHist([image], , None, , [0, 256])
This line computes the actual histogram. Explain this in the OpenCV Histogram function section. We can see that our first parameter is the grayscale image. A grayscale image has only one channel, hence we have a value of  for channels. We don’t have a mask, so we set the mask value to None. We will use 256 bins in our histogram, and the possible values range from 0 to 256.
Finally, a call to plt.plot() plots our grayscale histogram, the results of which can be seen in
the bins (0-255) are plotted on the x-axis. And the y-axis counts the number of pixels in each bin. The bulk of the pixels fall in the range of roughly 60 to 120. Looking at the right tail of the histogram, we see very few pixels in the range of 200 to 255. This means that there are very few “white” pixels in the image.
More Computer vision tutorial on raspberry pi click COMPUTER VISION BEGINNER
Visit My Blog
143 total views, 1 views today