I used the code below to read an image then generate a color histogram for it in python.
path = r"G:\3Y_individualProject\farm_colormap1.jpg"
colormap1 = cv2.imread(path)
colormap1=cv2.cvtColor(colormap1, cv2.COLOR_BGR2RGB)
plt.imshow(colormap1)
chans=cv2.split(colormap1)
colors=("b", "g", "r")
plt.figure()
plt.title("Color histogram")
plt.xlabel("Bins")
plt.ylabel("Number of pixels")
for (chan, c) in zip(chans, colors):
hist=cv2.calcHist([chan], [0], None, [256], [0,256])
plt.plot(hist, color=c)
plt.xlim([0,256])
plt.show()
However, it plots 3 color channels (red, green, blue) into 3 different figures like this
You call plt.show()
twice. Therefore after executing one part of the loop it shows and then it does the loop again and it shows the second one.
The fixed version is as follows:
path = r"G:\3Y_individualProject\farm_colormap1.jpg"
colormap1 = cv2.imread(path)
colormap1=cv2.cvtColor(colormap1, cv2.COLOR_BGR2RGB)
plt.imshow(colormap1)
chans=cv2.split(colormap1)
colors=("b", "g", "r")
plt.figure()
plt.title("Color histogram")
plt.xlabel("Bins")
plt.ylabel("Number of pixels")
for (chan, c) in zip(chans, colors):
hist=cv2.calcHist([chan], [0], None, [256], [0,256])
plt.plot(hist, color=c)
plt.xlim([0,256])
plt.show()