Python OpenCV - cv2.calcHist method
Last Updated :
03 Jan, 2023
OpenCV provides us with the cv2.calcHist() function to calculate the image histograms. We could apply it to calculate the histogram of the constituent color channels (blue, green, and red) of the image. When we read the image using cv2.imread() method, the image read is in BGR format. We could use the below syntax for the cv2.calcHist() function.
cv2.calcHist() function:
Syntax: cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
Parameters:
- images: list of images as numpy arrays. All images must be of the same dtype and same size.
- channels: list of the channels used to calculate the histograms.
- mask: optional mask (8 bit array) of the same size as the input image.
- histSize: histogram sizes in each dimension
- ranges: Array of the dims arrays of the histogram bin boundaries in each dimension
- hist: Output histogram
- accumulate: accumulation flag, enables to compute a single histogram from several sets of arrays.
Return: It returns an array of histogram points of dtype float32.
Let's understand the cv2.calcHist() function with the help of some Python examples.
The following image is used as an input image in the examples below.

Example 1:
In this example, we calculate the histogram of the blue color channel of the input image "mountain.jpg" using cv2.calcHist() function. We pass the parameter channels = [0] to calculate the histogram of the blue channel. We also plot the histogram using Matplotlib.
Python3
# Python program to compute and visualize the
# histogram of Blue channel of image
%matplotlib inline
# importing libraries
import cv2
import numpy as np
from matplotlib import pyplot as plt
# reading the input image
img = cv2.imread('mountain.jpg')
# computing the histogram of the blue channel of the image
hist = cv2.calcHist([img],[0],None,[256],[0,256])
# plot the above computed histogram
plt.plot(hist, color='b')
plt.title('Image Histogram For Blue Channel GFG')
plt.show()
Output:

Example 2:
In this example, we calculate the histogram of the green color channel of the input image "mountain.jpg" using cv2.calcHist() function. We pass the parameter channels = [1] to calculate the histogram of the green channel. We also plot the histogram using Matplotlib.
Python3
# Python program to compute and visualize the
# histogram of Green channel of image
%matplotlib inline
# importing libraries
import cv2
import numpy as np
from matplotlib import pyplot as plt
# reading the input image
img = cv2.imread('mountain.jpg')
# computing the histogram of the green channel of the image
hist = cv2.calcHist([img],[1],None,[256],[0,256])
# plot the above computed histogram
plt.plot(hist, color='g')
plt.title('Image Histogram For Green Channel GFG')
plt.show()
Output:

Example 3:
In this example, we calculate the histogram of the red color channel of the input image "mountain.jpg" using cv2.calcHist() function. We pass the parameter channels = [2] to calculate the histogram of the red channel. We also plot the histogram using Matplotlib.
Python3
# Python program to compute and visualize the
# histogram of Red channel of image
%matplotlib inline
# importing libraries
import cv2
import numpy as np
from matplotlib import pyplot as plt
# reading the input image
img = cv2.imread('mountain.jpg')
# computing the histogram of the Red channel of the image
hist = cv2.calcHist([img],[2],None,[256],[0,256])
# plot the above computed histogram
plt.plot(hist, color='r')
plt.title('Image Histogram For Red Channel GFG')
plt.show()
Output:

Example 4:
In this example, we calculate the histogram of all three color channels of the input image "mountain.jpg" using cv2.calcHist() function. We use a for loop to iterate over all three color channels. We also plot the histogram using Matplotlib.
Python3
# Python program to compute and visualize the
# histogram of image for all three channels
%matplotlib inline
# importing libraries
import cv2
import numpy as np
from matplotlib import pyplot as plt
# reading the input image
img = cv2.imread('mountain.jpg')
# define colors to plot the histograms
colors = ('b','g','r')
# compute and plot the image histograms
for i,color in enumerate(colors):
hist = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(hist,color = color)
plt.title('Image Histogram GFG')
plt.show()
Output:
Similar Reads
Essential OpenCV Functions to Get Started into Computer Vision Computer vision is a process by which we can understand the images and videos how they are stored and how we can manipulate and retrieve data from them. Computer Vision is the base or mostly used for Artificial Intelligence. Computer-Vision is playing a major role in self-driving cars, robotics as w
7 min read
cv2.imread() method - Python OpenCV OpenCV-Python is a Python library used to solve computer vision tasks. cv2.imread() method loads an image from the specified file. If the image cannot be read because of missing file, improper permissions or an unsupported/invalid format then it returns an empty matrix.Example:Pythonimport cv2 image
2 min read
Python OpenCV | cv2.imshow() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imshow() method is used to display an image in a window. The window automatically fits the image size. Syntax: cv2.imshow(window_name, image)Parameters: window_name: A string representing the name of the wi
3 min read
Python OpenCV | cv2.cvtColor() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV. We will use some of color space conversion codes be
4 min read
Python OpenCV | cv2.imwrite() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image) Parameters:file
2 min read
Python OpenCV | cv2.rectangle() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.rectangle() method is used to draw a rectangle on any image. Syntax: cv2.rectangle(image, start_point, end_point, color, thickness) Parameters:image: It is the image on which rectangle is to be drawn. start
4 min read
cv2.circle() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.circle() method is used to draw a circle on any image. We use this image:Example:Pythonimport cv2 path = r'C:\Users\user\Desktop\geeks14.png' src = cv2.imread(path) cv2.circle(src, center=(100, 100), radius
2 min read
Python OpenCV | cv2.line() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.line() method is used to draw a line on any image.Syntax:cv2.line(image, start_point, end_point, color, thickness) Parameters: image: It is the image on which line is to be drawn. start_point: It is the sta
3 min read
Python OpenCV | cv2.putText() method OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.putText() method is used to draw a text string on any image. Syntax: cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) Parameters:image: It is the image on w
5 min read
Line detection in python with OpenCV | Houghline method The Hough Transform is a method that is used in image processing to detect any shape, if that shape can be represented in mathematical form. It can detect the shape even if it is broken or distorted a little bit.We will see how Hough transform works for line detection using the HoughLine transform m
6 min read