前言
这是图像增强部分的最后一点内容,因为在《指数增强》中说过,除了指数增强有FPGA,所以其他几个方法只讲Python,懂原理即可。
一、图像灰度转换
图像灰度处理很简单,把图像的红色、绿色、蓝色通道像素乘上相应的系数再加和。公式如下。
g r a y = r e d × 0.299 + g r e e n × 0.587 + b l u e × 0.114 gray = red \times 0.299 + green \times 0.587 + blue \times 0.114 gray=red×0.299+green×0.587+blue×0.114
import numpy as np
import matplotlib.pyplot as plt
img = plt.imread("lenna.png")
gray = 0.299 * img[:, :, 0] + 0.587 * img[:, :, 1] + 0.114 * img[:, :, 2]
gray *= 255
gray = gray.astype(np.uint8)
#以下用严谨的方式画图
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(1,2,1)
ax.set_title("raw image")
ax.set_xlabel("width")
ax.set_ylabel("height")
plt.imshow(img