MagickImage.ClipMask usage

Aug 23, 2013 at 1:21 PM
Edited Aug 23, 2013 at 1:25 PM
Hi, hope someone can help me. I've been trying to apply a grayscale mask to an image with no luck. I have 2 MagickImage images, one 8-bit grayscale (my mask) and one 8-bit sRGB. They're both the same size.

I'm trying to apply the mask like so:
using (MagickImage first = magickImg)
            {
                MagickImage magickMask = new MagickImage(mask.ToBitmap());
                first.ClipMask = magickMask;
                UpdateUI("pictureBox2", first.ToBitmap());
            }
Where magickImg was already defined elsewhere, UpdateUI is my method for updating a picturebox on my main form. If I write the image to the disk I get the same result, ie. as if the mask had no effect.

I also tried:
using (MagickImage first = magickImg)
            {
                MagickImage magickMask = new MagickImage(mask.ToBitmap());
                first.Composite(magickMask, Gravity.Center, CompositeOperator.CopyOpacity);
                UpdateUI("pictureBox2", first.ToBitmap());
            }
And I've fiddled about with toggling the HasAlpha property on "first", but that doesn't make a difference.

It's worth noting that in my previous release I was using a much older version of MagickNET (IM6.5.3) and CompositeOperator.CopyOpacity worked perfectly.

*Edit - sorry it's also worth mentioning that the mask is generated by emgucv hence the "mask.ToBitmap()" line.

Many thanks
Coordinator
Aug 23, 2013 at 2:27 PM
Can you share a link to your input image and your mask? And maybe also an image of the expected result.
Aug 28, 2013 at 8:58 AM
Yeah no problem, it is a green screen image. I'd rather not provide one with people in it so here is a blank image.

Mask:
Image

Original:
Image
Aug 30, 2013 at 7:55 AM
I got round the problem by using the MagickImage.Transparent method on the mask before blurring it, just with black as the color argument, then blurred the mask to smooth the edge.
magickMask.Transparent(new MagickColor(0,0,0));
if (blur != 0) magickMask.Blur(-blur, Math.Sqrt(blur), Channels.All);
first.Composite(magickMask, Gravity.Center, CompositeOperator.CopyOpacity);
Still seems like this solution is one step longer than it need be, though.
Coordinator
Aug 31, 2013 at 8:29 PM
I noticed that your mask is a grayscale image. So when you copy the alpha channel you probably copy noting. I tried using the alpha method to enable the alpha channel:
magickMask.Alpha(AlphaOption.Background);
Would you mind posting your expected result, so I can compare it with my image?