Python各种图像库的图像的基本读写方式

目前主流的图像库有几下几种:

1. OpenCV      2. PIL(Pillow)       3. matplotlib.image     4. skimage      5. scipy.misc

结论:以上图片库中当属OpenCV最为强大,成熟。

 

1.1 OpenCV 图像的读取与储存

import cv2
#读取图像 直接是numpy矩阵格式
img = cv2.imread('horse.jpg',1) # 0表示读入灰色图片,1表示读入彩色图片
cv2.imshow('image',img) # 显示图像
print(img.shape)   # (height,width,channel)
print(img.size)    # 像素数量
print(img.dtype)   # 数据类型
print(img)         # 打印图像的numpy数组,3纬数组

#储存图像
# 当前目录储存
cv2.write(‘horse1.jpg',img)
# 自定义储存
cv2.write(‘/path_name/’ + str(image_name) + '.jpg',img)
          
cv2.waitKey()

1.2OpenCV 图像灰化处理

import cv2
#方法一
img = cv2.imread('horse.jpg',0) # 0表示读入灰色图片,或者使用cv2.IMREAD_GRATSCALE 替代0
cv2.imshow('gray image',img)

#方法二
img = cv2.imread('horse.jpg')
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray image',gray_img)

print(gray_img.shape) # (height, width)
print(gray_img.size)  # 像素数量
print(gray_img)       # 打印图像的numpy数组,2维
cv2.waitKey()

1.3 OpenCV 矩阵格式变换

Why?:OpenCV的矩阵格式 (height, width, channels) -->> 深度学习矩阵类型可能是 (channels,height,width)

import cv2
import numpy as np
img = cv2.imread('horse.jpg',1)
cv2.imshow('image',img)
# 矩阵格式的变换
print(img.shape)
img = img.transpose(2,0,1) #变换函数 
print(img.shape)
# 矩阵扩展 (batch_size, channels, height, width) 预测单张图片的操作
# 加一列作为图片的个数
img = np.expand_dims(img, axis=0) #使用numpy函数
print(img.shape)
# 训练阶段构建batch
data_lst = []
loop:
    img = cv2.imread('xxx.jpg')
    data_lst.append(img)
data_arr = np.array(data_lst)

1.4 OpenCV 图片归一化 (Data Normalization)

import cv2
# 为了减少计算量,需要把像素值0-255转换到0-1之间
img = cv2.imread('horse.jpg')
img = img.astype('float') / 255.0 # 先转化数据类型为float
print(img.dtype)
print(img)

1.5 OpenCV BRG转换为RGB

import cv2
img = cv2.imread('horse.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 转为RGB format
print(img)

1.6 OpenCV 访问像素点

import cv2
img = cv2.imread('horse.jpg')
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 转为Gray image
print(img[4,4])        # 3 channels
print(gray_img[4,4])   # 1 channel

1.7 OpenCV 感兴趣区域剪切(ROI)

import cv2
img = cv2.imread('horse.jpg')
print(img.shape)
roi = img[0:437,0:400] # [y:height,x:width]

cv2.imshow('roi',roi)
cv2.waitKey()

 

 

2.1 PIL 图像读取与储存

from PIL import Image
import numpy as np
#图像读取
img = Image.open('horse.jpg')
print(img.format) # 图片格式
print(img.size)   # (width,height)
print(img.mode)   # 图片通道类型

#将图像转化为矩阵格式
arr = np.array(img)
print(arr.shape)
print(arr.dtype)

#图像储存
new_img = Image.fromarray(arr)
new_img.save('test.jpg')

img.show()

2.2 PIL 图像灰化处理 

#图像灰化处理
gray = Image.open('horse.jpg').convert('L')
gray_arr = np.array(gray)
print(gray_arr.shape) # (height,width)
print(gray_arr.dtype)
print(gray_arr)
gray.show()

2.3 PIL 感兴趣区域剪切

# 感兴趣区域剪切
img = Image.open('horse.jpg')
roi = img.crop((0,0,200,200)) # (左上x,左上y,右下x,右下y)
roi.show()

2.4 通道操作

# 通道处理
r,g,b = img.split() #分离
img = Image.merge("RGB",(b,g,r)) #合并
img = img.copy() #复制

 

3.1 Matplotlib 读取和存储图片

import matplotlib.pyplot as plt
import numpy as np
# 图像读取为numpy数组格式
img = plt.imread('horse.jpg')

plt.axis('off') # 关闭刻度显示

print(img.shape) # (height, width, channel)
print(img.size) # 像素数量
print(img.dtype)

#储存图片
plt.savefig('./name.jpg')

figure = plt.figure(figsize=(20,10)) # 调整显示图片的大小

plt.imshow(img)
plt.show()

3.2 Matplotlib 图片灰化处理

#图片灰化处理
# 平均值发
img_mean = img.mean(axis=2)
plt.imshow(img_mean,cmap='gray')
plt.show()

#最大值法
img_max = img.max(axis=-1)
plt.imshow(img_max,cmap='gray')
plt.show()

#RGB三原色法
gravity = np.array([0.299,0.587,0.114])
img_gravity = np.dot(img,gravity)
plt.imshow(img_gravity,cmap="gray")
plt.show()

 

4.1 skimage 读取和储存图像

from skimage import io
#读取图像numpy数组格式
img = io.imread('horse.jpg')
print(img.shape)
print(img.dtype)
print(img.size)
#print(img)
io.imshow(img)

#储存图像
io.imsave('test.jpg',img)

4.2 skimage 灰化处理

#图像灰化处理并归一化
img = io.imread('horse.jpg',as_gray=True)
print(img.shape)
print(img.dtype) # 数据类型位float
print(img.size)
print(img)
io.imshow(img)
io.show()

 

5.1 scipy.misc 读取和储存图像

#在1.2.0 之后统一用imageio模块
import imageio
import matplotlib.pyplot as plt
#读取图片为numpy数组
img = imageio.imread('horse.jpg')
print(img.dtype)
print(img.size)  # 像素数量
print(img.shape) #(height, width, channels)
plt.imshow(img)
plt.show()
print(img)
#储存图片
imageio.imsave('test.jpg',img)

 

未完待续......

 

posted @ 2019-03-14 11:00  Witness_for_light  阅读(2397)  评论(0编辑  收藏  举报