Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Enhancing Contrast by Histogram Equalization





No account yet ?

Sign up to access all content


Tested in Anaconda and Python 3.7

import cv2
import numpy as np
 
#The line below is necessary to show Matplotlib's plots inside a Jupyter Notebook
%matplotlib inline
from matplotlib import pyplot as plt
 
#Use this helper function if you are working in Jupyter Lab
#If not, then directly use cv2.imshow(<window name>, <image>)
 
def showimage(myimage):
    if (myimage.ndim>2):  #This only applies to RGB or RGBA images (e.g. not to Black and White images)
        myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order
         
    fig, ax = plt.subplots(figsize=[10,10])
    ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()
 
# Read in our image as Grayscale
Phone = cv2.imread("woman-g7def3b622_1920.jpg")
Phone_grey = cv2.cvtColor(Phone, cv2.COLOR_BGR2GRAY)
showimage(Phone_grey)  #This displays our image

# Using Numpy Histogram function, we determine the number of pixels with intensity
# between 0..255.
Phone_hist, Phone_bin = np.histogram(Phone_grey,256,[0,256])
print(Phone_hist)
 
plt.hist(Phone_grey.ravel(), 256,[0,256])
plt.show()
 
# Using OpenCV's built in equalizeHist function, we could easily equalize the image
# intensity by passing in our greyscale image
Phone_e = cv2.equalizeHist(Phone_grey)
 
# With Matplotlib, we now plot the two histograms side by side for comparison
# This illustrates how the histogram looks like after equalization
 
fig, ax = plt.subplots(ncols=2, figsize=(12,6))
ax[0].hist(Phone_grey.ravel(), 256,[0,256])
ax[1].hist(Phone_e.ravel(),256,[0,256])
plt.show()
 
# Using Numpy's function to append the two images horizontally
side_by_side = np.hstack((Phone_grey,Phone_e))
 
showimage(side_by_side)
 
 
 
 
Free image provided by pexel.com
Free image provided by pexel.com




Tested in Anaconda and Python 3.7

import cv2
import numpy as np
 
#The line below is necessary to show Matplotlib's plots inside a Jupyter Notebook
%matplotlib inline
 
from matplotlib import pyplot as plt
 
#Use this helper function if you are working in Jupyter Lab
#If not, then directly use cv2.imshow(<window name>, <image>)
 
def showimage(myimage, figsize=[10,10]):
    if (myimage.ndim>2):  #This only applies to RGB or RGBA images (e.g. not to Black and White images)
        myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order
         
    fig, ax = plt.subplots(figsize=figsize)
    ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()
 
colorimage = cv2.imread("woman-g7def3b622_1920.jpg")
showimage(colorimage)
 
color = ('b','g','r')
 
#Loop through each color sequentially
for i,col in enumerate(color):
 
    #To use OpenCV's calcHist function, uncomment below
    #histr = cv2.calcHist([colorimage],[i],None,[256],[0,256])
     
    #To use numpy histogram function, uncomment below
    histr, _ = np.histogram(colorimage[:,:,i],256,[0,256])
 
    plt.plot(histr,color = col)  #Add histogram to our plot 
    plt.xlim([0,256])
 
plt.show()  #Show our plot

# For ease of understanding, we explicitly equalize each channel individually
colorimage_b = cv2.equalizeHist(colorimage[:,:,0])
colorimage_g = cv2.equalizeHist(colorimage[:,:,1])
colorimage_r = cv2.equalizeHist(colorimage[:,:,2])
 
# Next we stack our equalized channels back into a single image
colorimage_e = np.stack((colorimage_b,colorimage_g,colorimage_r), axis=2)
colorimage_e.shape
 
# Using Numpy to calculate the histogram
color = ('b','g','r')
for i,col in enumerate(color):
    histr, _ = np.histogram(colorimage_e[:,:,i],256,[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()
# Using Numpy's function to append the two images horizontally
side_by_side = np.hstack((colorimage,colorimage_e))
showimage(side_by_side,[20,10])
 
 
Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com
?>









Filters using histograms













Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License