对来自各种“图像”的 numpy 数组进行标准化物体
请考虑这个可重现的示例:
from PIL import Image
import numpy as np
import scipy.misc as sm
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cbook as cbook
import urllib
datafile = cbook.get_sample_data('lena.jpg')
lena_pil = Image.open(datafile)
lena_pil_np = np.asarray(lena_pil)
lena_scipy = sm.lena()
lena_tmp = open('lena_tmp.png', 'wb')
lena_tmp.write(urllib.urlopen('http://optipng.sourceforge.net/pngtech/img/lena.png').read())
lena_tmp.close()
lena_mpl = mpimg.imread('lena_tmp.png')
sm.info(lena_pil_np)
sm.info(lena_scipy)
sm.info(lena_mpl)
输出为:
>>> sm.info(lena_pil_np)
class: ndarray
shape: (512, 512, 3)
strides: (1536, 3, 1)
itemsize: 1
aligned: True
contiguous: True
fortran: False
data pointer: 0xb707e01cL
byteorder: little
byteswap: False
type: uint8
>>> sm.info(lena_scipy)
class: ndarray
shape: (512, 512)
strides: (2048, 4)
itemsize: 4
aligned: True
contiguous: True
fortran: False
data pointer: 0xb6f7d008L
byteorder: little
byteswap: False
type: int32
>>> sm.info(lena_mpl)
class: ndarray
shape: (512, 512, 3)
strides: (6144, 12, 4)
itemsize: 4
aligned: True
contiguous: True
fortran: False
data pointer: 0xb6c7b008L
byteorder: little
byteswap: False
type: float32
因此所有数组都具有不同的形状和类型。
为了进行额外的处理,我希望这个数组在最后一个变量 lena.mpl
中表示,或者只是将数组值转换为其标准化的 [0..1] float32 类型。
最好的方法是什么?
please consider this reproducible example:
from PIL import Image
import numpy as np
import scipy.misc as sm
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cbook as cbook
import urllib
datafile = cbook.get_sample_data('lena.jpg')
lena_pil = Image.open(datafile)
lena_pil_np = np.asarray(lena_pil)
lena_scipy = sm.lena()
lena_tmp = open('lena_tmp.png', 'wb')
lena_tmp.write(urllib.urlopen('http://optipng.sourceforge.net/pngtech/img/lena.png').read())
lena_tmp.close()
lena_mpl = mpimg.imread('lena_tmp.png')
sm.info(lena_pil_np)
sm.info(lena_scipy)
sm.info(lena_mpl)
Output is:
>>> sm.info(lena_pil_np)
class: ndarray
shape: (512, 512, 3)
strides: (1536, 3, 1)
itemsize: 1
aligned: True
contiguous: True
fortran: False
data pointer: 0xb707e01cL
byteorder: little
byteswap: False
type: uint8
>>> sm.info(lena_scipy)
class: ndarray
shape: (512, 512)
strides: (2048, 4)
itemsize: 4
aligned: True
contiguous: True
fortran: False
data pointer: 0xb6f7d008L
byteorder: little
byteswap: False
type: int32
>>> sm.info(lena_mpl)
class: ndarray
shape: (512, 512, 3)
strides: (6144, 12, 4)
itemsize: 4
aligned: True
contiguous: True
fortran: False
data pointer: 0xb6c7b008L
byteorder: little
byteswap: False
type: float32
so all arrays are of different shape and type.
For additional processing I would like this arrays to be represented as in last variable lena.mpl
, or just to transform array values to their normalized [0..1] float32 type.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)