简体   繁体   English

这是什么数据格式? 以及如何从 numpy 数组转换?

[英]What is this data format? and how to convert from numpy array?

I tried to do a pattern recognition assignment using this data format in txtfile.我尝试在 txtfile 中使用这种数据格式进行模式识别分配。

3.700000000000000000e+01 3.700000000000000000e+01

But i only managed to wrote this.但我只设法写了这个。

import numpy
import PIL

# Convert Image to array
img = PIL.Image.open("imagefilename.png").convert("L")
arr = numpy.array(img)

and the output i get in array is this.我在数组中得到的输出是这个。

[[ 85  85  86 ..., 194 196 194]
 [ 84  84  85 ..., 194 196 194]
 [ 84  85  86 ..., 193 195 195]
 ..., 
 [177 177 177 ..., 162 162 162]
 [174 174 173 ..., 163 163 163]
 [  1   1   1 ...,   0   0   0]]

so my question is what is this format?所以我的问题是这种格式是什么? 3.700000000000000000e+01 which follow my lecturer's sample in txt file. 3.700000000000000000e+01遵循我讲师在txt文件中的示例。

and how to convert it from an RGB image?以及如何将其从 RGB 图像转换?

That is most probably scientific notation:这很可能是科学记数法:

3.7e+01 = 3.7 * 10**1
2.8e+03 = 2.8 * 10**3 

etc. See here for the other way round: Display a decimal in scientific notation and here for more explanation: https://en.wikipedia.org/wiki/Scientific_notation等。另一种方式参见此处: 以科学记数法显示小数,此处了解更多解释: https : //en.wikipedia.org/wiki/Scientific_notation

If you put it into a variable, python will know it:如果你把它放到一个变量中,python就会知道:

d = 3.7e6
print(d)

Output:输出:

3700000.0  

As for how to convert it to an image: it is a number - unless you have much more of them they won't make an image.至于如何将其转换为图像:它是一个数字 - 除非您有更多的数字,否则它们不会制作图像。

Most image formats I know of use triplets/quadruplets of ints (RGB/RGBA) or/and discrete values for widht*height.我知道的大多数图像格式都使用整数(RGB/RGBA)的三元组/四元组或/和宽度*高度的离散值。

You can transform the byte values into floats if you prefer to work with them that way, but my hedged guess is they only do value/255.0 to achive this.如果您更喜欢以这种方式使用它们,您可以将字节值转换为浮点数,但我对冲的猜测是它们只使用value/255.0来实现这一点。

Read more here: http://scikit-image.org/docs/dev/user_guide/data_types.html在此处阅读更多信息: http : //scikit-image.org/docs/dev/user_guide/data_types.html


To convert from np array on ints to np array of floats:要将整数上的 np 数组转换为浮点数的 np 数组:

import numpy as np

arr = np.array([5,7,200,255])
print(arr)

arr = arr / 255.0
print (arr)

Output:输出:

[  5   7 200 255]
[ 0.01960784  0.02745098  0.78431373  1.        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM