Tuesday, 23 March 2021

opencv 22 histogram


red channel

green channel

blue channel

histogram
#main.py
import numpy as np
import cv2
from matplotlib import pyplot as plt

mountain1 = cv2.imread('assets/mountain2.jpg')
b, g, r = cv2.split(mountain1)

cv2.imshow('img', mountain1)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('r', r)

fig, axs = plt.subplots(1, 3)
axs[0].hist(b.ravel(), 256, [0, 256], color='b')
axs[0].set_title('blue')
axs[1].hist(g.ravel(), 256, [0, 256], color='g')
axs[1].set_title('green')
axs[2].hist(r.ravel(), 256, [0, 256], color='r')
axs[2].set_title('red')

for ax in axs.flat:
    ax.set(xlabel='pixel value', ylabel='count')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

#plt.hist(b.ravel(), 256, [0, 256])
#plt.hist(g.ravel(), 256, [0, 256])
#plt.hist(r.ravel(), 256, [0, 256])
fig.suptitle('image histogram')
plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

reference:

matplotlib import ft2font error
pip uninstall matplotlib
pip install -U matplotlib==3.2.0rc1

matplotlib multiple plot

No comments:

Post a Comment