1、对图片中的每个像素值增加50个像素和减去50个像素
# 1 导入库
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 2 方法:显示图片
def show_image(image, title, pos):
# 顺序转换:BGR TO RGB
image_RGB = image[:, :, ::-1] # shape : (height, width, channel)
# 显示标题
plt.title(title)
plt.subplot(2, 3, pos) # 定位
plt.imshow(image_RGB)
# 3 方法:显示图片的灰度直方图
def show_histogram(hist, title, pos, color):
# 显示标题
plt.title(title)
plt.subplot(2, 3, pos) # 定位图片
plt.xlabel("Bins") # 横轴信息
plt.ylabel("Pixels") # 纵轴信息
plt.xlim([0, 256]) # 范围
plt.plot(hist, color=color) # 绘制直方图
# 4 主函数 main()
def main():
# 5 创建画布
plt.figure(figsize=(15, 6)) # 画布大小
plt.suptitle("Gray Image Histogram", fontsize=14, fontweight="bold") # 设置标题形式
# 6 加载图片
img = cv2.imread("children.jpg")
# 7 灰度转换
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 8 计算灰度图的直方图
hist_img = cv2.calcHist([img_gray], [0], None, [256], [0, 256])
# 9 展示灰度直方图
# 灰度图转换成BGR格式图片
img_BGR = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
show_image(img_BGR, "BGR image", 1)
show_histogram(hist_img, "gray image histogram", 4, "m")
# 10 对图片中的每个像素值增加50个像素
M = np.ones(img_gray.shape, np.uint8) * 50 # 构建矩阵
# np.ones()函数返回给定形状和数据类型的新数组,其中元素的值设置为1。 乘以50
added_img = cv2.add(img_gray, M)
add_img_hist = cv2.calcHist([added_img], [0], None, [256], [0, 256]) # 计算直方图 列表元素
added_img_BGR = cv2.cvtColor(added_img, cv2.COLOR_GRAY2BGR) #BGR
show_image(added_img_BGR, "added image", 2)
show_histogram(add_img_hist, "added image hist", 5, "m")
# 11 对图片中的每个像素值减去50个像素
subtract_img = cv2.subtract(img_gray, M)
subtract_img_hist = cv2.calcHist([subtract_img], [0], None, [256], [0, 256]) # 计算直方图
subtract_img_BGR = cv2.cvtColor(subtract_img, cv2.COLOR_GRAY2BGR)
show_image(subtract_img_BGR, "subtracted image", 3)
show_histogram(subtract_img_hist, "subtracted image hist", 6, "m")
plt.show()
# 调用主函数
if __name__ == '__main__':
main()
2、灰度直方图——mask
mask : 提取感兴趣区域。
# 1 导入库
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 2 方法:显示图片
def show_image(image, title, pos):
img_RGB = image[:, :, ::-1] # BGR to RGB
plt.title(title)
plt.subplot(2, 2, pos)
plt.imshow(img_RGB)
# 3 方法:显示灰度直方图
def show_histogram(hist, title, pos, color):
plt.subplot(2, 2, pos)
plt.title(title)
plt.xlim([0, 256])
plt.plot(hist, color=color)
# 4 主函数
def main():
# 5 创建画布
plt.figure(figsize=(12, 7))
plt.suptitle("Gray Image and Histogram with mask", fontsize=16, fontweight="bold")
# 6 读取图片并灰度转换,计算直方图,显示
img_gray = cv2.imread("children.jpg", cv2.COLOR_BGR2GRAY) # 读取并进行灰度转换
img_gray_hist = cv2.calcHist([img_gray], [0], None, [256], [0, 256]) # 计算直方图
show_image(img_gray, "image gray", 1)
show_histogram(img_gray_hist, "image gray histogram", 2, "m")
# 7 创建mask,计算位图,直方图
mask = np.zeros(img_gray.shape[:2], np.uint8)
mask[130:500, 600:1400] = 255 # 获取mask,并赋予颜色
img_mask_hist = cv2.calcHist([img_gray], [0], mask, [256], [0, 256]) # 计算mask的直方图
# 返回来一个给定形状和类型的用0填充的数组;zeros(shape, dtype=float, order=‘C’)
# shape: 形状 dtype: 数据类型,可选参数,默认numpy.float64 order: 可选参数,c代表与c语言类似,行优先;F代表列优先
# 8 通过位运算(与运算)计算带有mask的灰度图片
mask_img = cv2.bitwise_and(img_gray, img_gray, mask = mask)
# 9 显示带有mask的图片和直方图
show_image(mask_img, "gray image with mask", 3)
show_histogram(img_mask_hist, "histogram with masked gray image", 4, "m")
plt.show()
if __name__ == '__main__':
main()