user198729 user198729 - 11 months ago 117
C++ Question

How to get rgb value by cimg?

CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr =,10);

How can I get

Answer Source

From the CImg documentation -- section 6.13 on page 34, and section on page 120 -- it looks like the data method can take four arguments: x, y, z, and c:

T* data(const unsigned int x, const unsigned int y = 0, 
        const unsigned int z = 0, const unsigned int c = 0)

...where c refers to the color channel. I'm guessing that if your image is indeed an RGB image, then using values of 0, 1, or 2 for c will give you the red, green, and blue components at a given x, y location.

For example:

unsigned char *r =, 10, 0, 0);
unsigned char *g =, 10, 0, 1);
unsigned char *b =, 10, 0, 2);

(But this is just a guess!)


It looks like there's also an operator() for CImg that works in a similar manner:

unsigned char r = src(10, 10, 0, 0);