【问题标题】:Getting pixel color with Magick++?使用 Magick++ 获取像素颜色?
【发布时间】:2018-04-16 15:43:25
【问题描述】:

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

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

【问题讨论】:

    标签: c++ imagemagick magick++


    【解决方案1】:

    第 6 版 API

    给定一个“Image”对象,您必须请求一个“像素缓存”,然后使用它。文档是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
    

    【讨论】:

    • 试图检索 RGB 的值,就像我的图像一样,结果只输出一个空白?
    • 注意量子大小,可能配置为16位
    • 谢谢,我搞定了!我将以我的方式发布另一个答案,希望你不介意。
    【解决方案2】:

    @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
    }
    

    【讨论】:

    • 调用InitializeMagick() 只需要在Windows(可能还有Mac)下。详情请见here
    • @ynn 好点,当时这是在 Windows 机器上运行的,我假设在 Linux 机器上调用 InitializeMagick() 不会有任何不利影响?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2014-08-05
    相关资源
    最近更新 更多