Most idomatic way to convert an Images.jl Image to a 3-d array?

question

#1

I’ve loaded an image file to get a 2-d array of RGB color values. What I want is a 3-d array with depth 3.

The function below will do the job. But is there a more idomatic way?

rgb_to_array(img) = begin
    H, W = height(img), width(img)
    a = zeros(H, W, 3)
    for w in 1:W
        for h in 1:H
            rgb = img[h,w]
            a[h,w,1] = rgb.r
            a[h,w,2] = rgb.g
            a[h,w,3] = rgb.b
        end
    end
    a
end

#2

I think this should do the job:
permutedims(reinterpret(eltype(eltype(img)), img), (2, 3, 1))


#3

Check out channelview(): http://juliaimages.github.io/latest/quickstart.html#Colors,-the-0-to-1-intensity-scale,-and-views-1


#4
permutedims(channelview(img), (2,3,1))

#5

note that the permutedims is only needed because you seem to want the color channel to be in the last dimension.


#6

Awesome. Thanks!