"""
# -*- coding :utf-8 -*-
# @Time:2022/3/129:05
# @Author:WEIKETAN
# @File:equalizeHIst.py
# @Desc:灰度直方图、直方图均衡化、掩膜的应用,直方图规定化(TODO)
"""
import matplotlib
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
""" 1.draw and show grayscale_image"""
def dsGrayscaleimage():
# 1.read grayscale_image directly
"""
imread(imgpath, 0)
:param
imgpath
gray or RGB: default RGB、 gray -- 0
:return:the image arrays
"""
img = cv.imread("F:\\Develop_Tools_Python\\Python_WorkSpace\\Notebook_Python_WorkSpace\\IMage_processing\\homeworks\\photo\\cat.jpeg", 0)
plt.imshow(img, cmap=plt.cm.gray) # default--热图(heatmap)、 another show_style: cv.imshow("grayImg",img)
# 2.calculate grayscale_image
"""
cv2.calcHist(images,channels,mask,histSize,ranges[,hist[,accumulate]])
:param
images:raw images eg:[img]
channels:number [0]--grayImage [0][1][2]--BGR(26 letter order)
mask:local or globel default:globel(None)
histSize:the number of BIN(直条或组距) eg:[256]
ranges:the range of pixel eg:[0,255]
:return:the histr arrays
"""
histr = cv.calcHist([img], [0], None, [256], [0, 255])
# print(histr, histr.shape)
# 3.draw grayImage
"""
pyplot.figsize()
:param
figsize : (float, float), default: :rc:`figure.figsize`
Width, height in inches.
dpi : float, default: :rc:`figure.dpi`
The resolution of the figure in dots-per-inch.
"""
plt.figure(figsize=(10, 6), dpi=100)
plt.plot(histr)
plt.show()
""" 2.mask_ROI(Region of interest):使用掩膜提取感兴趣区域"""
def mask_ROI():
# 1. 直接以灰度图的方式读入
img = cv.imread("F:\\Develop_Tools_Python\\Python_WorkSpace\\Notebook_Python_WorkSpace\\IMage_processing\\homeworks\\photo\\cat.jpeg", 0)
# plt.imshow(img, cmap=plt.cm.gray)
# 2. 创建膜板
mask = np.zeros(img.shape[:2], np.uint8) # 提取图片的长和宽
mask[400:650, 200:500] = 1
# plt.imshow(mask, cmap=plt.cm.gray)
# 3. 掩膜后数据
masked_img = cv.bitwise_and(img, img, mask=mask)
# plt.imshow(masked_img, cmap=plt.cm.gray)
# 4. 统计掩膜后图像的灰度图
mask_histr = cv.calcHist([img], [0], mask, [256], [0, 255])
# 5. 图像展示
# 设置字体为楷体,解决显示字体乱码问题
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
axes[0, 0].imshow(img, cmap=plt.cm.gray)
axes[0, 0].set_title("原图")
axes[0, 1].imshow(mask, cmap=plt.cm.gray)
axes[0, 1].set_title("膜板数据")
axes[1, 0].imshow(masked_img, cmap=plt.cm.gray)
axes[1, 0].set_title("掩膜后数据")
axes[1, 1].plot(mask_histr)
axes[1, 1].grid()
axes[1, 1].set_title('灰度直方图')
plt.show()
""" 3.直方图均衡化"""
def equalizeHist():
# 1. 读入图片
img = cv.imread("F:\\Develop_Tools_Python\\Python_WorkSpace\\Notebook_Python_WorkSpace\\IMage_processing\\homeworks\\photo\\cat.jpeg", 0)
# 2. 原图直方图
histr = cv.calcHist([img], [0], None, [256], [0, 255])
# 3. 均衡化处理
dst = cv.equalizeHist(img)
# 4. 均衡化后直方图
histr1 = cv.calcHist([dst], [0], None, [256], [0, 255])
# 4. 结果显示
# 设置字体为楷体,解决显示字体乱码问题
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
axes[0, 0].imshow(img, cmap=plt.cm.gray)
axes[0, 0].set_title("原图")
axes[0, 1].imshow(dst, cmap=plt.cm.gray)
axes[0, 1].set_title("均衡化过的图片")
axes[1, 0].plot(histr)
axes[1, 0].set_title("原图直方图")
axes[1, 1].plot(histr1)
axes[1, 1].grid()
axes[1, 1].set_title('均衡化直方图')
plt.show()
""" 4.自适应的直方图均衡化"""
def selfAdaptionQqualizeHist():
# 1. 读取图片
img = cv.imread("F:\\Develop_Tools_Python\\Python_WorkSpace\\Notebook_Python_WorkSpace\\IMage_processing\\homeworks\\photo\\cat.jpeg", 0)
# 2. 原图直方图
histr = cv.calcHist([img], [0], None, [256], [0, 255])
# 3. 创建一个自适应均衡化的对象,并应用于图像
# clipLimit:对比度限制,默认为 40
# tileGridSize:分块大小,默认为 8 * 8
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl1 = clahe.apply(img)
# 4. 原图直方图
histr1 = cv.calcHist([cl1], [0], None, [256], [0, 255])
# 5. 图像显示
# 设置字体为楷体,解决显示字体乱码问题
matplotlib.rcParams['font.sans-serif'] = ['KaiTi']
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))
axes[0, 0].imshow(img, cmap=plt.cm.gray)
axes[0, 0].set_title("原图")
axes[0, 1].imshow(cl1, cmap=plt.cm.gray)
axes[0, 1].set_title("均衡化过的图片")
axes[1, 0].plot(histr)
axes[1, 0].set_title("原图直方图")
axes[1, 1].plot(histr1)
axes[1, 1].set_title('均衡化直方图')
plt.show()
if __name__ == '__main__':
# 1.灰度直方图计算和显示
# dsGrayscaleimage()
# 2.使用掩膜提取感兴趣区域
# mask_ROI()
# 3.直方图均衡化
# equalizeHist()
# 4. 自适应直方图均衡化
selfAdaptionQqualizeHist()
# 5.直方图规定化
# todo