- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV Python - Histogram
Histogram shows the intensity distribution in an image. It plots the pixel values (0 to 255) on X axis and number of pixels on Y axis.
By using histogram, one can understand the contrast, brightness and intensity distribution of the specified image. The bins in a histogram represent incremental parts of the values on X axis.
In our case, it is the pixel value and the default bin size is one.
In OpenCV pbrary, the function cv2.calcHist() function which computes the histogram from the input image. The command for the function is as follows −
cv.calcHist(images, channels, mask, histSize, ranges)
Parameters
The cv2.calcHist() function’s parameters are as follows −
images − It is the source image of type uint8 or float32, in square brackets, i.e., "[img]".
channels − It is the index of the channel for which we calculate histogram. For a grayscale image, its value is [0]. For BGR images, you can pass [0], [1] or [2] to calculate the histogram of each channel.
mask − Mask image is given as "None" for full image. For a particular region of image, you have to create a mask image for that and give it as a mask.
histSize − This represents our BIN count.
ranges − Normally, it is [0,256].
Histogram using Matplotpb
A histogram plot can be obtained either with the help of Matplotpb’s pyplot.plot() function or by calpng Polypnes() function from OpenCV pbrary.
Example
Following program computes histogram for each channel in the image (lena.jpg) and plots the intensity distribution for each channel −
import numpy as np import cv2 as cv from matplotpb import pyplot as plt img = cv.imread( lena.jpg ) color = ( b , g , r ) for i,col in enumerate(color): hist = cv.calcHist([img],[i],None,[256],[0,256]) plt.plot(hist, color = col) plt.xpm([0,256]) plt.show()