Convert Array{RGB{Float32},2} to Array{Float32,2}

I have an image that is Type: Array{RGB{Float32},2} but need to convert it to Type: Array{Float32,2}. Is there a way to do that ? Thanks!

reinterpret should do what you want.

1 Like

Thanks Oscar! When I try that the type of object that is returned is:
Base.ReinterpretArray{Float32,2,RGB{Float32},Array{RGB{Float32},2}}. Is there a way for the type to simply be: Array{Float32,2}? Thanks!

You can use collect to convert most lazy wrapper types in Julia to their concrete representations, including ReinterpretArray, Adjoint, SubArray, etc.

Be aware that reinterpreting a 2D color array of dimensions 2 x 2 will result in an array of size 6 x 2, as the pixel values are unpacked along each column like so:

[rgb rgb     [r r
 rgb rgb] ->  g g
              b b
              r r
              g g
              b b]

If you want to maintain the original image shape, you’ll probably want to convert the array to grayscale before reinterpreting: collect(reinterpret(Float32, Gray.(img)))

1 Like

Brilliant stillyslalom. That worked perfectly. Thanks so much :pray: