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

Viewed 238

I have a 224x224 image with 3 channels. I want to display the image in a Jupyter Notebook. When I do IJulia.display(imgs[:, :, :, 1]), all I get is the raw numbers and not the actual image. The code does work with ImageView.imshow but I want to capture the image in the notebook itself. What do I need to do to my data to be able to show it as an image?

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
 ⋮                                           ⋱                     
2 Answers

You simply need to reshape as 3x224x224.

This will plot something

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

This will return an error

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

Therefore if you have an array x that is in format (i,j,3) you want to first reshape it as (3,i,j) and then use colorview.

enter image description here

By the way, there's a difference between reshaping and permuting the dimensions, and you'll get different results from each.

Here's an array where the final dimensions have values between 0 and 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

If you reshape the array you'll see this:

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

reshaped array1

whereas if you permute the dimensions, you'll see this:

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

permuted dims2

PermutedDimsArray in Images returns a view of the original, so, unlike permutedims, no copying occurs.

Related