我已经问过这个问题,但那是关于 FreeImage
的。现在我正在尝试用 ImageMagick
做同样的事情(更准确地说,用 Magick++
)。
我只需要获取图像中像素的 RGB 值,并能够将其打印到屏幕上。我在 ImageMagick
论坛上问过这个问题,但那里似乎没有人。 :-( 有人可以帮忙吗?
最佳答案
版本 6 API
给定一个“Image”对象,您必须请求一个“像素缓存”,然后使用它。文档是 here和 here :
// 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");
版本 7 API
对像素的访问在版本 7 中发生了变化(参见:porting),但低级访问仍然存在:
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
关于c++ - 使用 Magick++ 获取像素颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678511/