MLearning.ai
Published in

MLearning.ai

How to plot color channels Histogram of an Image in Python using OpenCV

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

Read the full article with source code here — https://machinelearningprojects.net/plot-color-channels-histogram/

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)

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]])

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 — Let's 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.

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

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

For further code explanation and source code visit here https://machinelearningprojects.net/plot-color-channels-histogram/

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 projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net.

--

--

--

Data Scientists must think like an artist when finding a solution when creating a piece of code. ⚪️ Artists enjoy working on interesting problems, even if there is no obvious answer ⚪️ linktr.ee/mlearning 🔵 Follow to join our 18K+ Unique DAILY Readers 🟠

Recommended from Medium

Considering Cloud Migration? Here’s What You Need to Know

Supporting a monolithic backend for video streaming

Top Java Projects you need to know in 2021

Serverless-Fixed CORS with SAM on full stack local development and deployment

Xmigrate Explained

Standard and Datacenter Windows Server 2016 Versions: Overview

Gamifying Context Sharing

Introduction to GameBox | NFTs and GAME tokens.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Sharma

Abhishek Sharma

Data Scientist || Blogger || machinelearningprojects.net

More from Medium

HOW TO GENERATE A NEGATIVE IMAGE IN PYTHON USING OPENCV — INTERESTING PROJECT

Image Colorizer using Deep Learning

Custom Image Classification Model Using TensorFlow Lite Model Maker

Histogram of an Image with python