将代码中图片改为灰度图import matplotlib.pyplot as plt import matplotlib.image as mping import numpy as np img = mping.imread('D:\作业\兵马俑.jpg') plt.figure(1) plt.imshow(img) a = img.shape[0] b = img.shape[1] # 只保留左半部分,右半部分置为白色 img2 = np.ones((a, b, 3), dtype=np.uint8) * 255 img2[:, :b//2, :] = img[:, :b//2, :] img2[:, b//2:, :] = 255 plt.figure(2) plt.imshow(img2) # 左右镜像 img3 = np.ones((a, b, 3), dtype=np.uint8) * 255 img3[:, :b//2, :] = img[:, :b//2, :] img3[:, b//2:, :] = np.fliplr(img[:, :b//2, :]) plt.figure(3) plt.imshow(img3)
时间: 2023-07-16 10:13:08 浏览: 97
Matplotlib.pyplot 三维绘图的实现示例
可以使用matplotlib中的cmap参数将彩色图片转换为灰度图。修改代码如下:
import matplotlib.pyplot as plt
import matplotlib.image as mping
import numpy as np
img = mping.imread('D:\作业\兵马俑.jpg')
gray_img = np.dot(img[...,:3], [0.299, 0.587, 0.114]) # 将彩色图片转换为灰度图
plt.figure(1)
plt.imshow(gray_img, cmap='gray')
a = gray_img.shape[0]
b = gray_img.shape[1]
# 只保留左半部分,右半部分置为白色
img2 = np.ones((a, b, 3), dtype=np.uint8) * 255
img2[:, :b//2, :] = img[:, :b//2, :]
img2[:, b//2:, :] = 255
plt.figure(2)
plt.imshow(img2)
# 左右镜像
img3 = np.ones((a, b, 3), dtype=np.uint8) * 255
img3[:, :b//2, :] = img[:, :b//2, :]
img3[:, b//2:, :] = np.fliplr(img[:, :b//2, :])
plt.figure(3)
plt.imshow(img3)
阅读全文