# 分别在三个颜色通道显示一副彩色图片
import cv2 as cv
import copy
# 从指定路径读入一副彩色图片
path = './nature.jpg'
img_source = cv.imread(path)
# 显示原图
cv.imshow('Source_image', img_source)
# 显示蓝色通道对应的颜色图
img = copy.deepcopy(img_source)
img[:, :, 1:] = 0 # 令红色和绿色通道的值变为0
img_blue = img
cv.imshow('img_blue', img_blue)
# 显示红色通道对应的颜色图
img = copy.deepcopy(img_source)
img[:, :, :2] = 0 # 令蓝色和绿色通道的值变为0
img_red = img
cv.imshow('img_red', img_red)
# 显示绿色通道对应的颜色图
img = copy.deepcopy(img_source)
img[:, :, 0] = 0 # 令蓝色通道的值变为0
img[:, :, 2] = 0 # 令绿色通道的值变为0
img_green = img
cv.imshow('img_green', img_green)
# 重新得到复原图像
img_recover = img_blue+img_green+img_red
cv.imshow('img_recover', img_recover)
# 程序停止等待按键
cv.waitKey(0)
# 在三维图像中显示一副灰度图片
from matplotlib import pyplot as plt
import cv2 as cv
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# 以灰度图的形式读入一副图片
path = './image.jpg'
img = cv.imread(path, cv.IMREAD_GRAYSCALE)
cv.imwrite('./imggray.jpg', img)
# 分别以图像的横纵坐标和每个像素点的灰度作为三维坐标
X = np.arange(0, img.shape[1])
Y = np.arange(img.shape[0], 0, -1)
Z = 255 - img
# 创建名为figure的图像对象
figure = plt.figure()
# 创建名为ax的Axes3D对象,并将figure作为其图像来源
ax = Axes3D(figure)
# 网格化数据
X, Y = np.meshgrid(X, Y)
# 创建一副曲面图
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap='rainbow')
# 显示图片
plt.show()