如何使用 Julia 在 Jupyter Notebook 中显示 (224, 224, 3) 图像?

How to display a (224, 224, 3) image in a Jupyter Notebook with Julia?

我有一张 224x224 的图像,有 3 个通道。我想在 Jupyter Notebook 中显示图像。当我执行 IJulia.display(imgs[:, :, :, 1]) 时,我得到的只是原始数字,而不是实际图像。该代码确实适用于 ImageView.imshow 但我想在笔记本本身中捕获图像。我需要对我的数据做些什么才能将其显示为图像?

224×224×3 Array{Float32, 3}:
[:, :, 1] =
 0.117647   0.117647   0.117647   0.117647   …  0.384314  0.396078  0.403922
 0.117647   0.117647   0.117647   0.117647      0.384314  0.392157  0.4
 0.117647   0.117647   0.117647   0.117647      0.384314  0.388235  0.392157
 0.121569   0.117647   0.121569   0.121569      0.388235  0.388235  0.388235
 0.12549    0.117647   0.12549    0.129412      0.396078  0.396078  0.396078
 0.12549    0.117647   0.12549    0.129412   …  0.407843  0.407843  0.407843
 0.12549    0.117647   0.12549    0.129412      0.423529  0.423529  0.423529
 0.145098   0.133333   0.129412   0.129412      0.419608  0.419608  0.419608
 0.14902    0.141176   0.137255   0.137255      0.407843  0.407843  0.407843
 0.141176   0.145098   0.141176   0.137255      0.396078  0.396078  0.396078
 ⋮                                           ⋱                     

您只需将形状重塑为 3x224x224。

这会策划一些事情

using Images
Images.colorview(RGB, rand(3,224,224))

这将return一个错误

using Images
Images.colorview(RGB, rand(224,224,3))

因此,如果您有一个格式为 (i,j,3) 的数组 x,您希望首先将其重塑为 (3,i,j),然后使用 colorview。

顺便说一句,重塑和排列维度之间存在差异,您会从中得到不同的结果。

这是一个数组,其中最终维度的值介于 0 和 1 之间。

using Images

img = Array{Float32}(undef, 5, 5, 3)
img[:, :, 1] = range(0, 1, length= 5^2)
img[:, :, 2] = range(0, 1, length= 5^2)
img[:, :, 3] = range(0, 1, length= 5^2)
5×5×3 Array{Float32, 3}:
[:, :, 1] =
 0.0        0.208333  0.416667  0.625     0.833333
 0.0416667  0.25      0.458333  0.666667  0.875
 0.0833333  0.291667  0.5       0.708333  0.916667
 0.125      0.333333  0.541667  0.75      0.958333
 0.166667   0.375     0.583333  0.791667  1.0

[:, :, 2] =
 0.0        0.208333  0.416667  0.625     0.833333
 0.0416667  0.25      0.458333  0.666667  0.875
 0.0833333  0.291667  0.5       0.708333  0.916667
 0.125      0.333333  0.541667  0.75      0.958333
 0.166667   0.375     0.583333  0.791667  1.0

[:, :, 3] =
 0.0        0.208333  0.416667  0.625     0.833333
 0.0416667  0.25      0.458333  0.666667  0.875
 0.0833333  0.291667  0.5       0.708333  0.916667
 0.125      0.333333  0.541667  0.75      0.958333
 0.166667   0.375     0.583333  0.791667  1.0

如果重塑数组,您会看到:

colorview(RGB, reshape(img, (3, 5, 5)))

而如果您排列尺寸,您会看到:

colorview(RGB, PermutedDimsArray(img, (3,1,2)))

PermutedDimsArray 图片 returns 原始视图,因此,与 permutedims 不同,不会发生复制。