示例#1
0
        private static void DrawMask(IInputOutputArray image, Mat mask, Rectangle rect, MCvScalar color)
        {
            using (Mat maskLarge = new Mat())
                using (Mat maskLargeInv = new Mat())
                    using (InputArray iaImage = image.GetInputArray())
                        using (Mat matImage = iaImage.GetMat())
                            using (Mat subRegion = new Mat(matImage, rect))
                                using (Mat largeColor = new Mat(
                                           subRegion.Size,
                                           Emgu.CV.CvEnum.DepthType.Cv8U,
                                           3))
                                {
                                    CvInvoke.Resize(mask, maskLarge, rect.Size);

                                    //give the mask at least 30% transparency
                                    using (ScalarArray sa = new ScalarArray(0.7))
                                        CvInvoke.Min(sa, maskLarge, maskLarge);

                                    //Create the inverse mask for the original image
                                    using (ScalarArray sa = new ScalarArray(1.0))
                                        CvInvoke.Subtract(sa, maskLarge, maskLargeInv);

                                    //The mask color
                                    largeColor.SetTo(color);
                                    if (subRegion.NumberOfChannels == 4)
                                    {
                                        using (Mat bgrSubRegion = new Mat())
                                        {
                                            CvInvoke.CvtColor(subRegion, bgrSubRegion,
                                                              ColorConversion.Bgra2Bgr);
                                            CvInvoke.BlendLinear(largeColor, bgrSubRegion, maskLarge,
                                                                 maskLargeInv, bgrSubRegion);
                                            CvInvoke.CvtColor(bgrSubRegion, subRegion,
                                                              ColorConversion.Bgr2Bgra);
                                        }
                                    }
                                    else
                                    {
                                        CvInvoke.BlendLinear(largeColor, subRegion, maskLarge, maskLargeInv,
                                                             subRegion);
                                    }
                                }
        }