Convert Array{UInt32, 2} to ARGB image with Images.jl

How to convert Array{UInt32, 2} to ARGB image with Images.jl?

julia> reinterpret(ARGB{N0f8}, rand(UInt32, 3, 2))
3×2 reinterpret(ARGB{N0f8}, ::Array{UInt32,2}):
 ARGB{N0f8}(0.569,0.341,0.922,0.075)  ARGB{N0f8}(0.239,0.31,0.69,0.192)
 ARGB{N0f8}(0.153,0.761,0.208,0.149)  ARGB{N0f8}(0.486,0.769,0.282,0.78)
 ARGB{N0f8}(0.09,0.427,0.765,0.482)   ARGB{N0f8}(0.733,0.443,0.616,0.518)

Note that you may want to collect the reinterpreted array before further processing:

julia> RA = reinterpret(ARGB{N0f8}, rand(UInt32, 512, 512));

julia> A = collect(RA);

julia> using Statistics, BenchmarkTools

julia> @btime mean($RA)
  2.528 ms (0 allocations: 0 bytes)
ARGB{Float32}(0.49983406f0,0.500403f0,0.4998448f0,0.50037414f0)

julia> @btime mean($A)
  81.999 μs (0 allocations: 0 bytes)
ARGB{Float32}(0.4998341f0,0.50040305f0,0.4998448f0,0.5003742f0)
2 Likes

Thank you, it works!