I used the code below to generate histograms of a colored image using 2 methods :
Method 1 :-
- Using cv2.calcHist() function to calculate the frequency
- Using plt.plot() to generate a line plot of the frequency
Method 2 :-
- Using plt.hist() function to calculate and generate the histogram (I added bin=250 so that the 2 histograms are consistent)
Observation : Both histograms are roughly similar. The 1st histogram (using plt.plot) looks pretty smooth. However the 2nd histogram (using plt.hist) has additional spikes and drops.
Question : Since the image only has int values there shouldn't be inconsistent binning. What is the reason for these additional spikes and drops in the histogram-2 ?
blue_bricks = cv2.imread('Computer-Vision-with-Python/DATA/bricks.jpg')
fig = plt.figure(figsize=(17,10))
color = ['b','g','r']
# Histogram Type-1
fig.add_subplot(2,2,1)
for i,c in enumerate(color):
hist = cv2.calcHist([blue_bricks], mask=None, channels=[i], histSize=[256], ranges=[0,256])
plt.plot(hist,color=c)
plt.title('Histogram-1')
# Histogram Type-2
fig.add_subplot(2,2,2)
for i,c in enumerate(color):
plt.hist(blue_bricks[:,:,i].flatten(),color=c, alpha=0.5, bins=250)
plt.title('Histogram-2')

