简体   繁体   English

在Magick ++中将Alpha蒙版应用于图像

[英]Applying alpha mask to image in Magick++

I have an image, that has a sequence of two frames. 我有一个图像,该图像具有两个帧的序列。 The second frame is supposed to be an alpha mask for the first frame. 第二帧应该是第一帧的alpha蒙版。

Here is an example: 这是一个例子:

http://i.imgur.com/c2M10u7.png http://i.imgur.com/c2M10u7.png

I've written the following code using Magick++ to split the image into two halves, and apply the alpha mask: 我已经使用Magick ++编写了以下代码,将图像分为两半,并应用了alpha蒙版:

#include "stdafx.h"
#include <iostream>
#include <Magick++.h>

int main(int argc, char **argv)
{
    Magick::InitializeMagick(*argv);

    Magick::Image base, mask;
    std::string image;

    if (argc > 1)
    {
        image = argv[1];
    }
    else
        return EXIT_FAILURE;

    // Read image
    base.read(image);
    mask = base;

    // Crop out mask and sprite
    base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
    mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

    // Apply mask
    base.composite(mask, 0, 0, Magick::BlendCompositeOp);

    // Write
    base.write("output.png");

    return EXIT_SUCCESS;
}

However, I can't figure out how to actually apply this as an alpha mask, rather than just blending it. 但是,我无法弄清楚如何将其实际用作Alpha蒙版,而不仅仅是混合它。 I cannot find a solution for it anywhere either, at least not for C++. 我也找不到任何解决方案,至少在C ++中也找不到。

Any help would be greatly appreciated. 任何帮助将不胜感激。

You should disable the alpha channel on both images and use CopyOpacity instead of Blend. 您应该在两个图像上都禁用Alpha通道,并使用CopyOpacity而不是Blend。 It also looks like your mask is inverted so you should probably negate it. 看起来您的遮罩也被倒置了,因此您应该否定它。 Below is an example in C++: 以下是C ++中的示例:

// Crop out mask and sprite
base.crop(Magick::Geometry(base.columns() / 2, base.rows(), 0, 0));
mask.crop(Magick::Geometry(mask.columns() / 2, mask.rows(), mask.columns() / 2, 0));

// Disable alpha channel
base.matte(false);
mask.matte(false);

// Invert the mask
mask.negate();

// Apply mask
base.composite(mask, 0, 0, Magick::CopyOpacityCompositeOp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM