How to get rgb cimg value?

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 rgbout ptr?

+3
source share
4 answers

From the CImg documentation - section 6.13 on page 34 and section 8.1.4.16 on page 120 - it looks like it datacan 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 it crefers to the color channel. I assume that if your image is really an RGB image, then using values โ€‹โ€‹of 0, 1, or 2 for cwill give you red, green, and blue components at that location x, y.

For instance:

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 an assumption!)

Edit:

, CImg (), :

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

Ubuntu 10.04 3x3 RGB, test.png:

sudo apt-get install cimg-dev

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;
}

:

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

.

+5

- ():

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

, , CImg . R1, R2, ..., G1, G2, ..., B1, B2, ... R1, G1, B1, R2, G2, B2, ...: http://cimg.eu/reference/group__cimg__storage.html

.data() , , , :

CImg<unsigned char> 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];
0

@wamp: CImg, RGB :

R = G = B

CMYK:

C = M = Y = 0

K =

, ...

-1

Source: https://habr.com/ru/post/1755553/


All Articles