cv2 和 matplotlib.pyplot 和 PIL.Image 读取图片方式对比【Python读取图片】
【摘要】
文章目录
import matplotlib.pyplot as plt 和 cv2 读取图像对比PIL 和 cv2 读取图片对比mxnet.image.imread 读取图片PIL 和 torc...
import matplotlib.pyplot as plt 和 cv2 读取图像对比
- cv2.imread(imagepath) 读取图片默认为 BGR mode 的 numpy 格式
- matplotlib.pyplot.imread(imagepath) 默认为 RGB mode 的 numpy 格式
import matplotlib.pyplot as plt
import numpy as np
import cv2
imagepath = 'dog.jpg'
print(" matplotlib.pyplot 读取图像 RGB \n")
plot_img = plt.imread(imagepath)
print(type(plot_img))
print(plot_img.size)
print(plot_img.shape)
print(plot_img[0])
plt.imsave("plt_img.png",plot_img)
print(" \n plot_img RGB 转 BGR \n")
plot_img2BGR_CV = cv2.cvtColor(plot_img, cv2.COLOR_RGB2BGR)
print(plot_img2BGR_CV[0])
print(" opencv 读取图像 BGR \n")
CV_img = cv2.imread(imagepath)
print(type(CV_img))
print(CV_img.shape)
print(CV_img[0])
cv2.imwrite("CV_img.png",CV_img)
exit(0)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
输出如下
matplotlib.pyplot 读取图像 RGB
<class 'numpy.ndarray'>
1327104
(576, 768, 3)
[[ 57 58 50]
[ 58 59 51]
[ 60 61 53]
...
[143 89 43]
[ 89 49 41]
[ 65 70 47]]
plot_img RGB 转 BGR
[[ 50 58 57]
[ 51 59 58]
[ 53 61 60]
...
[ 43 89 143]
[ 41 49 89]
[ 47 70 65]]
opencv 读取图像 BGR
<class 'numpy.ndarray'>
(576, 768, 3)
[[ 50 58 57]
[ 51 59 58]
[ 53 61 60]
...
[ 47 89 142]
[ 41 50 88]
[ 47 71 63]]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
PIL 和 cv2 读取图片对比
- cv2.imread(imagepath) 读取图片默认为 BGR mode 的 numpy 格式
- PIL.Image.open(imagepath) 打开图片,是 RGB mode 的图片格式
- PIL.Image.open 读取的图片,转 numpy 再 RGB2BGR 则和 cv2.imread 一致
from PIL import Image
import cv2
imagepath = 'dog.jpg'
# opencv 读取图像
CV_img = cv2.imread(imagepath)
print(type(CV_img))
print(CV_img.shape) # (h、w、c)
print(CV_img[0])
# PIL.Image 读取图片
PIL_img = Image.open(imagepath)
print(type(PIL_img))
print(PIL_img.size) # (w、h)
print(PIL_img.mode)
PIL_img2np = np.array(PIL_img)
print(PIL_img2np[0])
print(PIL_img2np.shape)
print("PIL_img 转 numpy 再 RGB2BGR 之后: ")
BRG_CV = cv2.cvtColor(PIL_img2np, cv2.COLOR_RGB2BGR)
print(BRG_CV[0])
exit(0)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
输出如下
# cv2.imread(imagepath)
<class 'numpy.ndarray'>
(576, 768, 3)
[[ 50 58 57]
[ 51 59 58]
[ 53 61 60]
...
[ 47 89 142]
[ 41 50 88]
[ 47 71 63]]
#PIL Image.open(imagepath)
<class 'PIL.JpegImagePlugin.JpegImageFile'>
(768, 576)
RGB
[[ 57 58 50]
[ 58 59 51]
[ 60 61 53]
...
[143 89 43]
[ 89 49 41]
[ 65 70 47]]
(576, 768, 3)
# PIL_img 转 numpy 再 RGB2BGR 之后:
[[ 50 58 57]
[ 51 59 58]
[ 53 61 60]
...
[ 43 89 143]
[ 41 49 89]
[ 47 70 65]]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
调试分析如下
mxnet.image.imread 读取图片
import mxnet as mx
path= 'dog.jpg'
# 该方法 内部使用 openCV 读取图像 ; 那么读取的图 应该会是 BGR 格式 【盲猜】
img = mx.image.imread(path)
print(img.shape)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
PIL 和 torchvision.transforms 预处理图像 【pytorch】
from torchvision import transforms
from PIL import Image
imagepath = 'dog.jpg'
# PIL.Image 读取图片
PIL_img = Image.open(imagepath)
test_transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
print(type(PIL_img))
print(PIL_img)
print(PIL_img.size) # (w,h)
print(PIL_img.mode)
# 经过 torchvision.transforms 预处理之后的数据类型 是一个 Tensor
image = test_transform(PIL_img)
print(type(image))
print(image.shape)
exit(0)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
输出如下
<class 'PIL.JpegImagePlugin.JpegImageFile'>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x576 at 0x7FF0FA173350>
(768, 576)
RGB
<class 'torch.Tensor'>
torch.Size([3, 256, 256])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
pytorch 后处理图片常用方法
这里用到了 scipy 【代码摘自 – https://github.com/alpc91/NICE-GAN-pytorch/blob/master/utils.py】
from scipy import misc
import os, shutil, cv2, torch
import numpy as np
def load_test_data(image_path, size=256):
img = misc.imread(image_path, mode='RGB')
img = misc.imresize(img, [size, size])
img = np.expand_dims(img, axis=0)
img = preprocessing(img)
return img
def preprocessing(x):
x = x/127.5 - 1 # -1 ~ 1
return x
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def inverse_transform(images):
return (images+1.) / 2
def imsave(images, size, path):
return misc.imsave(path, merge(images, size))
def merge(images, size):
h, w = images.shape[1], images.shape[2]
img = np.zeros((h * size[0], w * size[1], 3))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[h*j:h*(j+1), w*i:w*(i+1), :] = image
return img
def check_folder(log_dir):
if os.path.exists(log_dir):
shutil.rmtree(log_dir)
os.makedirs(log_dir)
return log_dir
def str2bool(x):
return x.lower() in ('true')
def cam(x, size = 256):
x = x - np.min(x)
cam_img = x / np.max(x)
cam_img = np.uint8(255 * cam_img)
cam_img = cv2.resize(cam_img, (size, size))
cam_img = cv2.applyColorMap(cam_img, cv2.COLORMAP_JET)
return cam_img / 255.0
def imagenet_norm(x):
mean = [0.485, 0.456, 0.406]
std = [0.299, 0.224, 0.225]
mean = torch.FloatTensor(mean).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device)
std = torch.FloatTensor(std).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device)
return (x - mean) / std
def denorm(x):
return x * 0.5 + 0.5
def tensor2numpy(x):
return x.detach().cpu().numpy().transpose(1,2,0)
def RGB2BGR(x):
return cv2.cvtColor(x, cv2.COLOR_RGB2BGR)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
pytorch 模型实例化 推理示例
只是一个示例整理,无法运行 【用到了上面预处理和后处理相关方法】
from PIL import Image
import os
from model import WaterRemove
# 实例化 模型对象
gan = niceGAN(args)
gan.build_model()
gan.load()
# 读取数据
img_path = 'dataset/test25/0.jpg'
image = Image.open(img_path)
# 预处理
image = test_transform(img).unsqueeze(0)
# torch 模型 推理 输入为 Tensor , 这里的输出为 Tensor 格式图像
fake_A2B = gan.deal(image)
# 后处理[ 解析 Tensor ]
A2B = RGB2BGR(tensor2numpy(denorm(fake_A2B[0])))
res_img = cv2.resize(A2B * 255.0, image.size)
cv2.imwrite(os.path.join('results/test_cv.jpg'), res_img)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
持续更新…
文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。
原文链接:positive.blog.csdn.net/article/details/120055469
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)