How to get rgb value by cimg?

前端 未结 4 2035
野性不改
野性不改 2020-12-16 05:43
CImg src(\"image.jpg\");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); 

How can I

4条回答
  •  佛祖请我去吃肉
    2020-12-16 05:51

    The easiest way to access data is with the () operator:

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

    You are probably hitting confusion because CImg stores the raw data non-interleaved. i.e. your raw data is stored R1, R2, ..., G1, G2, ..., B1, B2, ... instead of R1, G1, B1, R2, G2, B2, ... see: http://cimg.eu/reference/group__cimg__storage.html

    .data() just returns a pointer, so to access the data directly as above you would do:

    CImg src("image.jpg");
    int width = src.width();
    int height = src.height();
    unsigned char* ptr = src.data(10,10);
    unsigned char r = ptr[0];
    unsigned char g = ptr[0+width*height];
    unsigned char b = ptr[0+2*width*height];
    

提交回复
热议问题