Image 如何将{UINT16,2}数组保存到Julia中的图像

Image 如何将{UINT16,2}数组保存到Julia中的图像,image,julia,hdf5,image-size,ijulia-notebook,Image,Julia,Hdf5,Image Size,Ijulia Notebook,我在Julia中有一个数组{UInt16,2},大小为5328×3040。我想将其保存到png图像 我尝试了以下方法: save("gray.png", colorview(Gray, img)) 但出现以下错误: ERROR: TypeError: in Gray, in T, expected T<:Union{Bool, AbstractFloat, FixedPoint}, got Type{UInt16} Stacktrace: [1] ccolor_nu

我在Julia中有一个数组{UInt16,2},大小为5328×3040。我想将其保存到png图像

我尝试了以下方法:

save("gray.png", colorview(Gray, img))
但出现以下错误:

ERROR: TypeError: in Gray, in T, expected T<:Union{Bool, AbstractFloat, FixedPoint}, got Type{UInt16}
Stacktrace:
 [1] ccolor_number at C:\Users\ankushar\.julia\packages\ImageCore\KbJyT\src\convert_reinterpret.jl:60 [inlined]
 [2] ccolor_number at C:\Users\ankushar\.julia\packages\ImageCore\KbJyT\src\convert_reinterpret.jl:57 [inlined]
 [3] colorview(::Type{Gray}, ::Array{UInt16,2}) at C:\Users\ankushar\.julia\packages\ImageCore\KbJyT\src\colorchannels.jl:104
 [4] top-level scope at REPL[16]:1
caused by [exception 3]
IOError: symlink: operation not permitted (EPERM)

ERROR:TypeError:灰色,T,预期T您可以在保存之前对像素值进行规格化

using Images

img = rand(UInt16, 10, 20)

img[1:3]

# => 3-element Array{UInt16,1}:
 0x7fc2
 0x057e
 0xae79

gimg = colorview(Gray, img ./ typemax(UInt16))

gimg[1:3] |> channelview

# => 3-element reinterpret(Float64, ::Array{Gray{Float64},1}):
 0.4990615701533532
 0.02145418478675517
 0.6815442130159457

save("gray.png", gimg)


一个更快更准确的解决方案是将数组重新解释为
N0f16
数组,它是
FixedPointNumbers
中的一种类型,基本上只是一个
Uint16
0
1
之间缩放的数组。这既可以避免舍入错误,也可以避免复制

using FixedPointNumbers

img = rand(UInt16, 10, 20)
colorview(Gray, reinterpret(N0f16, img)))

您的值不应该介于0和1.0之间吗?您建议将值规格化为1.0?并将其更改为浮点型?如何
img=rand(UInt16,100200);colorview(灰色,img./maximum(img))
?@daycaster,谢谢你的指点。这对我有用。我使用了你提到的同样的公式。为了转换为图像,必须将其规格化为1.0。我尝试在此处使用reinterpret函数将Unit16更改为N0f16,但这导致在保存图像时出现空白图像。虽然除以最大值保存了我期望的图像。这里会出什么问题?