本部分内容所用的数据放在百度网盘:
链接: https://pan.baidu.com/s/1DXKTofRWufhVpP_HEQKk9A 提取码: pl5u
下载到本地,置于代码文件同级目录的新建文件夹“images”即可。
from skimage.io import imread
from skimage.color import rgb2gray
import matplotlib.pylab as plt
im = imread("images/parrot.png")
im_g = rgb2gray(im)
#print(im.shape)
plt.figure(figsize=(10,10))
plt.subplot(121)
plt.imshow(im, cmap='gray')
plt.axis('off')
plt.subplot(122)
plt.imshow(im_g, cmap='gray')
plt.axis('off')
plt.show()
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open('images/parrot.png') #open image
r, g, b = image.split()
plt.figure(figsize=(10,8))
plt.subplot(2,2,1), plt.imshow(image),plt.axis('off'),plt.title('Origin')
plt.subplot(2,2,2), plt.imshow(r,cmap='gray'),plt.axis('off'),plt.title('R Channel')
plt.subplot(2,2,3), plt.imshow(g,cmap='gray'),plt.axis('off'),plt.title('G Channel')
plt.subplot(2,2,4), plt.imshow(b,cmap='gray'),plt.axis('off'),plt.title('B Channel')
plt.show()
from PIL import Image
import matplotlib.pyplot as plt
image = Image.open('images/parrot.png') #open image
imgage_g = image.convert('L')
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(image),plt.axis('off'),plt.title('Origin')
plt.subplot(1,2,2), plt.imshow(r,cmap='gray'),plt.axis('off'),plt.title('gray image')
plt.show()
import matplotlib.pylab as plt
import matplotlib.image as mpimg
im = mpimg.imread("images/parrot.png")
im_g = im[:,:,0]
plt.figure(figsize=(10,8))
plt.subplot(1,2,1), plt.imshow(im),plt.axis('off'),plt.title('Origin')
plt.subplot(1,2,2), plt.imshow(im_g,cmap='gray'),plt.axis('off'),plt.title('gray image')
plt.show()
from matplotlib.image import imread
im = np.mean(imread('images/parrot.png'), axis=2) # 通过求每个像素向量的均值获得灰度图像
print(im.shape)
plt.imshow(im,cmap='gray')
plt.axis('off')
plt.show()