introduce
So far in our advanced OpenCV tutorial we have:
Understand the concept of contrast.
Understand the concept of histogram equalization.
Implements contrast enhancement on grayscale images.
Plot a pixel histogram of a grayscale image.
However, as we all know, our world is made up of many, many colors. Now we will try to analyze the contrast and pixel intensities of color images, i.e. images with color channels present. lcd display lcm
Understanding Color Images
If you’ve read our previous article, remember correctly, we learned that advanced OpenCV uses BGR color channels, not RGB. You will find that the red and blue channels have been swapped.
As you can see in the image above, there are plenty of colors that are both striking and eye-catching. These different shades of color are the result of mixing color channels.
There are three color channels in this image:
blue
green
red
It is through the mixing of these colors that the secondary colors and many other colors that belong to the color spectrum arise.
Gain Image Insights
The first and most important step is to import the necessary packages into our python script, and then we load the image into our system RAM. This will be done using the imread() method provided by the OpenCV package:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2. imread (‘C:/Users/Shivek/Pictures/Picture1.jpg’)
The output of the above code block will appear as follows:
As one might observe, the image is too large to display at full size on my screen. However, we will not reduce the size of the image. This is because we want to gain insight into the properties and related information of pixels.
First, let’s look at the shape of the Image (NumPy array):
print(image.shape)
The output of the above line of code will appear as follows:
After careful inspection of this tuple of information, it is decrypted as follows:
The color channel is BGR-NOT RGB. Next, let’s look at the size of the image — the size tells us the total number of pixels that make up the entire image.
print(image.size)
output:
6912000
There are over 6 million pixels in our images.
Let’s look at the first pixel in the image. This is the pixel in the first row and first column.
print(image[0][0])
Its BGR values are 22, 15, and 6, respectively.
Now we move on: BGR pixel count map.
BGR pixel intensity line graph
Since each pixel in our image contains 3 color channels, we will need to go through all the pixels 3 times, picking values from the B, G, and R channels each time. To do this, you can use a for loop with the enumerate() function.
As we probably already know, the enumerate() function will allow us to iterate over a fixed number of elements and automatically record the number of each element.
We will use the MatPlotLib package to obtain visuals of pixel intensities and counts in the form of line graphs.
colors = (‘blue’, ‘green’, ‘red’)
label = (“Blue”, “Green”, “Red”)
for count, color in enumerate(colors):
histogram = cv2. calcHist([image], [count], None, [256], [0, 256])
plt. plot(histogram, color = color, label = label[count] + str(“Pixels”))
A line-by-line explanation of the above code block is as follows:
In the following line of code, we create a tuple containing the colors of the three different lines of the graph. Since there are 3 iterations and each iteration selects a different color, we can see the BGR pattern on the graph.
Color = (‘Blue’, ‘Green’, ‘Red’)
After this, we proceed to create a tuple containing the corresponding labels for the chart legend. Again, at each iteration, a label will be chosen to mark the corresponding color of the graph. These labels will be included in the legend of the chart, which we will see in the lines of code that follow.
label = (“Blue”, “Green”, “Red”)
To facilitate graph generation, we have a for-enumerate loop that will be executed to select each desired item from the pixel array and tuple on which they will be drawn as a line graph on the same canvas.
for count, color in enumerate(colors):
histogram = cv2. calcHist([image], [count], None, [256], [0, 256])
plt. plot(histogram, color = color, label = label[count] + str(“Pixels”))
The line of code in the for loop looks like this:
histogram = cv2. calcHist([image], [count], None, [256], [0, 256])
We will use the calcHist() method from the OpenCV package, which will obtain a histogram of pixel intensities, as we learned in previous articles. We pass in the following parameters:
To plot the graph, we use the plot() method provided by the MatPlotLib package as follows:
plt. plot(histogram, color=color, label=label[count]+str(“Pixels”))
We pass in the data to be plotted, followed by the appropriate color. Specify a label for the graph and iterate over the corresponding label in the index tuple based on the for loop. We append “pixels” to the label name to make it clear to the viewer the finished graphic.
Before displaying the chart to the end user, we add some elements to the chart, such as:
title.
Label for the Y axis.
Label for the x-axis.
a legend.
To wrap up processing, we use the show() method provided by the MatPlotLib package to display the visual on the screen.
plt. title(“Histogram Showing Number Of Pixels Belonging To Respective Pixel Intensity”, color=”crimson”)
plt. ylabel(“Number Of Pixels”, color=”crimson”)
plt. xlabel(“Pixel Intensity”, color=”crimson”)
plt. legend(numpoints=1, loc=”best”)
plt. show()
The final BGR pixel intensity line graph will look like this:
The visualization above shows and tells us exactly how the pixels are distributed in the original color image. The locations of the 6 million+ pixels that make up our image can be summarized in the line graph above.
Most of our pixels are on the left side of the image, and as you can see from the image, the image is blue from the middle to the right, as indicated by the blue pixel intensity line. Blue pixel intensity lines dominate these areas – if one looks at the image, it looks like this:
Notice how the full image fits on my computer screen – this is because I resized it.
A very interesting fact is that although I have resized the image, if I were to create a pixel intensity map on the resized image, the gradient of the BGR map would not be any different compared to the original image – it Just reduced the size or “scale”.
in conclusion
In this article, we examine:
· Understanding color images using OpenCV in the Python programming language
· BGR color channel
· Mapping BGR pixels to BGR pixel intensity line plots using the MatPlotLib package in Python