CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10);
如何从ptr
获得rgb
?如何通过cimg获取rgb值?
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10);
如何从ptr
获得rgb
?如何通过cimg获取rgb值?
从the CImg documentation - 120页上的第34页和第8.1.4.16节上6.13 - 它看起来像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,在一个工程的运营商()类似的方式:
unsigned char r = src(10, 10, 0, 0);
测试在Ubuntu 1 0.04保存为test.png
一个手工制作的3x3 RGB图像:
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
它的工作原理。
如何使它适用于灰度图像? – wamp 2010-07-23 03:02:38
@wamp:src(c,r,0,0)是灰度图像(r,c)像素的0-255值。 – 2010-07-23 18:17:38
@wamp:我不知道CIMG但在RGB灰度图像有:
R = G = B
和在CMYK:
C = M = Y = 0
K =
亮度所以你甚至不需要一个函数,该函数......
这是不正确的。尽管您可以使用仅使用R = G = B的RGB格式保存图像,但您将使用必要的3倍数据来存储图像。这就是为什么只有一个灰度格式的图像只能保存一个通道。 – 2015-07-06 19:40:01
访问数据最简单的方法是用()
操作:
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];
测试,但失败... – user198729 2010-07-20 16:31:24
有你尝试过operator(),例如unsigned char r = src(10,10,0,0); – 2010-07-20 17:10:57
真的取决于你的图像,但是rg和b通道是分开的,所以读取一个通道会得到红色的值(即带有jpg图像的通道0),通道1会让你变绿,通道2会让你蓝色...这是一个例子,它与一个浮动图像在文档中的8.1.4.9 ..更改为无符号字符浮动,它应该工作...继承人的一个例子 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); – 2010-07-20 17:37:49