使用 c++ 通过 magick++ 获取 rgb 颜色
Get rgb color with magick++ using c++
我正在尝试从每个像素中获取 rgb。但是当我 运行 我的 C++
代码时,我在 shell 上得到了红色的
55512
55255
55255
为什么它不是我预期的 0 到 255 之间的数字?
这是我的代码
Image image("image_test.jpg");
int w = image.columns();
int h = image.rows();
// get a "pixel cache" for the entire image
PixelPacket *pixels = image.getPixels(0, 0, w, h);
// now you can access single pixels like a vector
int row = 0;
int column = 0;
Color color = pixels[w * row + column];
for(row=0; row<h-1; row++){
for(column=0; column<w-1; column++){
Color color = pixels[w * row + column];
cout<<pixels[w * row + column].red;
image.syncPixels();
}
}
//image.syncPixels();
// write the image to file.
//image.write("test_modified.jpg");
您需要做一些数学运算才能将像素包从系统量子深度转换为您想要的格式。
Image image("image_test.jpg");
int w = image.columns();
int h = image.rows();
// Calc what your range is. See http://www.imagemagick.org/Magick++/Color.html
// There's also other helpful macros, and definitions in ImageMagick's header files
int range = pow(2, image.modulusDepth());
assert(range > 0); // Better do some assertion/error checking here
// get a "pixel cache" for the entire image
PixelPacket *pixels = image.getPixels(0, 0, w, h);
// now you can access single pixels like a vector
int row = 0;
int column = 0;
Color color = pixels[w * row + column];
for(row=0; row<h-1; row++){
for(column=0; column<w-1; column++){
Color color = pixels[w * row + column];
cout << (color.redQuantum() / range) << endl;
}
}
我正在尝试从每个像素中获取 rgb。但是当我 运行 我的 C++
代码时,我在 shell 上得到了红色的
55512
55255
55255
为什么它不是我预期的 0 到 255 之间的数字?
这是我的代码
Image image("image_test.jpg");
int w = image.columns();
int h = image.rows();
// get a "pixel cache" for the entire image
PixelPacket *pixels = image.getPixels(0, 0, w, h);
// now you can access single pixels like a vector
int row = 0;
int column = 0;
Color color = pixels[w * row + column];
for(row=0; row<h-1; row++){
for(column=0; column<w-1; column++){
Color color = pixels[w * row + column];
cout<<pixels[w * row + column].red;
image.syncPixels();
}
}
//image.syncPixels();
// write the image to file.
//image.write("test_modified.jpg");
您需要做一些数学运算才能将像素包从系统量子深度转换为您想要的格式。
Image image("image_test.jpg");
int w = image.columns();
int h = image.rows();
// Calc what your range is. See http://www.imagemagick.org/Magick++/Color.html
// There's also other helpful macros, and definitions in ImageMagick's header files
int range = pow(2, image.modulusDepth());
assert(range > 0); // Better do some assertion/error checking here
// get a "pixel cache" for the entire image
PixelPacket *pixels = image.getPixels(0, 0, w, h);
// now you can access single pixels like a vector
int row = 0;
int column = 0;
Color color = pixels[w * row + column];
for(row=0; row<h-1; row++){
for(column=0; column<w-1; column++){
Color color = pixels[w * row + column];
cout << (color.redQuantum() / range) << endl;
}
}