python深度学习库pytorch::transforms练习:opencv,scikit-image,PIL图像处理库比较

进行深度学习时,对图像进行预处理的过程是非常重要的,使用pytorch或者TensorFlow时需要对图像进行预处理以及展示来观看处理效果,因此对python中的图像处理框架进行图像的读取和基本变换的掌握是必要的,接下来python中几个基本的图像处理库进行纵向对比。

项目地址:https://github.com/Oldpan/Pytorch-Learn/tree/master/Image-Processing

比较的图像处理框架:

  • PIL
  • scikit-image
  • opencv-python
PIL:

由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了许多新特性,因此,我们可以直接安装使用Pillow。

摘自廖雪峰的官方网站

scikit-image

scikit-image is a collection of algorithms for image processing. It is available free of charge and free of restriction. We pride ourselves on high-quality, peer-reviewed code, written by an active community of volunteers.
摘自官网的介绍,scikit-image的更新还是比较频繁的,代码质量也很好。

opencv-python

opencv的大名就不要多说了,这个是opencv的python版


# Compare Image-Processing Modules
# Use Transforms Module of torchvision
#               &&&
# 对比python中不同的图像处理模块
# 并且使用torchvision中的transforms模块进行图像处理

# packages
from PIL import Image
from skimage import io, transform
import cv2

import torchvision.transforms as transforms
import matplotlib.pyplot as plt
%matplotlib inline

img_PIL = Image.open('./images/dancing.jpg')
img_skimage = io.imread('./images/dancing.jpg')
img_opencv = cv2.imread('./images/dancing.jpg')
img_plt = plt.imread('./images/dancing.jpg')

loader = transforms.Compose([
    transforms.ToTensor()])  # 转换为torch.tensor格式


print('The shape of \n img_skimage is {}\n img_opencv is {}\n img_plt is {}\n'.format(img_skimage.shape, img_opencv.shape, img_plt.shape))
print('The type of \n img_skimage is {}\n img_opencv is {}\n img_plt is {}\n'.format(type(img_skimage), type(img_opencv), type(img_plt)))

output:

The shape of 
 img_skimage is (444, 444, 3)
 img_opencv is (444, 444, 3)
 img_plt is (444, 444, 3)

The size of img_PIL is (444, 444) 
 The mode of img_PIL is RGB
The type of 
 img_skimage is <class 'numpy.ndarray'>
 img_opencv is <class 'numpy.ndarray'>
 img_plt is <class 'numpy.ndarray'>
 img_PIL if <class 'PIL.JpegImagePlugin.JpegImageFile'>
# 定义一个图像显示函数
def my_imshow(image, title=None):
    plt.imshow(image)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # 这里延时一下,否则图像无法加载


plt.figure()
my_imshow(img_skimage, title='img_skimage')
# 可以看到opencv读取的图像打印出来的颜色明显与其他不同
plt.figure()
my_imshow(img_opencv, title='img_opencv')
plt.figure()
my_imshow(img_plt, title='img_plt')

# opencv读出的图像颜色通道为BGR,需要对此进行转换
img_opencv = cv2.cvtColor(img_opencv, cv2.COLOR_BGR2RGB)
plt.figure()
my_imshow(img_opencv, title='img_opencv_new')

这里写图片描述

toTensor = transforms.Compose([transforms.ToTensor()])

# 尺寸变化、缩放
transform_scale = transforms.Compose([transforms.Scale(128)])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp, title='after_scale')

# 随机裁剪
transform_randomCrop = transforms.Compose([transforms.RandomCrop(32, padding=4)])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp, title='after_randomcrop')

# 随机进行水平翻转(0.5几率)
transform_ranHorFlip = transforms.Compose([transforms.RandomHorizontalFlip()])
temp = transform_scale(img_PIL)
plt.figure()
my_imshow(temp, title='after_ranhorflip')

# 随机裁剪到特定大小
transform_ranSizeCrop = transforms.Compose([transforms.RandomSizedCrop(128)])
temp = transform_ranSizeCrop(img_PIL)
plt.figure()
my_imshow(temp, title='after_ranSizeCrop')

# 中心裁剪
transform_centerCrop = transforms.Compose([transforms.CenterCrop(128)])
temp = transform_centerCrop(img_PIL)
plt.figure()
my_imshow(temp, title='after_centerCrop')

# 空白填充
transform_pad = transforms.Compose([transforms.Pad(4)])
temp = transform_pad(img_PIL)
plt.figure()
my_imshow(temp, title='after_padding')

# 标准化是在整个数据集中对所有图像进行取平均和均方差,演示图像数量过少无法进行此操作
# print(train_data.mean(axis=(0,1,2))/255)
# print(train_data.std(axis=(0,1,2))/255)
# transform_normal = transforms.Compose([transforms.Normalize()])

# Lamdba使用用户自定义函数来对图像进行剪裁
# transform_pad = transforms.Compose([transforms.Lambda()])

这里写图片描述
这里写图片描述

除了OpenCV之外,Python中还有许多其他的图像处理可以用于获取摄像头图像,例如Pillow、PILscikit-image等。这些通常也支持将图像转换为PyTorch张量,因此你可以使用任何一个你喜欢的来获取图像,并使用PyTorch模型进行解析。 下面是一个使用Pillow获取摄像头图像,并使用PyTorch模型进行解析的示例代码: ```python from PIL import Image import torch import torchvision.transforms as transforms # 创建摄像头对象 cam = cv2.VideoCapture(0) # 创建PyTorch的图像变换 transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) while True: # 读取摄像头图像 ret, frame = cam.read() if not ret: break # 将图像转换为Pillow图像 pil_image = Image.fromarray(frame) # 对图像进行变换和归一化 tensor = transform(pil_image).unsqueeze(0) # 使用PyTorch模型进行解析 output = model(tensor) # TODO: 在此处添加你想要进行的操作 # 显示图像 pil_image.show() if cv2.waitKey(1) == ord("q"): break cam.release() cv2.destroyAllWindows() ``` 在代码中,我们首先创建一个摄像头对象,并创建PyTorch的图像变换,将图像转换为Pillow图像,并对图像进行变换和归一化。然后,我们使用PyTorch模型对图像进行解析,你可以在此处添加你想要进行的操作。最后,我们使用Pillow的`show()`方法来显示图像,并使用`cv2.waitKey`等待按键事件。你可以根据实际情况修改代码中的图像变换和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老潘的博客

请老潘吃块饼干!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值