To obtain a histogram for finding the frequency of pixels in an Image with pixel values ranging from 0 to 255. Also write the code using OpenCV to perform histogram equalization.
Anaconda - Python 3.7
Read the gray and color image using imread()
Print the image using imshow().
Use calcHist() function to mark the image in graph frequency for gray and color image.
cv2.equalize() is used to transform the gray image to equalized form.
The Histogram of gray scale image and color image is shown.
Developed By: Shafeeq Ahamed. S
Register Number: 212221230092
import cv2
import matplotlib.pyplot as plt
# Gray Scale Image
im = cv2.imread("mikasa_c.png",0)
cv2.imshow("Mikasa",im)
hist = cv2.calcHist([im],[0],None,[256],[0,255])
# Colour Image
im_c = cv2.imread("mikasa_c.png",1)
cv2.imshow("Mikasa",im_c)
hist_c = cv2.calcHist([im_c],[1],None,[256],[0,255])
plt.figure()
plt.title("Histogram of B/W Image")
plt.xlabel("GrayScale Values")
plt.ylabel("Pixel Count")
plt.stem(hist)
plt.show()
plt.figure()
plt.title("Histogram of B/W Image")
plt.xlabel("GrayScale Values")
plt.ylabel("Pixel Count")
plt.stem(hist_c)
plt.show()
equ = cv2.equalizeHist(im)
cv2.imshow("Mikasa",equ)
hist1 = cv2.calcHist([equ],[0],None,[256],[0,255])
plt.figure()
plt.title("Histogram of B/W Image")
plt.xlabel("GrayScale Values")
plt.ylabel("Pixel Count")
plt.stem(hist1)
plt.show()
Thus the histogram for finding the frequency of pixels in an image with pixel values ranging from 0 to 255 is obtained. Also,histogram equalization is done for the gray scale image using OpenCV.