2011-10-06 219 views
7

我已經問過這個問題,但那是關於FreeImage的。現在我試圖用ImageMagick做同樣的事情(用Magick ++更正確)。我需要的只是獲得圖像中像素的RGB值,並能夠在屏幕上打印它。我在ImageMagick論壇上問過這個問題,但似乎沒有人在那裏。 :-(任何人可以幫助嗎?使用Magick獲取像素顏色++

回答

11

6版API

發出「圖片」對象,你有權要求一個「像素高速緩存」,然後使用它。文檔是herehere

// 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