Magick++ get pixel color. pixelpacket doesnt seem to exist anymore

拟墨画扇 提交于 2020-01-17 03:11:46

问题


I'm trying to get the color of a pixel in a jpg image and all the tutorials I can find online say to use pixelpacket which doesnt exist anymore. searching and searching through google i've found that getvirtualpixels might be the new way but that doesnt seem to exist in the newest version either. so how do I get the color of pixels from an image with an image cache? I do not need to set any pixels btw. Using windows10 and visual studio 2015


回答1:


PixelPacket structures have been replaced with a pointer to Quantum data type. From Porting to ImageMagick Version 7, the key thing to understand is that a pixel can now have dynamic parts (or "traits" previously called as "channels").

ImageMagick version 7 supports any number of channels from 1 to 32 (and beyond) and simplifies access with a single method that returns an array of pixel channels of type Quantum.

You are now responsible for authoring code that handles deterministic (is that the right term?) pixel channels. For example...

Magick::Image rose("rose:");
const Magick::Quantum * q = rose.getConstPixels(5, 5, 1, 1);
size_t channel_length = rose.channels();
for (size_t k = 0; k < channel_length; k++ ) {
    std::cout << "Channel " << k << " = ";
    std::cout << q[k] << " of " << QuantumRange << std::endl;
}
/** Outputs
    -------
    Channel 0 = 13107 of 65535
    Channel 1 = 12079 of 65535
    Channel 2 = 11308 of 65535
 */

Without seeing your code, or understanding what your finial goal would be, I would guess that you really just want RGB info. If that's the case, the Magick::Image.getColor will work,

Magick::Image rose("rose:");
Magick::Color pixelColor = rose.pixelColor(5, 5);

std::cout << "Channel 0 = ";
std::cout << pixelColor.quantumRed() << " of " << QuantumRange << std::endl;
std::cout << "Channel 1 = ";
std::cout << pixelColor.quantumGreen() << " of " << QuantumRange << std::endl;
std::cout << "Channel 2 = ";
std::cout << pixelColor.quantumBlue() << " of " << QuantumRange << std::endl;
/** Outputs
    -------
    Channel 0 = 13107 of 65535
    Channel 1 = 12079 of 65535
    Channel 2 = 11308 of 65535
 */

But! Sometimes it's just easier to export the image data into a format you're already working with. For example, I might only need Green values as single-point float list.

Magick::Image rose("rose:");
Magick::PixelData pixelBlob(rose, "G", Magick::FloatPixel);
float * buffer = (float *)pixelBlob.data();
// ...


来源:https://stackoverflow.com/questions/37903963/magick-get-pixel-color-pixelpacket-doesnt-seem-to-exist-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!