济南市住房建设网站,网页设计基础知识点总结,招标网怎么投标,wordpress猫咪主题文章目录 1. Opencv2. Matplotlib3. PIL4. 三者的区别和相互转换5. Torchvision 中的相关转换库5.1 ToPILImage([mode])5.2 ToTensor5.3 PILToTensor 1. Opencv
opencv的基本图像类型可以和numpy数组相互转化#xff0c;因此可以直接调用torch.from_numpy(img) 将图像转换成t… 文章目录 1. Opencv2. Matplotlib3. PIL4. 三者的区别和相互转换5. Torchvision 中的相关转换库5.1 ToPILImage([mode])5.2 ToTensor5.3 PILToTensor 1. Opencv
opencv的基本图像类型可以和numpy数组相互转化因此可以直接调用torch.from_numpy(img) 将图像转换成tensor
读取: imgcv2.imread(path) OpenCV读取图像后返回的是一个代表图像的numpy.ndarray采用的格式是(H,W,C)通道顺序为BGR, 取值范围[0,255], dtypeuint8。
import cv2
def read_img_cv(path):img_cvcv2.imread(path)return img_cv显示: cv2.imshow(name,img)
import cv2
def show_img_cv(img_cv):cv2.imshow(Image, img_cv)cv2.waitKey(0) # 暂停显示图片数字0代表按键后 0 ms执行保存: cv2.imwrite(path, img)
import cv2
def save_img_cv(img_cv,path):cv2.imwrite(path, img_cv) # 保存图片2. Matplotlib
matplotlib 是python仿照matlab绘图开发的图像绘制库。使用matplotlib绘图时可以读取tesnor和numpy数据类型。
读取: imgmpimg.imread(path)
如果是灰度图返回(H,W)形状的数组 如果是RGB图像返回(H, W, 3) 形状的数组,图片通道顺序为RGB 如果是RGBA图像返回(H.W, 4) 形状的数组, 图片通道顺序为RGBA
此外PNG 图像以浮点数组 (0-1) dtypefloat32的形式返回所有其他格式都作为 int 型数组dtypeuint8返回位深由具体图像决定。
import matplotlib.image as mpimg
def read_img_mat(path):img_matmpimg.imread(path)return img_mat显示: plt.imshow(img) plt.show()
显示彩色图
import matplotlib.pyplot as plt
# 如果在jupyter notebook中显示需要添加如下一行代码
%matplotlib inlinedef show_img_mat(img_mat):plt.imshow(img_mat)plt.axis(off)plt.show()显示灰度图 matplotlib显示图像默认以三通道显示图像我们需要在plt.imshow()里添加参数gray。
def show_img_gray(img_gray):plt.imshow(img_gray,cmapgray)plt.axis(off)plt.show()显示Image类型图片
def show_img_pil(img_pil):plt.imshow(img_pil)plt.axis(off)plt.show()保存: plt.imsave(name,img)
def save_img_pil(img_pil,name):plt.imsave(name,img_pil)3. PIL
PIL是python对于图像处理的基本库。 图像的模式如下图比如1: 二值图L灰度图P: 8位彩色图RGB24位彩色图每个通道8位例如jpg图像RGBA : 相比RGB多了alpha通道不透明度例如png图像 可以使用img.convert(mode) 转换模式。
读取 imgImage.open(path) 读到的是一个PIL.xxxImageFIie的类型。
import PIL
from PIL import Image
def read_img_pil(path):img_pilImage.open(path) # PIL Image 类型return img_pil显示image.show()
def show_img_pil(img_pil):img_pil.show()保存: image.save(path)
def save_img_pil(img_pil,path):img_pil.save(path)4. 三者的区别和相互转换 三者的区别
Opencv 的数据类型是Numpy数组通道顺序为BGRMatplotlib 的数据类型是Numpy数组, 通道顺序是RGBPIL 的数据类型是PIL.Image类通道顺序是RGB 三种图像处理库相互转换
Opencv和Matplotlib之间的相互转换
# cv-mat
def cv2mat(img_cv):img_matcv2.cvtColor(img_cv,cv2.COLOR_BGR2RGB) # 将颜色通道从BGR改变成RGB# 另一种等价写法# img_matimg_cv[:,:,::-1]return img_matdef mat2cv(img_mat): # 将颜色通道从RGB改变成BGRimg_cvimg_mat[:,:,::-1]return img_cvMatplotlib和PIL之间的相互转换 np.asarry(img) img-array Image.fromarray(array) array-img
# mat-PIL
#方法1三通道的转换
def mat2PIL_RGB(img_mat):img_pilImage.fromarray(img_mat.astype(uint8))# unit8 是无符号的8位整形,用astype [0,255]截断处理# 另外一种写法# img_pil Image.fromarray(np.unit8(img_mat))return img_pil # 方法2 四通道的转换
def mat2PIL_RGBA(img_mat):img_pilImage.fromarray(img_mat.astype(uint8)).convert(RGB)return img_pil# 方法三使用torchvision的库函数
from torchvision import transforms
def mat2PIL_trans(img_mat):transtransformers.ToPILImage()img_piltrans(img_mat)return img_pilPIL-matdef PIL2mat(img_pil):img_matnp.array(img_pil) # 深拷贝# 如果是jpg格式通道顺序是RGB, (H,W,3)# 如果是png格式通道顺序是RGBA, (H,W,4)# 返回的类型均是numpy.ndarray, dtypeunit8, 取值返回[0,255]# 或者也可以采用浅拷贝# img_matnp.asarray(img_pil)return img_mat区间变换
# [0,255]-[0,1]
def PIL2mat_norm(img_pil):img_matnp.asarray(img_pil)/255.0return img_mat
# [0,1]-[0,255]
def mat_255(img_mat):img_mat(np.maximum(img_mat, 0) / img_mat.max()) * 255.0 img_matnp.unit8(img_mat)Opencv和PIL之间的相互转换
# cv-PIL
#方法1三通道的转换
def cv2PIL_RGB(img_cv):img_rgb img_cv[:,:,::-1] # OpenCV 的通道顺序为 BGR, 转换成RGB# nparray img_pil Image.fromarray(np.uint8(img_rgb))return img_pil # 方法2 四通道的转换
def cv2PIL_RGBA(img_cv):img_rgb img_cv[:,:,::-1]img_pilImage.fromarray(img_rgb.astype(uint8)).convert(RGB)return img_pil# 方法三使用torchvision的库函数
from torchvision import transforms
def cv2PIL_trans(img_cv):img_rgb img_cv[:,:,::-1]transtransformers.ToPILImage()img_piltrans(img_rgb)return img_pil# PIL-cv
def PIL2cv(img_pil):img_arynp.array(img_pil) # 深拷贝通道顺序是 RGB, (H,W,C)# 或者也可以采用深拷贝# img_arynp.asarray(img_pil)img_cvimg_ary[:,:,-1]return img_cv三种格式和Tensor之间的相互转换
numpy格式转成Tensor
import torch
def nparray2tensor(npary):tstorch.from_numpy(npary)# 如果需要修改成浮点类型# tstorch.from_numpy(npary).float()return tsPIL和numpy格式转成Tensor 可以利用torchvision 中transforms.ToTensor() 该函数可以将PIL 中的Image 或者 numpy.ndarray(dtypeunit8): 大小 (H,W,C) 、范围[0,255] 转化成torch.FloatTensor: 大小(C,H,W)、范围[0.0,1.0]
from torchvision import transforms
# img_pil: Image
transtransforms.ToTensor()
tenstrans(img_pil) # (C,H,W) [0.0,1,0]
# tens_hwctens.transpose((1,2,0))5. Torchvision 中的相关转换库
5.1 ToPILImage([mode])
CLASS
torchvision.transforms.ToPILImage(modeNone)功能 将tensor或ndarray转换为PIL图像——这不会缩放值。这个转换不支持torchscript。 转换形状为C x H x W的torch.*Tensor或形状为H x W x C的numpy ndarray到PIL图像同时保留值范围。 参数 mode(PIL.Image mode) 输入数据的颜色空间和像素深度(可选)。mode为None时(默认)对输入数据有如下假设 : 输入为4通道时假设模式为RGBA。如果输入为3通道则假设为RGB模式。输入为2路时假设为LA模式。如果输入有1个通道模式由数据类型(即int、float、short)确定。
5.2 ToTensor
CLASS
torchvision.transforms.ToTensor功能 将PIL图像或ndarray转换为tensor并相应地缩放。这个转换不支持torchscript。 转换PIL Image或在[0,255]区间内的numpy.ndarray (H x W x C) 到[0.0,1.0]区间内的torch.FloatTensor (C x H x W)。其中PIL Image属于其中一种模式(L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)如果numpy.Ndarray的dtype np.uint8 在其他情况下张量在不缩放的情况下返回。
5.3 PILToTensor
CLASS
torchvision.transforms.PILToTensor功能 将PIL图像转换为相同类型的张量-这不会缩放值。这个转换不支持torchscript。 将PIL Image (H x W x C)转换为形状(C x H x W)的张量。