如何通过cimg获取rgb值?

发布于 2024-09-10 19:09:28 字数 209 浏览 2 评论 0原文

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

如何从 ptr 获取 rgb

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久光 2024-09-17 19:09:28

在 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

它可以工作。

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.

红尘作伴 2024-09-17 19:09:28

来自CImg 文档 - 第 34 页第 6.13 节和第 120 页第 8.1.4.16 节-- 看起来 data 方法可以采用四个参数:x、y、z 和 c:

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

...其中 c 指的是颜色通道。我猜测,如果您的图像确实是 RGB 图像,那么对 c 使用 0、1 或 2 的值将为您提供给定 处的红色、绿色和蓝色分量x, y 位置。

例如:(

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

但这只是一个猜测!)

编辑:

CImg 似乎还有一个以类似方式工作的operator():

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

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);
谎言月老 2024-09-17 19:09:28

访问数据的最简单方法是使用 () 运算符:

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

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<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];
迷你仙 2024-09-17 19:09:28

@wamp:我不知道 CImg 但 RGB 中的灰度图像有:

R = G = B

和 CMYK 中:

C = M = Y = 0

K = 亮度

所以你甚至不需要一个函数......

@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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文