Python中Matplotlib对不同灰度值范围图像显示

1. 显示时,灰度值转化到0~255

2. 对0~1的灰度值,转化到0~255,在显示。

3. 对小于0的灰度值,将其视为0,然后 转化为0~255,在显示。

import numpy as np
import torchvision
import torch
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
import os
import time
import matplotlib.pyplot as plt

data_dir="E:\PycharmProjects\PytorchLearn\DogsVSCats\\test1\\1.jpg"
image=plt.imread(data_dir)

image1=image/255.0

mean=image.mean()
std=image.std()
image2=(image-mean)/std

image3=image2.copy()
image3[image3<0]=0

plt.figure(1)
plt.imshow(image)

plt.figure(2)
plt.imshow(image1)

plt.figure(3)
plt.imshow(image2)

plt.figure(4)
plt.imshow(image3)

print("-"*10)
print(image)

print("-"*10)
print(image1)

print("-"*10)
print(image2)

print("-"*10)
print(image3)


plt.show()

 

 

C:\ProgramData\Anaconda3\python.exe E:/PycharmProjects/PytorchLearn/TransformLearning/test.py
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
----------
[[[ 37  64  94]
  [ 35  62  92]
  [ 34  60  93]
  ...
  [ 71 108 199]
  [ 49  86 175]
  [ 48  85 173]]

 [[ 41  68  98]
  [ 39  66  96]
  [ 37  63  96]
  ...
  [ 71 106 198]
  [ 48  85 174]
  [ 47  84 172]]

 [[ 45  72 102]
  [ 43  70 100]
  [ 40  66  99]
  ...
  [ 70 105 199]
  [ 48  83 173]
  [ 47  83 171]]

 ...

 [[165 169 170]
  [167 171 174]
  [171 174 179]
  ...
  [ 97  89  70]
  [ 85  77  54]
  [ 87  77  52]]

 [[168 172 173]
  [169 173 176]
  [172 175 180]
  ...
  [116 108  89]
  [105  97  74]
  [108  98  73]]

 [[173 177 178]
  [172 176 179]
  [172 175 180]
  ...
  [125 117  98]
  [115 107  84]
  [118 108  83]]]
----------
[[[0.14509804 0.25098039 0.36862745]
  [0.1372549  0.24313725 0.36078431]
  [0.13333333 0.23529412 0.36470588]
  ...
  [0.27843137 0.42352941 0.78039216]
  [0.19215686 0.3372549  0.68627451]
  [0.18823529 0.33333333 0.67843137]]

 [[0.16078431 0.26666667 0.38431373]
  [0.15294118 0.25882353 0.37647059]
  [0.14509804 0.24705882 0.37647059]
  ...
  [0.27843137 0.41568627 0.77647059]
  [0.18823529 0.33333333 0.68235294]
  [0.18431373 0.32941176 0.6745098 ]]

 [[0.17647059 0.28235294 0.4       ]
  [0.16862745 0.2745098  0.39215686]
  [0.15686275 0.25882353 0.38823529]
  ...
  [0.2745098  0.41176471 0.78039216]
  [0.18823529 0.3254902  0.67843137]
  [0.18431373 0.3254902  0.67058824]]

 ...

 [[0.64705882 0.6627451  0.66666667]
  [0.65490196 0.67058824 0.68235294]
  [0.67058824 0.68235294 0.70196078]
  ...
  [0.38039216 0.34901961 0.2745098 ]
  [0.33333333 0.30196078 0.21176471]
  [0.34117647 0.30196078 0.20392157]]

 [[0.65882353 0.6745098  0.67843137]
  [0.6627451  0.67843137 0.69019608]
  [0.6745098  0.68627451 0.70588235]
  ...
  [0.45490196 0.42352941 0.34901961]
  [0.41176471 0.38039216 0.29019608]
  [0.42352941 0.38431373 0.28627451]]

 [[0.67843137 0.69411765 0.69803922]
  [0.6745098  0.69019608 0.70196078]
  [0.6745098  0.68627451 0.70588235]
  ...
  [0.49019608 0.45882353 0.38431373]
  [0.45098039 0.41960784 0.32941176]
  [0.4627451  0.42352941 0.3254902 ]]]
