How do I convert ArrayFire Image to Julia Image?

121 views Asked by At

I want to use the plotting functionality of Plots.jl with an image loaded using the load_image() function of ArrayFire.

What I have is :

AFArray: 1000×300×3 Array{Float32,3}

What I want is :

300×1000 Array{RGB{Any},2} with eltype RGB

I couldn't be able to find direct conversion in documentations. Is there any efficient way to do this?

1

There are 1 answers

8
DNF On BEST ANSWER

I don't know specifically about ArrayFire arrays, but in general you can use reinterpret for operations like this. If you want the new array to reside on the cpu, then copy it over.

Then, ideally, you could just do

rgb = reinterpret(RGB{Float32}, A)

Unfortunately, MxNx3 is not the optimal layout for RGB arrays, since you want the 3-values to be located sequentially. So you should either make sure that the array has 3xMxN-layout, or you can do permutedims(A, (3, 1, 2)).

Finally, to get a matrix, you must drop the leading singleton dimension, otherwise you get a 1xMxN array.

So,

rgb = dropdims(reinterpret(RGB{Float32}, permutedims(A, (3, 1, 2))); dims=1)

I assumed that you actually want RGB{Float32} instead of RGB{Any}.

BTW, I'm not sure how this will work if you want to keep the final array on the GPU.

Edit: You might consider reshape instead of dropdims, it seems slightly faster on my pc.