1. Install matplotlib
- In the cmd environment, enter the Scripts directory according to the python location you installed, and enter the command: pip install matplotlib
- Or install the package in pyCharm
Second, draw the histogram of the picture
code show as below:
import cv2 as cv
from matplotlib import pyplot as plt
def hist_image(image):
color = ("blue", "green", "red")
for i, color in enumerate(color):
hist = cv.calcHist([image], [i], None, [256], [0, 256])
plt.plot(hist, color=color)
plt.xlim([0, 256])
plt.show()
src = cv.imread("./static/image/windows.jpg")
cv.namedWindow("image", cv.WINDOW_NORMAL)
cv.imshow("image", src)
hist_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
operation result:
3. Histogram application
code show as below:
import cv2 as cv
from matplotlib import pyplot as plt
def equalHist_image(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("oldImage", gray) # 因为只能处理灰度图像,所以输出原图的灰度图像用于对比
dst = cv.equalizeHist(gray)
cv.imshow("default",dst)
# 对比度限制(自定义提示参数)
def clahe_image(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(4, 4))#clipLimit是对比度的大小,tileGridSize是每次处理块的大小
dst = clahe.apply(gray)
cv.imshow("define", dst)
src = cv.imread('./static/image/windows.jpg')
equalHist_image(src)
clahe_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
running result: