use*_*729 5 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);
Run Code Online (Sandbox Code Playgroud)
我怎样才能rgb从ptr?
在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;
}
Run Code Online (Sandbox Code Playgroud)
编译并运行:
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
有用.
从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)
Run Code Online (Sandbox Code Playgroud)
...其中c指的是颜色通道。我猜测,如果您的图像确实是 RGB 图像,那么使用 0、1 或 2 的值c将为您提供给定位置的红色、绿色和蓝色分量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);
Run Code Online (Sandbox Code Playgroud)
(但这只是猜测!)
编辑:
看起来 CImg 也有一个operator(),其工作方式类似:
unsigned char r = src(10, 10, 0, 0);
Run Code Online (Sandbox Code Playgroud)