微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从包含数据类型 UInt8 的数组中绘制图像

我有一堆(猫的)图像,想绘制其中一个。值图像采用 UInt8 格式并包含 3 个波段。当我尝试使用绘图进行绘图时,出现以下错误 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,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 通道组合成单个图像。您首先需要将数组元素类型转换为 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,i),1:3)...)
64×64 mappedarray(RGB{N0f8},ImageCore.extractchannels,view(::Array{N0f8,3},:,1),2),3)) with eltype RGB{N0f8}:
[...]

那么您应该能够绘制此图像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。