Skip to content
Advertisement

Why can’t I plot image histogram’s in python-OpenCV

I have to get a image, apply Histogram equalization and plot the histogram of both images (original and modified). So I tried this code:

import cv2
import numpy as np
from skimage import io, img_as_float, img_as_ubyte
import matplotlib.pyplot as plt  
from matplotlib import pyplot as plt

gray = cv2.imread("folderimg1.jpg",0)


equ = cv2.equalizeHist(gray)

io.imshow(gray)
io.imshow(equ)

histr = cv2.calcHist([gray],[0],None,[256],[0,256])
  
plt.plot(histr)
plt.show()

But The plot returned is..: enter image description here

How can I fix it?

Advertisement

Answer

You are not creating a new figure, so the histogram is plotted on the last figure that displays the image.
You may create a new figure before plotting the histogram:

histr = cv2.calcHist(gray, [0], None, [256], (0,255))

plt.figure() # Create new figure for the histogram plot
plt.plot(histr)
plt.show()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement