从包含数据类型 UInt8 的数组中绘制图像
Plotting images from array containing datatype UInt8
我有一堆(猫的)图片,想绘制其中一张。值图像采用 UInt8
格式并包含 3 个波段。当我尝试使用 plots 绘图时,出现以下错误,ERROR: WhosebugError
.
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: WhosebugError:
# I also tried plotting one band and I get a line plot
plot(train_data_rsp[1,:,:,1])
#Out >
我的代码有什么不正确的想法吗?
首先,我会注意你reshape
的表现;我认为这只会重新排列图像中的像素,而不是交换尺寸,这似乎是你想要做的。您可能需要 train_data_rsp = permutedims(train_data_x, (4, 2, 3, 1))
,它实际上会交换维度并为您提供一个 209×64×64×3
数组,其中包含哪些像素属于哪些图像的语义。
然后,Julia 的 Images
package has a colorview
function that lets you combine the separate R,G,B channels into a single image. You'll first need to convert your array element type into 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}:
[...]
那么你应该可以绘制这张图了。
我有一堆(猫的)图片,想绘制其中一张。值图像采用 UInt8
格式并包含 3 个波段。当我尝试使用 plots 绘图时,出现以下错误,ERROR: WhosebugError
.
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: WhosebugError:
# I also tried plotting one band and I get a line plot
plot(train_data_rsp[1,:,:,1])
#Out >
我的代码有什么不正确的想法吗?
首先,我会注意你reshape
的表现;我认为这只会重新排列图像中的像素,而不是交换尺寸,这似乎是你想要做的。您可能需要 train_data_rsp = permutedims(train_data_x, (4, 2, 3, 1))
,它实际上会交换维度并为您提供一个 209×64×64×3
数组,其中包含哪些像素属于哪些图像的语义。
然后,Julia 的 Images
package has a colorview
function that lets you combine the separate R,G,B channels into a single image. You'll first need to convert your array element type into 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}:
[...]
那么你应该可以绘制这张图了。