"""
本次实训任务为应用线性变化对直方图进行修改,以卡通图像的图片为例,你需要首先读入图片,对图像矩阵进行线性变化,得到新的一副线性
变化后的图像,再进行计算和绘制直方图,在这里考核的重点是对直方图的峰值(最大值)的输出,你需要分别输出原图和改变后的图像的
峰值(最大值),因为一副图像可以唯一确定一副直方图,该直方图的峰值(最大值)也是被唯一被确定的。所以根据提示,完善相应代码,
相信你一定可以的。
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
绘制直方图函数
def grayHist(img,filename):
plt.figure(filename, figsize=(16, 8))
# 展示输入图像
plt.subplot(121)
plt.imshow(img,'gray')
# 展示直方图
plt.subplot(122)
h, w = img.shape[:2]
#将二维数组通过reshape()函数为一维数组
########## Begin ##########
"""
任务1. 将二维数组通过reshape()函数为一维数组,pixelSequence为reshape()函数得到的返回值
"""
numberBins = 256
"""
任务2. 应用plt.hist()绘制直方图,返回有三个值分别是histogram, bins, patch,
参数分别为pixelSequence, numberBins,facecolor='black', histtype='bar'
"""
########## End ##########
print(max(histogram))
plt.xlabel("gray label")
plt.ylabel("number of pixels")
plt.axis([0, 255, 0, np.max(histogram)])
plt.savefig(filename)
plt.show()
if name=='main':
#定义图像地址
img_path='histogram/step5/img_data/ex_1.jpg'
out_path='histogram/step5/stu_img/step5_plt.jpg'
out2_path = 'histogram/step5/stu_img/step5_2_plt.jpg'
#读取灰度图
img = cv2.imread(img_path, 0)
# #此时a=2.0,a>1图像的对比度增强
out = 2.0 * img
########## Begin ##########
"""
任务3. 进行数据截断,大于255的值截断为255
"""
########## End ##########
# 数据类型转换
out = np.around(out)
out = out.astype(np.uint8)
# 分别绘制处理前后的直方图
grayHist(img,out_path)
grayHist(out,out2_path)
将begin和end间代码补全