How to load a picture by GPU?


i wanted to know if it’s possible to load an image (JPEG, TIFF or whatever) from a folder by CUDA?

Can we do arrays with more than one dimension?

Thank you


To load a 2D image onto the GPU, you have to open the file and load the pixel information into an array on the CPU however you would normally do that (nothing to do with CUDA). Then you use cudaMallocArray() to allocate memory on the GPU for the image, and finally cudaMemcpyToArray() the CPU memory to the GPU memory.

Take a look at the simpleTexture example in the CUDA SDK to see how this works. Note that this example uses textures, which are very useful for most 2D image processing algorithms.

I’ve used ImageMagick to try to open various formats and get float / int / short / char arrays, and put them on the device.

thank you.

i’m a newbie in programing, so could you show me how to code for loading a picture?


sure. Here’s some code I have that loads an ImageMagick image into a 1D host array. This converts the image to grayscale, and converts the data to float as well. It’s not hard to modify this for int / short / whatever, to use a 2D array, or to read color info. I’m not exactly sure how transferring the 2D array to the device would work, but that’s another problem.

#include "Magick++.h" // include ImageMagick C++ bindings

#include <string>

#include <iostream>

using namespace Magick;

using namespace std;

int main(int argc, char *argv[])


    InitializeMagick(*argv); // mandatory once per program run

   string imageName(argv[1]);

   Magick::Image ImageIN;;

   int height = ImageIN.rows();

    int width = ImageIN.columns();

   int size = height*width;

    cout << "Image width: " << width << endl;

    cout << "Image height: " << height << endl;

   float imgData;

    Magick::PixelPacket* pp = ImageIN.getPixels(0,0,width,height);

   for(int i = 0; i < size; i++)


        imgData[i] = (float) pp[i].red / 65535.0f;

        // for float values, divide by USHORT_MAX because my ImageMagick  version uses unsigned shorts internally


   //TODO: cudaMemcpy(imgData_DEVICE, imgData, size * sizeof(float), cudaMemcpyHostToDevice);


Ok thank you

There’s a couple of library-oriented things missing from my description.

Assuming you’re using the Makefile / stuff from the NVIDIA sample projects. You’ll need -lMagick++ in your Makefile, like so:

LIB             := -lglut -lMagick++

Try this, it might be enough.

Actually, to get this to run myself a couple months ago, I did something not so good: I copied and hacked the NVIDIA_CUDA_SDK/common/ makefile. I changed a line:

LIB       := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib <b>/usr/lib/</b>

I’m not really sure hacking is necessary.

I hope someone posts a cleaner solution for linking this stuff.


I tried to use Imagemagick as in the example above.

I copied all the sourcefiles from projects/template into a new folder, and edited the sources.

After this, i replaced all the sources in template_gold.cpp with the example above.

I cleard the

Now, i have the problem, that i got some compiler errors!

template_gold.cpp:8: warning: unused parameter 'argc'

obj/release/template_gold.cpp_o: In function `main':

template_gold.cpp:(.text+0x90): undefined reference to `Magick::InitializeMagick(char const*)'

template_gold.cpp:(.text+0xb4): undefined reference to `Magick::Image::Image()'

template_gold.cpp:(.text+0xc3): undefined reference to `Magick::Image::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

template_gold.cpp:(.text+0xce): undefined reference to `Magick::Image::constImage() const'

template_gold.cpp:(.text+0xdf): undefined reference to `Magick::Image::constImage() const'

template_gold.cpp:(.text+0x172): undefined reference to `Magick::Image::getPixels(int, int, unsigned int, unsigned int)'

template_gold.cpp:(.text+0x1b1): undefined reference to `Magick::Image::~Image()'

template_gold.cpp:(.text+0x206): undefined reference to `Magick::Image::~Image()'

collect2: ld returned 1 exit status

make: *** [../../bin/linux/release/template] Error 1

Can anybody help me?