Julia 从包含数据类型UInt8的数组打印图像

Julia 从包含数据类型UInt8的数组打印图像,julia,plots.jl,Julia,Plots.jl,我有一堆(猫的)图片,想画一张。图像值的格式为UInt8,包含3个波段。当我尝试使用plots进行绘图时,会出现以下错误,错误:StackOverflowerError Using Plots # Get data train_data_x = fid["train_set_x"] |> HDF5.read #Out > 3×64×64×209 Array{UInt8, 4}: [:, :, 1, 1] = 0x11 0x16 0x19 0x19 0x

我有一堆(猫的)图片,想画一张。图像值的格式为
UInt8
,包含3个波段。当我尝试使用plots进行绘图时,会出现以下错误,
错误:StackOverflowerError

 Using Plots

# Get data
train_data_x = fid["train_set_x"] |> HDF5.read
#Out >
3×64×64×209 Array{UInt8, 4}:
[:, :, 1, 1] =
0x11  0x16  0x19  0x19  0x1b  …  0x01  0x01  0x01  0x01
0x1f  0x21  0x23  0x23  0x24     0x1c  0x1c  0x1a  0x16
0x38  0x3b  0x3e  0x3e  0x40     0x3a  0x39  0x38  0x33
...

# Reshape to be in the format, no_of_images x length x width x channels
train_data_rsp = reshape(train_data_x, (209,64,64,3))

# Get first image 
first_img = train_data_rsp[1, :, :, :]

plot(first_img)
Out >
ERROR: StackOverflowError:

# I also tried plotting one band and I get a line plot
plot(train_data_rsp[1,:,:,1])
#Out >


我的代码有什么不正确的地方吗?

首先,我要小心你如何重塑代码;我认为这只会重新排列图像中的像素,而不是交换尺寸,这似乎是你想要做的。您可能需要
train\u data\u rsp=permutedims(train\u data\u x,(4,2,3,1))
,它将实际交换周围的维度,并为您提供一个
209×64×64×3
数组,其中包含哪些像素属于哪些保留的图像的语义

然后,Julia的软件包有一个函数,可以将单独的R、G、B通道组合成一个图像。首先需要将数组元素类型转换为(单字节格式,其中0对应于0,255对应于1),以便
Images
可以使用它。它看起来像这样:

julia> arr_rgb = N0f8.(first_img // 255)  # rescale UInt8 in range [0,255] to Rational with denominator 255 in range [0,1]
64×64×3 Array{N0f8,3} with eltype N0f8:
[...]
julia> img = colorview(RGB, map(i->selectdim(arr_rgb, 3, i), 1:3)...)
64×64 mappedarray(RGB{N0f8}, ImageCore.extractchannels, view(::Array{N0f8,3}, :, :, 1), view(::Array{N0f8,3}, :, :, 2), view(::Array{N0f8,3}, :, :, 3)) with eltype RGB{N0f8}:
[...]

然后您应该能够打印此图像。

您可能希望加载ImageIO以显示图像,而不是使用打印来打印图像。您可能还需要检查图像文件存储格式,以确保重塑不会将图像混合在一起。