简体   繁体   中英

Get rgb color with magick++ using c++

I'm trying to get rgb from each pixel. But when I run my C++ code I get on the shell for color red something like this

55512

55255

55255

Why it is not a number between 0 and 255 as I exepected to be?

This is my code

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");

You'll need to do a bit of math to convert the pixel packet from the systems Quantum Depth to a format you want.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM