本章节为 图像数据 处理总结,其中包括图像的特征图像shape、灰度图等内容。
文本介绍关于数据分析工作中常用的 使用Python进行数据预处理 的方法总结。通过对图片数据、数值数字、文本数据、特征提取、特征处理等方面讲解作为一名数据分析师常用的数据处理套路。
import skimage import numpy as np import pandas as pd import matplotlib.pyplot as plt from skimage import io
cat = io.imread('./datasets/cat.png') dog = io.imread('./datasets/dog.png') df = pd.DataFrame(['Cat', 'Dog'], columns=['Image']) # 显示图片的维度 print(cat.shape, dog.shape) >>> (168, 300, 3) (168, 300, 3) cat #0-255,越小的值代表越暗,越大的值越亮 >>> array([[[114, 105, 90], [113, 104, 89], [112, 103, 88], ..., [127, 130, 121], [130, 133, 124], [133, 136, 127]], [[ 33, 27, 29], [ 32, 26, 28], [ 31, 25, 27], ..., [131, 131, 131], [131, 131, 131], [130, 130, 130]]], dtype=uint8) # PLT 显示图片 #coffee = skimage.transform.resize(coffee, (300, 451), mode='reflect') fig = plt.figure(figsize = (16,8)) ax1 = fig.add_subplot(1,2, 1) ax1.imshow(cat) ax2 = fig.add_subplot(1,2, 2) ax2.imshow(dog)
# 设置不同的色道 dog_r = dog.copy() # 红 dog_r[:,:,1] = dog_r[:,:,2] = 0 # set G,B pixels = 0 dog_g = dog.copy() # 绿 dog_g[:,:,0] = dog_r[:,:,2] = 0 # set R,B pixels = 0 dog_b = dog.copy() # 蓝 dog_b[:,:,0] = dog_b[:,:,1] = 0 # set R,G pixels = 0 plot_image = np.concatenate((dog_r, dog_g, dog_b), axis=1) plt.figure(figsize = (10,4)) plt.imshow(plot_image)
dog_r >>> array([[[160, 0, 0], [160, 0, 0], [160, 0, 0], ..., [113, 0, 0], [113, 0, 0], [112, 0, 0]], ...... [[164, 0, 0], [164, 0, 0], [164, 0, 0], ..., [209, 0, 0], [209, 0, 0], [209, 0, 0]]], dtype=uint8)
# 将图片转为黑白色 fig = plt.figure(figsize = (16,8)) ax1 = fig.add_subplot(2,2, 1) cat_ = Image.open('./datasets/cat.png') cat_ = cat_.convert("L") ax1.imshow(cat_, cmap="gray") ax2 = fig.add_subplot(2,2, 2) dog_ = Image.open('./datasets/dog.png') dog_ = dog_.convert("L") ax2.imshow(dog_, cmap='gray')