使用 Magick++ 获取像素颜色?

发布于 2024-12-08 19:18:50 字数 221 浏览 0 评论 0原文

我已经问过这个问题,但那是关于 FreeImage 的。现在我尝试使用 ImageMagick 做同样的事情(更正确的是,使用 Magick++)。

我所需要的只是获取图像中像素的 RGB 值,并能够将其打印到屏幕上。我在ImageMagick论坛上问过这个问题,但那里似乎没有人。 :-( 有人可以帮忙吗?

I've already asked this question, but that was about FreeImage. Now I'm trying to do the same thing with ImageMagick (to be more correct, with Magick++).

All I need is to get the RGB value of pixels in an image with the ability to print it out onto the screen. I asked this in the ImageMagick forum, but it seems there is nobody there. :-( Can anybody help, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2024-12-15 19:18:50

版本 6 API

给定一个“图像”对象,您必须请求“像素缓存”,然后使用它。文档位于此处此处

// 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 中对像素的访问已更改(请参阅:<一个href="http://www.imagemagick.org/script/porting.php" rel="noreferrer">移植),但低级别访问仍然存在:

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

Version 6 API

Given an "Image" object, you have to request a "pixel cache", then work with it. Documentation is here and 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");

Version 7 API

Access to pixels has changed in version 7 (see: porting), but low-level access is still present:

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
只是偏爱你 2024-12-15 19:18:50

@Sga 的答案对我不起作用,我正在使用 ImageMagick-7.0.7-Q8 (8 位深度)库。

这是我的做法,逐像素扫描图像并输出每个图像的 RGB 值:

// "InitializeMagick" called beforehand!
void processImage()
{
    std::ifstream fin;

    std::stringstream 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
}

@Sga's answer didn't work for me, I'm using the ImageMagick-7.0.7-Q8 (8 bit depth) library.

Here's how I did it, to scan an image pixel by pixel and output each one's RGB value:

// "InitializeMagick" called beforehand!
void processImage()
{
    std::ifstream fin;

    std::stringstream 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文