Reading Images Wondering how I would check pixel colour in a *.bmp file

Hi all,

Ok here is what I need to do, I am trying to take a radar image and covert the pixels that contain clouds into black and sky/terrain white. I am not sure how to read pixels in an image file. The files are of the bmp extension. Any suggestions, please don’t flip out if this isn’t the right type of forum for this question.

If anyone could give some tips on how I could maximize effciency please let me know.

I’ve used ImageMagick to do this. The Magick++ C++ library to be exact. I instantiate a Magick::Image, call the open(filename) function and then I do something like getPixels(0, 0, width, height). The source code is not accessible to me ATM but it’s really simple. Once I have the RGB pixels from ImageMagick, you just copy the arrays around as usual.

Something like this:

#include "Magick++.h

using namespace Magick;

int main(...)

	InitializeMagick(*argv);

	Magick::Image ImageIN;

	string imageName(argv[1]);

	ImageIN.read(imageName);

	ImageIN.type(Magick::GrayscaleType); // converts image to grayscale

	int height = ImageIN.rows();

	int width = ImageIN.columns();

	int size = height * width;

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

	float giantFloatPixelArray;

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

	{

		giantFloatArray[i] = (float) pp[i].red / 65535.0f; // because we converted to grayscale, all the data is in pp.red ... if you omit the grayscale conversion, you have to read pp.red, .green and .blue as well to get the RGB data, alpha is probably in pp.alpha

	}

	// send giantFloatArray, height and width to CUDA part of program

}

Btw, this not only converts to grayscale but also converts the pixels to [0.0-1.0] normalized float values, which was useful to us. Even better: load the unnormalized short values to CUDA and normalize and convert to float on the CUDA card. (Imagemagick gives you 16-bit shorts., hence the division by 65535 to reach 1.0.)

If I’m going too fast, let me know and i’ll explain better! Good luck.

Hi there,

I also have to read image for processing, and I wonder if there is any faster way of copying image to the device memory. In your method, kristleifur, we have to go through the whole image in a loop, so it would probably take some time. Is it possible to copy the image as a whole, and then (from the global memory) read values of pixels? Is there any appropriate library for doing this? I am new to the CUDA stuff, so forgive my naivety :unsure:

Sure, it just depends on how ImageMagick exposes the interal pixel representation. If you can get a pointer to raw data (and I do believe you can), you should just be able to memcpy that to the CUDA card.

–edit: I’ve also seen the DevIL library used for the same purpose … it may be more convenient to use, ImageMagick can be a bit arcane.

… and ps., you’re right, the for loop stuff should indeed be much faster in CUDA ! - classic example of an embarassingly parallel calculation

DevIL is pretty great, I have used it many times in the past.