c++ - 如何通过cimg获取rgb值?

标签 c++ image-processing cimg

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

如何从 ptr 获取 rgb

最佳答案

在 Ubuntu 10.04 上使用保存为 test.png 的手工制作的 3x3 RGB 图像进行测试:

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

编译运行:

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

有效。

关于c++ - 如何通过cimg获取rgb值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3291923/

相关文章:

c++ - 从live555入手,集成LIBMAD解码器

c++ - MFC C++ 从 CEdit 派生并派生 GetWindowText

c++ - CUDA 太多的 dll

swift - 缓存溢出的图像处理

c++ - 使用 Cimg 在 C++ 中绘制 vector

c++ - CImg库可以画粗线吗

c++ - 六边形网格算法

python - opencv findContours 错过了一些区域。[没有得到所有正确的边界框]

python - Opencv——检测眼睛瞳孔(中)

c++ - 将 exr/pfm 保存到位图 CImg