Converting ArrayFire array to OpenCV GpuMat

447 views
Skip to first unread message

abf...@gmail.com

unread,
Aug 24, 2015, 9:52:58 AM8/24/15
to ArrayFire Users
Hello,

I've been trying to create an interface between ArrayFire and OpenCV. I want to take an RGB image that was stored as an ArrayFire array and convert it to an OpenCV GpuMat. OpenCV expects a packed image of unsigned integers while ArrayFire array stores the image as floats with each image channel as a separate plane.

I can convert the array to unsigned integers and reindex the array to repack the image for OpenCV. Then I can create the GpuMat with the device pointer of the array, I added a few lines below to try and show what I'm doing :

array input;

array input_u8 = input.as(u8);

input_u8 = array(input_u8, dim4(height, width*channels);
input_u8 = input_u8(span, array(indices));

GpuMat myOpenCVImage(height, width, input_u8.device<uchar>()):

Where height and width are the height and width of the image and channels are the number of image planes which is 3 in this case. Indices is an array that re indexes the image into an RGB RGB RGB... format instead of a separate plane for each color channel. This allows me to get the image as an Opencv GpuMat. However, everytime this function is used (in my case, it is used in a while loop) I see that ArrayFire is allocating more memory until there is none left on the device and the ArrayFire throws an out of memory exception. I'm not sure why this is happening.

Just in case the af::info output is:
ArrayFire v3.1.0 (CUDA, 64-bit Linux, build a8d8837)
Platform: CUDA Toolkit 7, Driver: 346.46
[0] Quadro K4000M, 4096 MB, CUDA Compute 3.0

Adrian

Pavan Yalamanchili

unread,
Aug 24, 2015, 10:07:07 AM8/24/15
to abf...@gmail.com, ArrayFire Users
Hi,

You need to do the following to get the data in packed format:

input_u8 = moddims(input_u8, height, width, channels);
input_u8 = reorder(input_u8, 2, 0, 1); // This converts the 3rd dimension to be the first dimension
uchar *packed_ptr = input_u9.device<uchar>();

You can then pass packed_ptr to GpuMat for further operations.

--
Pavan

--
You received this message because you are subscribed to the Google Groups "ArrayFire Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arrayfire-use...@googlegroups.com.
To post to this group, send email to arrayfi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/arrayfire-users/baa949a4-0d7a-472d-a973-a6c8ab5e3629%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

abf...@gmail.com

unread,
Aug 24, 2015, 11:27:36 AM8/24/15
to ArrayFire Users
Hi Pavan,

Thanks for the quick reply, it looks like my converter is working after adding your changes. I appreciate your help!

Adrian
Reply all
Reply to author
Forward
0 new messages