【笔记】input data to the valid range for imshow with RGB data [0..1] for floats or [0.255] for integers

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

eg1 :  float 超出 0-1范围

import numpy as np
import matplotlib.pyplot as plt
import torch as t
import cv2
# image1=cv2.imread(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg")
image1=cv2.imdecode(np.fromfile(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg",dtype=np.uint8),-1)
print(image1.shape)
image1=t.tensor(image1,dtype=t.float32).permute(2,0,1).permute(1,2,0)
print(image1[:,:,2])
image1=np.array(image1)
b,g,r=cv2.split(image1)
image1=cv2.merge([r,g,b])
print(image1[:,:,0])
plt.imshow(image1)
plt.show()

D:\Anaconda\envs\deep_learning\python.exe E:/classification-pytorch-main/classification-pytorch-main/utils/dataloader.py
(374, 500, 3)
tensor([[203., 203., 204.,  ..., 240., 239., 238.],
        [203., 203., 204.,  ..., 241., 240., 238.],
        [203., 203., 204.,  ..., 241., 240., 239.],
        ...,
        [153., 153., 153.,  ...,   2.,   2.,   2.],
        [152., 152., 152.,  ...,   2.,   2.,   2.],
        [151., 151., 151.,  ...,   1.,   1.,   1.]])
[[203. 203. 204. ... 240. 239. 238.]
 [203. 203. 204. ... 241. 240. 238.]
 [203. 203. 204. ... 241. 240. 239.]
 ...
 [153. 153. 153. ...   2.   2.   2.]
 [152. 152. 152. ...   2.   2.   2.]
 [151. 151. 151. ...   1.   1.   1.]]
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

eg2: float 回到0-1范围

import numpy as np
import matplotlib.pyplot as plt
import torch as t
import cv2
# image1=cv2.imread(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg")
image1=cv2.imdecode(np.fromfile(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg",dtype=np.uint8),-1)
print(image1.shape)
image1=t.tensor(image1,dtype=t.float32).permute(2,0,1).permute(1,2,0)
print(image1[:,:,2])
image1=np.array(image1)
b,g,r=cv2.split(image1)
image1=cv2.merge([r,g,b])/255
print(image1[:,:,0])
plt.imshow(image1)
plt.show()
D:\Anaconda\envs\deep_learning\python.exe E:/transformer_1/classification-pytorch-main/classification-pytorch-main/utils/dataloader.py
(374, 500, 3)
tensor([[203., 203., 204.,  ..., 240., 239., 238.],
        [203., 203., 204.,  ..., 241., 240., 238.],
        [203., 203., 204.,  ..., 241., 240., 239.],
        ...,
        [153., 153., 153.,  ...,   2.,   2.,   2.],
        [152., 152., 152.,  ...,   2.,   2.,   2.],
        [151., 151., 151.,  ...,   1.,   1.,   1.]])
[[0.79607844 0.79607844 0.8        ... 0.9411765  0.9372549  0.93333334]
 [0.79607844 0.79607844 0.8        ... 0.94509804 0.9411765  0.93333334]
 [0.79607844 0.79607844 0.8        ... 0.94509804 0.9411765  0.9372549 ]
 ...
 [0.6        0.6        0.6        ... 0.00784314 0.00784314 0.00784314]
 [0.59607846 0.59607846 0.59607846 ... 0.00784314 0.00784314 0.00784314]
 [0.5921569  0.5921569  0.5921569  ... 0.00392157 0.00392157 0.00392157]]

eg3: int 保持在 0-255

import numpy as np
import matplotlib.pyplot as plt
import torch as t
import cv2
# image1=cv2.imread(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg")
image1=cv2.imdecode(np.fromfile(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg",dtype=np.uint8),-1)
print(image1.shape)
image1=t.tensor(image1,dtype=t.int32).permute(2,0,1).permute(1,2,0)
print(image1[:,:,2])
image1=np.array(image1)
b,g,r=cv2.split(image1)
image1=cv2.merge([r,g,b])
print(image1[:,:,0])
plt.imshow(image1)
plt.show()
D:\Anaconda\envs\deep_learning\python.exe E:/transformer_1/classification-pytorch-main/classification-pytorch-main/utils/dataloader.py
(374, 500, 3)
tensor([[203, 203, 204,  ..., 240, 239, 238],
        [203, 203, 204,  ..., 241, 240, 238],
        [203, 203, 204,  ..., 241, 240, 239],
        ...,
        [153, 153, 153,  ...,   2,   2,   2],
        [152, 152, 152,  ...,   2,   2,   2],
        [151, 151, 151,  ...,   1,   1,   1]], dtype=torch.int32)
[[203 203 204 ... 240 239 238]
 [203 203 204 ... 241 240 238]
 [203 203 204 ... 241 240 239]
 ...
 [153 153 153 ...   2   2   2]
 [152 152 152 ...   2   2   2]
 [151 151 151 ...   1   1   1]]

eg4:  int 转化为 0-1 float



import numpy as np
import matplotlib.pyplot as plt
import torch as t
import cv2
# image1=cv2.imread(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg")
image1=cv2.imdecode(np.fromfile(r"..\..\..\数据集和预训练模型\datasets\datasets\train\cat\cat.0.jpg",dtype=np.uint8),-1)
print(image1.shape)
image1=t.tensor(image1,dtype=t.int32).permute(2,0,1).permute(1,2,0)
print(image1[:,:,2])
image1=np.array(image1)
b,g,r=cv2.split(image1)
image1=cv2.merge([r,g,b])/255
print(image1[:,:,0])
plt.imshow(image1)
plt.show()
D:\Anaconda\envs\deep_learning\python.exe E:/transformer_1/classification-pytorch-main/classification-pytorch-main/utils/dataloader.py
(374, 500, 3)
tensor([[203, 203, 204,  ..., 240, 239, 238],
        [203, 203, 204,  ..., 241, 240, 238],
        [203, 203, 204,  ..., 241, 240, 239],
        ...,
        [153, 153, 153,  ...,   2,   2,   2],
        [152, 152, 152,  ...,   2,   2,   2],
        [151, 151, 151,  ...,   1,   1,   1]], dtype=torch.int32)
[[0.79607843 0.79607843 0.8        ... 0.94117647 0.9372549  0.93333333]
 [0.79607843 0.79607843 0.8        ... 0.94509804 0.94117647 0.93333333]
 [0.79607843 0.79607843 0.8        ... 0.94509804 0.94117647 0.9372549 ]
 ...
 [0.6        0.6        0.6        ... 0.00784314 0.00784314 0.00784314]
 [0.59607843 0.59607843 0.59607843 ... 0.00784314 0.00784314 0.00784314]
 [0.59215686 0.59215686 0.59215686 ... 0.00392157 0.00392157 0.00392157]]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值