In this article, image analysis using Matplotlib and OpenCV is discussed. Let’s first understand how to experiment image data with various styles and how to represent with Histogram.
Importing image data
import matplotlib.pyplot as plt #importing matplotlib
The image should be used in a PNG file as matplotlib supports only PNG images. Here, It’s a 24-bit RGB PNG image (8 bits for each of R, G, B) used in this example. Each inner list represents a pixel. Here, with an RGB image, there are 3 values. For RGB images, matplotlib supports float32 and uint8 data types.
img = plt.imread('flower.png') #reads image data
All about Histogram
Histogram is considered as a graph or plot which is related to frequency of pixels in an Gray Scale Image
with pixel values (ranging from 0 to 255). Grayscale image is an image in which the value of each pixel is a single sample, that is, it carries only intensity information where pixel value varies from 0 to 255. Images of this sort, also known as black-and-white, are composed exclusively of shades of gray, varying from black at the weakest intensity to white at the strongest where Pixel can be considered as a every point in an image.
How GrayScale Image looks like:
It quantifies the number of pixels for each intensity value considered. Before going through Histogram, lets have a rough idea from this given example.
Here, we get intuition about contrast, brightness, intensity distribution etc of that image. As we can see the image and its histogram which is drawn for grayscale image, not color image.
Left region of histogram shows the amount of darker pixels in image and right region shows the amount of brighter pixels.
Histogram creation using numpy array
To create a histogram of our image data, we use the hist() function.
plt.hist(n_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k') #calculating histogram
From the histogram, we can conclude that dark region is more than brighter region.
Now, we will deal with an image which consist of intensity distribution of pixels where pixel value varies. First, we need to calculate histogram using OpenCV in-built function.
Here, we use cv2.calcHist()(in-built function in OpenCV) to find the histogram.
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
images : it is the source image of type uint8 or float32 represented as “[img]”.
channels : it is the index of channel for which we calculate histogram. For grayscale image, its value is  and
color image, you can pass ,  or  to calculate histogram of blue, green or red channel respectively.
mask : mask image. To find histogram of full image, it is given as “None”.
histSize : this represents our BIN count. For full scale, we pass .
ranges : this is our RANGE. Normally, it is [0,256].
Then, we need to plot histogram to show the characteristics of an image.
Analysis using Matplotlib:
Illustration shows that each number of pixels of an image lie upon range of 0 to 255. In the second example, it directly finds the histogram and plot it. We need not use calcHist(). See the code below:
Thus, we conclude that image can be represented as a Histogram to conceive the idea of intensity distribution over an image and further its tranquility.
This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- CLAHE Histogram Eqalization - OpenCV
- Python PIL | Image.histogram()
- Mahotas – Full Histogram of Image
- PyQtGraph – Getting Histogram Object for Image View
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Transition from OpenCV 2 to OpenCV 3.x
- Image resizing using Seam carving using OpenCV in Python
- Histogram using Plotly in Python
- Plotting Histogram in Python using Matplotlib
- Plot 2-D Histogram in Python using Matplotlib
- Histogram Plotting and stretching in Python (without using inbuilt function)
- Compute the histogram of a set of data using NumPy in Python
- OpenCV Python Program to blur an image
- Python | OpenCV program to read and save an Image
- Reading an image in OpenCV using Python
- Python | Detect corner of an image using OpenCV
- Image Pyramid using OpenCV | Python
- Negative transformation of an image using Python and OpenCV
- Python | Image blurring using OpenCV