How to plot Color Channels Histogram of an Image in Python using OpenCV – easy project – 2022

Machine Learning Projects

In this very short blog, we will see that how we can plot all 3 color channels histogram (red, green, blue) of an Image in Python using OpenCV.

Let’s do it…

Step 1 – Importing required packages for plotting Color Channels Histogram.

import cv2
import matplotlib.pyplot as plt

Step 2 – Let’s read and visualize the image.

imgpath = "test.tiff"
img = cv2.imread(imgpath)

cv2.imshow('Image', img)
cv2.waitKey(0)
Machine Learning Projects

Step 3 – Convert the channels from BGR to RGB.

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

Step 4 – Finally let’s get the Color Channels Histograms…

Syntax: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])

Parameters:

images It is the source image of type uint8 or float32 represented as “[img]”.
channels It is the index of the channel for which we calculate the histogram. For a grayscale image, its value is [0] and for a color image, you can pass [0], [1], or [2] to calculate a histogram of blue, green, or red channel respectively.
mask mask image. To find the histogram of the full image, it is given as “None”.
histSize This represents our BIN count. For full scale, we pass [256].
ranges This is our RANGE. Normally, it is [0,255].
red_hist = cv2.calcHist([img], [0], None, [256], [0, 255])
green_hist = cv2.calcHist([img], [1], None, [256], [0, 255])
blue_hist = cv2.calcHist([img], [2], None, [256], [0, 255])

You can read more about Histogram Calculation here.

Step 5 – Lets plot these color channels histograms.

plt.subplot(4, 1, 1)
plt.imshow(img)
plt.title('image')
plt.xticks([])
plt.yticks([])

plt.subplot(4, 1, 2)
plt.plot(red_hist, color='r')
plt.xlim([0, 255])
plt.title('red histogram')

plt.subplot(4, 1, 3)
plt.plot(green_hist, color='g')
plt.xlim([0, 255])
plt.title('green histogram')

plt.subplot(4, 1, 4)
plt.plot(blue_hist, color='b')
plt.xlim([0, 255])
plt.title('blue histogram')

plt.tight_layout()
plt.show()
  • We have created a canvas with 4 rows and 1 column.
  • In the first row, we have plotted the image with x and y bars removed using xticks() and yticks().
  • In the second row, we have plotted the histogram for the red channel. We have set the limit of the histogram from 0-255 (which is the actual range of the red channel values that every pixel can take).
  • We have done a similar thing for green and blue channels also.
  • In the second last line, we have used plt.tight_layout() just to provide some spacings between the plots.
color channels histogram
Final Results…

NOTE: From this, we can infer that the major portion of the image is RED.

Let’s see the whole code for plotting Color Channels Histogram…

import cv2
import matplotlib.pyplot as plt

imgpath = "4.2.07.tiff"
img = cv2.imread(imgpath)

cv2.imshow('Image', img)
cv2.waitKey(0)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Color Channels Histogram
red_hist = cv2.calcHist([img], [0], None, [256], [0, 255])
green_hist = cv2.calcHist([img], [1], None, [256], [0, 255])
blue_hist = cv2.calcHist([img], [2], None, [256], [0, 255])

plt.subplot(4, 1, 1)
plt.imshow(img)
plt.title('image')
plt.xticks([])
plt.yticks([])

plt.subplot(4, 1, 2)
plt.plot(red_hist, color='r')
plt.xlim([0, 255])
plt.title('red histogram')

plt.subplot(4, 1, 3)
plt.plot(green_hist, color='g')
plt.xlim([0, 255])
plt.title('green histogram')

plt.subplot(4, 1, 4)
plt.plot(blue_hist, color='b')
plt.xlim([0, 255])
plt.title('blue histogram')

plt.tight_layout()
plt.show()

Do let me know if there’s any query regarding Color Channels Histogram by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HOW TO PERFORM BLURRINGS LIKE SIMPLE BLUR, BOX BLUR, GAUSSIAN BLUR, AND MEDIAN BLUR

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Comment

Your email address will not be published.