使用c ++获取magick ++的rgb颜色

时间:2015-01-26 13:43:22

标签: c++ imagemagick rgb magick++

我正试图从每个像素获得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");

1 个答案:

答案 0 :(得分:0)

您需要做一些数学运算才能将像素数据包从Quantum Depth系统转换为您想要的格式。

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