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
Import cv2, matplotlib.py libraries and display the saved images using cv2.imshow().
Use cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) to find the histogram of the image.
Plot the image and its stem plots using the plt.show() and plt.stem() functions.
Equalize the grayscale image using the in-built function cv2.equalizeHist().
Print the original and equalized image using cv2.imshow() and end the program.
# Developed By: Y SHAVEDHA
# Register Number: 212221230095
import cv2
import matplotlib.pyplot as plt
# Write your code to find the histogram of gray scale image and color image channels.
color_image=cv2.imread("rcolorimg2.jpg")
gray_image=cv2.imread("gimg.jpg")
cv2.imshow("Gray Image",gray_image)
cv2.imshow("Color_image",color_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Display the histogram of gray scale image and any one channel histogram from color image
hist_g = cv2.calcHist([gray_image],[0],None,[256],[0,256])
plt.figure()
plt.imshow(gray_image)
plt.show()
plt.title("Histogram")
plt.xlabel("Grayscale Value")
plt.ylabel("Pixel Count")
plt.stem(hist_g)
plt.show()
hist_c = cv2.calcHist([color_image],[2],None,[256],[0,256])
plt.figure()
plt.imshow(color_image)
plt.show()
plt.title("Histogram of Color Image - Red Channel")
plt.xlabel("Intensity Value")
plt.ylabel("Pixel Count")
plt.stem(hist_c)
plt.show()
# Write the code to perform histogram equalization of the image.
gray_image = cv2.imread("gimg.jpg",0)
cv2.imshow("Gray Image",gray_image)
equ = cv2.equalizeHist(gray_image)
cv2.imshow("Equalized Image",equ)
cv2.waitKey(0)
cv2.destroyAllWindows
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.