Obtenir des pixels de couleur avec Magick ++?

J’ai déjà posé cette question, mais c’était à propos de FreeImage . Maintenant, j’essaie de faire la même chose avec ImageMagick (pour être plus correct, avec Magick++ ).

Tout ce dont j’ai besoin, c’est d’obtenir la valeur RVB des pixels dans une image avec la possibilité de l’imprimer à l’écran. Je l’ai demandé dans le forum ImageMagick , mais il semble qu’il n’y ait personne là-bas. 🙁 Quelqu’un peut-il aider, s’il vous plaît?

API version 6

Étant donné un object ” Image “, vous devez demander un ” cache de pixels “, puis travailler avec. La documentation est ici et ici :

 // load an image Magick::Image image("test.jpg"); int w = image.columns(); int h = image.rows(); // get a "pixel cache" for the entire image Magick::PixelPacket *pixels = image.getPixels(0, 0, w, h); // now you can access single pixels like a vector int row = 0; int column = 0; Magick::Color color = pixels[w * row + column]; // if you make changes, don't forget to save them to the underlying image pixels[0] = Magick::Color(255, 0, 0); image.syncPixels(); // ...and maybe write the image to file. image.write("test_modified.jpg"); 

API version 7

L’access aux pixels a changé dans la version 7 (voir: portage ), mais l’access de bas niveau est toujours présent:

 MagickCore::Quantum *pixels = image.getPixels(0, 0, w, h); int row = 0; int column = 0; unsigned offset = image.channels() * (w * row + column); pixels[offset + 0] = 255; // red pixels[offset + 1] = 0; // green pixels[offset + 2] = 0; // blue 

La réponse de @ Sga n’a pas fonctionné pour moi, j’utilise la ImageMagick-7.0.7-Q8 (profondeur de 8 bits).

Voici comment je l’ai fait pour numériser une image pixel par pixel et générer la valeur RVB de chacune d’elles:

 // "InitializeMagick" called beforehand! void processImage() { std::ifstream fin; std::ssortingngstream fs; fs << "/img.png"; std::cout << "Opening image \"" << fs.str() << "\".." << std::endl; try { Image img; img.read( fs.str() ); int imgWidth = img.columns(); int imgHeight = img.rows(); std::cout << "Image width: " << imgWidth << std::endl; std::cout << "Image height: " << imgHeight << std::endl; std::cout << "Image channels: " << img.channels() << std::endl; img.modifyImage(); for ( int row = 0; row <= imgHeight; row++ ) { for ( int column = 0; column <= imgWidth; column++ ) { ColorRGB px = img.pixelColor( column, row ); std::cout << "Pixel " << column << "," << row << " R: " << px.red() << " G: " << px.green() << " B: " << px.blue() << std::endl; } } } catch ( Magick::Exception & error ) { std::cerr << "Caught Magick++ exception: " << error.what() << std::endl; } fin.close(); // Close the file }