julia - 从包含数据类型 UInt8 的数组中绘制图像

标签 julia plots.jl

我有一堆(猫的)图片,想绘制其中一张。值图像采用 UInt8 格式并包含 3 个波段。当我尝试使用 plots 绘图时,出现以下错误,ERROR: StackOverflowError

 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 >

enter image description here

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

最佳答案

首先,我会注意您如何 reshape ;我认为这只会重新排列图像中的像素,而不是交换尺寸,这似乎是你想要做的。你可能想要 train_data_rsp = permutedims(train_data_x, (4, 2, 3, 1)) 这实际上会交换维度并给你一个 209×64×64×3 具有哪些像素属于哪些图像的语义的数组。

然后,Julia 的 Images包裹有一个 colorview功能,可让您将单独的 R、G、B channel 组合成单个图像。您首先需要将数组元素类型转换为 N0f8 (一种单字节格式,其中 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}:
[...]

然后您应该能够绘制此图像。

关于julia - 从包含数据类型 UInt8 的数组中绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67455116/

相关文章:

julia - Julia 中的自定义颜色梯度热图

c - Julia:Ptr{Void} 终结器错误

date - 将包含缺失值的 DataFrame String 列转换为 Julia 中的日期

Julia:如何将符号表达式转换为函数?

metaprogramming - 在运行时将变量保存到文件

julia - 在 Julia 中绘制函数

performance - 在 Julia 中更快地读取 CSV 文件

plot - 更改颜色条热图中的中断数量 Julia

plot - 如何在 Plots.jl 中获取绘图属性

julia - plots.jl 中是否有任何 ax.view_init(elev, azim) 等效函数?