----------
[[[-1.28147369 -0.85929993 -0.39021797]
  [-1.31274582 -0.89057206 -0.4214901 ]
  [-1.32838189 -0.92184419 -0.40585403]
  ...
  [-0.74984747 -0.17131305  1.25156889]
  [-1.09384091 -0.51530649  0.87630332]
  [-1.10947697 -0.53094256  0.84503119]]

 [[-1.21892943 -0.79675567 -0.32767371]
  [-1.25020156 -0.8280278  -0.35894584]
  [-1.28147369 -0.87493599 -0.35894584]
  ...
  [-0.74984747 -0.20258519  1.23593282]
  [-1.10947697 -0.53094256  0.86066725]
  [-1.12511304 -0.54657862  0.82939512]]

 [[-1.15638517 -0.7342114  -0.26512945]
  [-1.1876573  -0.76548354 -0.29640158]
  [-1.23456549 -0.8280278  -0.31203764]
  ...
  [-0.76548354 -0.21822125  1.25156889]
  [-1.10947697 -0.56221469  0.84503119]
  [-1.12511304 -0.56221469  0.81375906]]

 ...

 [[ 0.71994267  0.78248693  0.79812299]
  [ 0.7512148   0.81375906  0.86066725]
  [ 0.81375906  0.86066725  0.93884758]
  ...
  [-0.34330977 -0.4683983  -0.76548354]
  [-0.53094256 -0.65603108 -1.01566058]
  [-0.49967043 -0.65603108 -1.04693271]]

 [[ 0.76685086  0.82939512  0.84503119]
  [ 0.78248693  0.84503119  0.89193938]
  [ 0.82939512  0.87630332  0.95448365]
  ...
  [-0.04622453 -0.17131305 -0.4683983 ]
  [-0.21822125 -0.34330977 -0.70293927]
  [-0.17131305 -0.32767371 -0.71857534]]

 [[ 0.84503119  0.90757545  0.92321152]
  [ 0.82939512  0.89193938  0.93884758]
  [ 0.82939512  0.87630332  0.95448365]
  ...
  [ 0.09450006 -0.03058847 -0.32767371]
  [-0.0618606  -0.18694912 -0.54657862]
  [-0.0149524  -0.17131305 -0.56221469]]]
----------
[[[0.         0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]
  ...
  [0.         0.         1.25156889]
  [0.         0.         0.87630332]
  [0.         0.         0.84503119]]

 [[0.         0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]
  ...
  [0.         0.         1.23593282]
  [0.         0.         0.86066725]
  [0.         0.         0.82939512]]

 [[0.         0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]
  ...
  [0.         0.         1.25156889]
  [0.         0.         0.84503119]
  [0.         0.         0.81375906]]

 ...

 [[0.71994267 0.78248693 0.79812299]
  [0.7512148  0.81375906 0.86066725]
  [0.81375906 0.86066725 0.93884758]
  ...
  [0.         0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]]

 [[0.76685086 0.82939512 0.84503119]
  [0.78248693 0.84503119 0.89193938]
  [0.82939512 0.87630332 0.95448365]
  ...
  [0.         0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]]

 [[0.84503119 0.90757545 0.92321152]
  [0.82939512 0.89193938 0.93884758]
  [0.82939512 0.87630332 0.95448365]
  ...
  [0.09450006 0.         0.        ]
  [0.         0.         0.        ]
  [0.         0.         0.        ]]]
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Process finished with exit code 0

 

 

Python中Matplotlib对不同灰度值范围图像显示_第1张图片

你可能感兴趣的:(Python语法注意点)