views:

140

answers:

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

How can I get rgb from ptr?

A: 

From the CImg documentation -- section 6.13 on page 34, and section 8.1.4.16 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 = src.data(10, 10, 0, 0);
unsigned char *g = src.data(10, 10, 0, 1);
unsigned char *b = src.data(10, 10, 0, 2);

(But this is just a guess!)

Edit:

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

unsigned char r = src(10, 10, 0, 0);
Nate Kohl
Tested ,but failed...
Have you tried operator(), e.g. unsigned char r = src(10,10,0,0);
Nate Kohl
Really depends on your image, but the r g and b channels are split, so reading one channel will get you the red value (ie, channel 0 with a jpg image), channel 1 will get you green, and channel 2 will get you blue... theres an example that does it with a float image in the docs in section 8.1.4.9.. change the floats into unsigned chars and it should work... Heres an example unsigned char r = src.data(10,10,0,0); unsigned char g = src.data(10,10,0,1); unsigned char b = src.data(10,10,0,2);
Jay Kramer
How to make it work with gray scale images?
wamp
@wamp: I'd guess that grayscale images have a single channel of data per pixel, and that you'd be able to access that value in exactly the same way that you'd access the "red" component of an RGB image. E.g. `char v = src(10, 10, 0, 0);`
Nate Kohl
How to determine whether it's rgb or grayscale image with cimg?
@user198729: It looks like the `spectrum` method returns the number of color channels. So `int numChannels = src.spectrum();` might set `numChannels` to 3 for an RGB image, whereas it might be 1 for a grayscale image.
Nate Kohl
A: 

Tested on Ubuntu 10.04 with a handmade 3x3 RGB image saved as test.png:

sudo apt-get install cimg-dev

Source file cimg_test.cpp:

#include <iostream>
using namespace std;

#include <CImg.h>
using namespace cimg_library;

int main()
{
    CImg<unsigned char> src("test.png");
    int width = src.width();
    int height = src.height();
    cout << width << "x" << height << endl;
    for (int r = 0; r < height; r++)
        for (int c = 0; c < width; c++)
            cout << "(" << r << "," << c << ") ="
                 << " R" << (int)src(c,r,0,0)
                 << " G" << (int)src(c,r,0,1)
                 << " B" << (int)src(c,r,0,2) << endl;
    return 0;
}

Compile and run:

g++ cimg_test.cpp -lX11 -lpthread -o cimg_test

./cimg_test 
3x3
(0,0) = R0 G0 B0
(0,1) = R255 G0 B0
(0,2) = R0 G255 B0
(1,0) = R0 G0 B255
(1,1) = R128 G128 B128
(1,2) = R0 G0 B128
(2,0) = R128 G0 B0
(2,1) = R0 G128 B0
(2,2) = R255 G255 B255

It works.

Vanni Totaro
How to make it work with gray scale images?
wamp
@wamp: src(c,r,0,0) is the 0-255 value of your gray scale image (r,c) pixel.
Vanni Totaro
A: 

@wamp: I don't know about CImg but grayscale images in RGB have:

R = G = B

and in CMYK:

C = M = Y = 0

K = luminance

So you don't even need a function for that...

J--