Histogram is a graphical representation of the intensity distribution of an image. In simple terms, it represents the number of pixels for each intensity value considered.
Histogram equalization is a method in image processing of contrast adjustment using the image’s histogram. This method usually increases the global contrast of many images, especially when the usable data of the image is represented by close contrast values. Through this adjustment, the intensities can be better distributed on the histogram. This allows for areas of lower local contrast to gain a higher contrast. Histogram equalization accomplishes this by effectively spreading out the most frequent intensity values. The method is useful in images with backgrounds and foregrounds that are both bright or both dark. OpenCV has a function to do this, cv2.equalizeHist(). Its input is just grayscale image and output is our histogram equalized image.
Tools Used: Jupyter NoteBook
Python Libraries: OpenCV, numpy, matplotlib
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('D:\Pictures\BiggestLoser.png')
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
plt.title('Original image')
plt.imshow(image);
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.title('Grayscale image')
plt.imshow(gray_image, cmap='gray', vmin = 0, vmax = 255);
histr = cv2.calcHist([gray_image],[0],None,[256],[0,256])
plt.title('Histogram of the image')
plt.xlabel('grayscale value')
plt.ylabel('pixel count')
plt.plot(histr)
plt.show()
equal = cv2.equalizeHist(gray_image)
result = np.hstack((gray_image, equal))
cv2.imshow('BiggestLoserImage', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
References: wikipedia, personal prepared notes
Reference 1 Reference 2
I tried to make the code simple to understand and gave explanation of the various functions used in the program. Feel free to give your suggestions. Until we meet again in some other post, this is @biggestloser signing off....