What is histogram sliding?

Histogram

A histogram is a type of graph that displays the frequency of each element. It is used to represent the frequency distribution. In the case of an image, a histogram can be a graph between the color intensity and its frequency.

An image and its histogram
An image and its histogram

Let’s run the following code to see the image and its histogram:

import cv2 # importing opencv libraries
from matplotlib import pyplot as plt # importing plotting library
image = cv2.imread('/images/img5.png') # reading an image
plt.subplot(2, 1, 1) # creating subplot to display image
plt.title("Image") # allocating title to the plot
plt.imshow(image) # diplaying image
plt.subplot(2, 1, 2) # selecting second subplot
plt.hist(image.ravel(),256,[0,256]) # histogram plotting
plt.title("Histogram", x=0.9, y =0.8) # allocating the title to the plot
plt.show() # displaying the plot

Histogram sliding

Histogram sliding is a technique to shift the frequency distribution to the left or the right. In the case of an image, this shifting causes a change in the brightness of each pixel.

A histogram shifting left and right
A histogram shifting left and right

Example

We can perform histogram sliding by adding or subtracting from each pixel value of the image. Let’s run the following code to see the histogram sliding:

import cv2 # importing opencv libraries
from matplotlib import pyplot as plt # importing plotting library
import numpy as np #importing numpy library
def histogram_sliding(img, shift):
fill = np.ones(img.shape, np.uint8) * abs(shift)
if shift > 0:
return cv2.add(img, fill)
else:
return cv2.subtract(img, fill)
image = cv2.imread('/images/img5.png') # reading an image
image1 = histogram_sliding(image, -30) # shifiting histogram to leftwards
plt.subplot(3, 2, 1) # creating subplot to display image
plt.title("Image") # allocating title to the plot
plt.imshow(image1) # diplaying image
plt.subplot(3, 2, 2) # selecting second subplot
plt.hist(image1.ravel(),256,[0,256]) # histogram plotting
plt.title("Histogram") # allocating the title to the plot
plt.show() # displaying the plot
plt.subplot(3, 2, 3) # creating subplot to display image
plt.imshow(image) # diplaying original image
plt.subplot(3, 2, 4) # selecting second subplot
plt.hist(image.ravel(),256,[0,256]) # histogram plotting
plt.show() # displaying the plot
image2 = histogram_sliding(image, 30) # shifiting histogram to rightwards
plt.subplot(3, 2, 5) # creating subplot to display image
plt.imshow(image2) # diplaying image
plt.subplot(3, 2, 6) # selecting second subplot
plt.hist(image2.ravel(),256,[0,256]) # histogram plotting
plt.show() # displaying the plot

Explanation

  • Lines 1–3: We import the related libraries.
  • Lines 5–10: We define a histogram_sliding() function. We create an np array and add/subtract it from the image.
  • Line 12: We read an image with imread() function of OpenCV.
  • Line 14: We slide the histogram to the left by 30.
  • Line 31: We slide the histogram to the right by 30.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved