zahi daoui
zahi daoui

Reputation: 267

convert an rgb image into a matrix using C++ and Cimg library

I have this project in blind source seperation where I need to represent a RGB image in a matrix using Cimg. but I can't actually understand how to use Cimg.. I've looked through the documentation in

But there are TOO many functions and I wasn't able to know which one to use! really too many of them. I have never used Cimg, so if anyone could explain to me what should my procedure be please do! I am programming with C++ and using eclipse. Thanks!

Upvotes: 2

Views: 4848

Answers (3)

bvalabas
bvalabas

Reputation: 29

First define your image :

CImg<float> img(320,200,1,3);  // Define a 320x200 color image (3 channels).

Then fill it with your data :

cimg_forXYC(img,x,y,c) {  // Do 3 nested loops
   img(x,y,c) = pixel_value_at(x,y,c); 
}

Then you can do everything you want with it.

img.display("Display my image");

when c==0, you will fill the red channel of your image, when c==1, the green one and when c==2 the blue one. Nothing really hard.

I have experimented a lof of image processing libraries, and CImg is probably one of the easiest to use. Look at the provided example files (folder CImg/examples/) to see how the whole thing is working (particularly CImg/examples/tutorial.cpp).

Upvotes: 3

SGH
SGH

Reputation: 75

If you're not forced with CImg, I suggest you to use DevIL, an example of a working code looks like:

ilLoad();
ILuint image = 0;
ilGenImages(1,&image);
if(!image)
{
    // Error
}
ilBindImage(image);
if(!ilLoadImage("yourimage.png"))
{
    // Error
}
// 4-bytes per pixel for RGBA
ILuint width = ilGetInteger(IL_IMAGE_WIDTH);
ILuint height = ilGetInteger(IL_IMAGE_HEIGHT);
unsigned char* data=width*height*4;

ilCopyPixels(0,0,0,width,height,1,IL_RGBA,IL_UNSIGNED_BYTE,data);

ilDeleteImages(1,&image);
image = 0;

// now you can use 'data' as a pointer to all your required data.
// You can access from data[0] up to data[ (width*height*4) - 1].
// First pixel's red value: data[0]
// Second pixel's green value: data[1 + (4 * 1)]
// Third pixel's alpha value: data[3 + (4 * 2)]

// Once you're done...
delete[] data;
data = 0;

Upvotes: 0

eladidan
eladidan

Reputation: 2644

Getting started with any 3rd party library, I find it useful to start with a tutorial, like this one: CImg Tutorial

Especially if you are new to C++/programming in general.

Don't get frustrated with the wealth of the interface or magnitude of code. Stick to what you are looking for and let Google be your friend.

To get you started, **get acquainted with the CImg class. Then advance as your need dictates...

Upvotes: 0

Related Questions