Python+OpenCV—Matplotlib draws grayscale/color histogram
1. Introduction to Matplotlib.Pyplot
1、Matplotlib
Matplotlib is a plotting library for Python that allows users to easily visualize data and provide a variety of output formats.
Matplotlib can be used to draw various static, dynamic, and interactive charts.
Matplotlib is a very powerful Python drawing tool, we can use this tool to present a lot of data more intuitively in the form of charts.
Matplotlib can draw line plots, scatterplots, contour plots, bar charts, histograms, 3D graphics, even graphic animations, and more.
2、Pyplot
Pyplot is a sub-library of Matplotlib that provides a drawing API similar to MATLAB.
Pyplot is a commonly used drawing module, which makes it easy for users to draw 2D charts.
Pyplot contains a series of related functions of drawing functions, each of which will make some modifications to the current image, for example: mark the image, generate a new image, generate a new drawing area in the image, and so on.
When using it, we can use import to import the pyplot library and set an alias plt:
import matplotlib.pyplot as plt
Then we can use it to draw graphics
Two, gray histogram
1. Main function
Mainly use two functions
(1) calcHist()
(2) hist()
I won’t go into too much detail about the specific usage, you can refer to the documentation yourself, or find explanations from the great gods on the Internet.
2. Realize the code
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread('Photos/1.bmp')
cv.imshow('Img', img)
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('GrayImg', gray_img)
# Gray Histogram
gray_hist = cv.calcHist([gray_img], [0], None, [256], [0,256], False)
# cv.calcHist(images, channels, mask, histSize, ranges, accumulate)
plt.figure(1)
plt.title('Gray Histogram Contour')
plt.xlabel('gray level')
plt.ylabel('number of pixels')
plt.plot(gray_hist)
plt.xlim([0,256])
plt.figure(2)
plt.title('Gray Histogram')
plt.xlabel('gray level')
plt.ylabel('number of pixels')
plt.hist(gray_img.ravel(),256)
plt.show()
cv.waitKey(0)
3. Effect example
3. Color histogram
Similar to the principle of the grayscale histogram, a simple traversal of the three colors is required to output the graphics in a loop.
1. Realize the code
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img = cv.imread('Photos/1.bmp')
cv.imshow('Img', img)
plt.figure()
plt.title('Color Histogram')
plt.xlabel('level')
plt.ylabel('number of pixels')
colors = ('b', 'g', 'r')
for i,item in enumerate(colors):
hist = cv.calcHist([img], [i], None, [256], [0,256])
plt.plot(hist, color=item)
plt.xlim([0,256])
plt.show()
cv.waitKey(0)
2. Effect example
From this figure, we can intuitively see the frequency of the occurrence of different thresholds of the three colors.
————————————————————————————————
Finally, I attach a website that I often consult information documents. The content is very comprehensive, covering almost all branches of the IT field. I personally think it is a very suitable tutorial website for beginners.
https://www.runoob.com/
Hope that we can make progress every day!