Example #1
0
        internal override void Minimum(Image originalImage, KernelFunction kernelFunction, uint neighborhoodSize, uint x, uint y, Image outputImage)
        {
            ImageGray <T> castedOriginalImage = (ImageGray <T>)originalImage;
            ImageGray <T> castedOutputImage   = (ImageGray <T>)outputImage;

            castedOutputImage.Gray.Minimum(castedOriginalImage.Gray, kernelFunction, x, y, neighborhoodSize);
        }
Example #2
0
        public override Image Clone()
        {
            ImageGray <T> clone = (ImageGray <T>)ImageFactory.Create(Width, Height, ColorModel.GRAY, GetDataType());

            clone.Gray = (GenericChannel <T>)Gray.Clone();
            return(clone);
        }
Example #3
0
        public Image <byte> Detect(Image <byte> image)
        {
            // Detect faces
            var dtor = this.detectors[this.detectorType];

            dtor.Detect(image);

            // Detect face feature points
            Image <byte> gray      = new ImageGray(image);
            var          ator      = this.alignmentors[this.alignmentType];
            var          faceRects = from f in dtor.Results select f.Rect;

            ator.Align(gray, faceRects);

            // Render the result
            int colorIndex = ((int)this.detectorType - 1) * this.Alignmentors.Count() + ((int)this.alignmentType - 1);

            using (var render = Renderer.Create(new ImageARGB(image)))
            {
                results = ator.Results;
                foreach (var shape in ator.Results)
                {
                    render.DrawPoints(shape, this.radias, this.colors[colorIndex]);
                    foundFace = true;
                }

                return(render.Image);
            }
        }
Example #4
0
        private void FaceSwapInTwoImages(
            Image <byte> argbImage1,
            Image <byte> argbImage2,
            Image <byte> facepointSource,
            out Image <byte> swapResult1)
        {
            if (argbImage1 == null || argbImage1.Channels < Color.RgbChannels)
            {
                throw new ArgumentException("Input image 1 should be a valid color image.");
            }

            if (argbImage2 == null || argbImage2.Channels < Color.RgbChannels)
            {
                throw new ArgumentException("Input image 2 should be a valid color image.");
            }

            // face detection
            Image <byte> imgGray1 = new ImageGray(facepointSource);  //argbImage1

            Stopwatch sw     = Stopwatch.StartNew();
            Stopwatch sw2    = Stopwatch.StartNew();
            var       faces1 = detector.Detect(imgGray1);

            if (faces1.Length < 1)
            {
                throw new InvalidOperationException("Fail to detect face in image 1.");
            }


            // pick two largest faces
            var face1 = faces1.OrderByDescending(y => y.Rect.Area).First();

            Image <byte> imgGray2 = new ImageGray(argbImage2);

            sw.Restart();
            var faces2 = detector.Detect(imgGray2);

            if (faces2.Length < 1)
            {
                throw new InvalidOperationException("Fail to detect face in image 2.");
            }

            var face2 = faces2.OrderByDescending(y => y.Rect.Area).First();

            sw.Restart();
            var alignmentShape1 = alignmentor.Align(imgGray1, face1.Rect);

            sw.Restart();
            var alignmentShape2 = alignmentor.Align(imgGray2, face2.Rect);

            // create the face point file
            FaceSwapper.SaveTestData("points-Source", argbImage1, alignmentShape1);
            FaceSwapper.SaveTestData("facepoints-Source", facepointSource, alignmentShape1);
            FaceSwapper.SaveTestData("points-target", argbImage2, alignmentShape2);

            swapResult1 = argbImage2.Clone();

            sw.Restart();
            FaceSwapInternal(argbImage1, argbImage2, alignmentShape1, alignmentShape2, swapResult1);
        }
Example #5
0
        internal override GenericImage <T> Add(GenericImage <T> other)
        {
            ImageGray <T> outcome   = (ImageGray <T>)ImageFactory.Create(Width, Height, GetColorModel(), GetDataType());
            ImageGray <T> otherGray = (ImageGray <T>)other.ToGray();

            outcome.Gray = Gray.Add(otherGray.Gray);
            return(outcome);
        }
    public override void OnInspectorGUI()
    {
        // Show default inspector property editor
        DrawDefaultInspector();
        ImageGray ig = (ImageGray)target;

        ig.Gray = GUILayout.Toggle(ig.Gray, "Gray");
    }
Example #7
0
    /// <summary>
    /// 战斗中,最下方一排的英雄显示赋值
    /// </summary>
    /// <param name="fightHero"></param>
    /// <param name="node"></param>
    void SetShowNodeData(FightHero fightHero, UINode node)
    {
        node.GetRef("Icon").GetComponent <Image>().sprite     = GameResSys.Instance.GetCard(fightHero.CreatureData.Icon);
        node.GetRef("Name").GetComponent <Text>().text        = fightHero.CreatureData.Name;
        node.GetRef("Hp").GetComponent <Text>().text          = "血";
        node.GetRef("HpSlider").GetComponent <Slider>().value = fightHero.CreatureData.HpPercent;
        node.GetRef("Mp").GetComponent <Text>().text          = "气";
        node.GetRef("MpSlider").GetComponent <Slider>().value = fightHero.CreatureData.MpPercent;

        ImageGray ig = node.GetRef("Icon").GetComponent <ImageGray>();

        ig.Gray = fightHero.CreatureData.HpPercent <= 0;
    }
Example #8
0
        /// <summary>
        /// Detect faces in the given image
        /// </summary>
        /// <param name="image">Image object</param>
        public void Detect(Image <byte> image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image", "Invalid parameter.");
            }

#if CALL_ADVANCED_DETECTION
            //
            // You can fine control the detection behavior by providing more information, like
            // whether to use color information, the face area in the image, the minimum and maximum face size,
            // whether to use post-filter (post filter can efficiently prune false positives),
            // the search step (in pixel) between neighboring sliding windows (in both horizontal and vertical directions).
            //
            // The following code shows how to call the advanced Detect method
            //

            // Assume the input is a color image, convert it to grayscale.
            ImageGray gray = new ImageGray(image);

            // The minimum detectable face size is given by Detector.MinResolution. Small faces below that resolution
            // will not be included in the detection result. For convenince, we specify (0,0) here.
            //
            // The maximum detectable face size is given by Detector.MaxResolution. Large faces beyond that resolution
            // will not be included in the detection result. For convenince, we specify image size here.
            //
            // The step ratio specifies the gaps (in pixel) between neighboring sliding windows (a sliding window defines
            // a search area) in both horiztonal and vertical directions. For example, if the window size is 40x40, given
            // a step ratio of 0.1, the gaps will be 4 pixels.
            // A smaller ratio probably will improve the recall, but takes more time. We use 0.1 as a default value.
            //
            // The post filter flag indicates whether to apply post filter over the raw detection result. By default, we
            // turn on this flag to efficiently remove false positives.
            //
            // The search region parameter defines the local area to search the faces. By default, we search the entire image.
            //
            // If the color image is provided, the detector will employ the color information to search the faces.
            // For example, it will first label the possible skin region and then search faces on those regions only, which
            // can greatly improve the detection speed.

            this.faces = this.detector.Detect(gray, Size.Empty, gray.Size, 0.1f, true, gray.Region, image);
#else
            // If the input is grayscale image, the Detect method will search the face over the entire
            // image region based on the local patterns.
            // If the input is a color image (RGB or ARGB), internally the Detect method will
            // convert it to grayscale, and employ color information to search the face area.
            this.faces = this.detector.Detect(image);
#endif
        }
Example #9
0
    void OnGetRewardReturnToStage(object p1, object p2)
    {
        //update Node
        int       nodeId    = (int)p2;
        Transform nodeTrans = _nodeUIs[nodeId];
        ImageGray ig        = nodeTrans.gameObject.AddComponent <ImageGray>();

        ig.Gray = true;

        FightHero hero = (FightHero)p1;
        Transform t    = nodeTrans.GetComponent <UINode>().GetRef("HeroRoot");

        _fightHeroNodes[hero.Id].transform.SetParent(t);
        SetPathPassed(hero.LastNodeId, hero.NowNodeId, ((HeroData)hero.CreatureData).TheColor);
    }
Example #10
0
        /// <summary>
        /// Detect faces in the given image
        /// </summary>
        /// <param name="image">Image object</param>
        public void Detect(Image<byte> image)
        {
            if (image == null) throw new ArgumentNullException("image", "Invalid parameter.");

#if CALL_ADVANCED_DETECTION
            // 
            // You can fine control the detection behavior by providing more information, like
            // whether to use color information, the face area in the image, the minimum and maximum face size,
            // whether to use post-filter (post filter can efficiently prune false positives), 
            // the search step (in pixel) between neighboring sliding windows (in both horizontal and vertical directions).
            // 
            // The following code shows how to call the advanced Detect method
            //

            // Assume the input is a color image, convert it to grayscale.
            ImageGray gray = new ImageGray(image);

            // The minimum detectable face size is given by Detector.MinResolution. Small faces below that resolution 
            // will not be included in the detection result. For convenince, we specify (0,0) here.
            //
            // The maximum detectable face size is given by Detector.MaxResolution. Large faces beyond that resolution 
            // will not be included in the detection result. For convenince, we specify image size here.
            //
            // The step ratio specifies the gaps (in pixel) between neighboring sliding windows (a sliding window defines 
            // a search area) in both horiztonal and vertical directions. For example, if the window size is 40x40, given 
            // a step ratio of 0.1, the gaps will be 4 pixels.
            // A smaller ratio probably will improve the recall, but takes more time. We use 0.1 as a default value.
            //
            // The post filter flag indicates whether to apply post filter over the raw detection result. By default, we 
            // turn on this flag to efficiently remove false positives.
            //
            // The search region parameter defines the local area to search the faces. By default, we search the entire image.
            //
            // If the color image is provided, the detector will employ the color information to search the faces. 
            // For example, it will first label the possible skin region and then search faces on those regions only, which
            // can greatly improve the detection speed.

            this.faces = this.detector.Detect(gray, Size.Empty, gray.Size, 0.1f, true, gray.Region, image); 
#else
            // If the input is grayscale image, the Detect method will search the face over the entire
            // image region based on the local patterns.
            // If the input is a color image (RGB or ARGB), internally the Detect method will
            // convert it to grayscale, and employ color information to search the face area.
            this.faces = this.detector.Detect(image);
#endif
        }
Example #11
0
    void OnGetSafeReturnToStage(object p1, object p2)
    {
        FightHero hero = (FightHero)p1;
        //
        Transform nodeTrans = _nodeUIs[hero.LastNodeId];
        ImageGray ig        = nodeTrans.gameObject.AddComponent <ImageGray>();

        ig.Gray = true;
        //
        nodeTrans = _nodeUIs[hero.NowNodeId];
        UINode    node = nodeTrans.GetComponent <UINode>();
        Transform t    = node.GetRef("HeroRoot");

        _fightHeroNodes[hero.Id].transform.SetParent(t);

        SetPathPassed(hero.LastNodeId, hero.NowNodeId, ((HeroData)hero.CreatureData).TheColor);

        if (node.GetRef("HeroRoot").childCount > 1)
        {
            node.GetRef("All").gameObject.SetActive(true);
            Dragable dragable = node.GetRef("All").gameObject.GetComponent <Dragable>();
            if (dragable == null)
            {
                dragable = node.GetRef("All").gameObject.AddComponent <Dragable>();
            }
            dragable.HasTail     = true;
            dragable.TailColor   = Color.white;
            dragable.TailSprite  = GameResSys.Instance.GetMask(GameConstants.CommonDragTail);
            dragable.TailWidth   = 30;
            dragable.ActionId    = hero.NowNodeId;
            dragable.Canv        = _stageCanvas;
            dragable.OnDragStart = i =>
            {
                _dragFrom = DragFrom.All;
                _dragId   = hero.NowNodeId;
            };
        }
    }
Example #12
0
    void OnEnemyAttack(object p1, object p2)
    {
        FightHero hero   = (FightHero)p1;
        int       damage = (int)p2;

        UINode node = _heroNodes[hero.Id].GetComponent <UINode>();

        SetHeroData(hero, node);
        node.GetRef("Selected").gameObject.SetActive(false);
        ShowHurt(node, damage);

        if (hero.CreatureData.Hp.Value <= 0)
        {
            ImageGray imageGray = node.GetRef("Icon").gameObject.GetComponent <ImageGray>();
            if (imageGray == null)
            {
                imageGray = node.GetRef("Icon").gameObject.AddComponent <ImageGray>();
            }
            imageGray.Gray = true;
        }

        ShowItemDes(string.Empty, null);
    }
Example #13
0
    void OnFightWinReturnStage(object p1, object p2)
    {
        //update Node
        int       nodeId = (int)p2;
        ImageGray ig     = _nodeUIs[nodeId].gameObject.AddComponent <ImageGray>();

        ig.Gray = true;

        Dictionary <int, FightHero> heros = (Dictionary <int, FightHero>)p1;

        foreach (KeyValuePair <int, FightHero> pair in heros)
        {
            int hId = pair.Value.Id;
            //change pos
            Transform targetTrans = _nodeUIs[pair.Value.NowNodeId].GetComponent <UINode>().GetRef("HeroRoot");
            _fightHeroNodes[hId].SetParent(targetTrans);
            //update data
            UINode showNode = _showHeroNodes[hId].GetComponent <UINode>();
            SetShowNodeData(pair.Value, showNode);

            Color c = ((HeroData)pair.Value.CreatureData).TheColor;
            SetPathPassed(pair.Value.LastNodeId, pair.Value.NowNodeId, c);
        }
    }
        //主执行程序
        protected override void RunAct()
        {
            base.RunAct();


            #region 初始化

            if (!engineIsnitial)
            {
                MyEngine = new HDevEngine();
                InitialEngine();
            }
            RuningFinish  = false;
            base.Result   = string.Empty;
            base.IsOk     = false;
            base.isRealOk = false;
            //功能块
            bFindShapeMode    = false;
            bGrabPointSetting = false;

            if (ImageTestIn == null || !ImageTestIn.IsInitialized())
            {
                return;
            }
            int channels = ImageTestIn.CountChannels();

            HImage ImageGray;
            if (channels == 3)
            {
                ImageGray = ImageTestIn.Rgb1ToGray();
            }
            else
            {
                ImageGray = ImageTestIn.Clone();
            }

            #endregion

            #region 创建模板
            if (MCreateShapeModel.hShapeModel == null || !MCreateShapeModel.hShapeModel.IsInitialized() || MCreateShapeModel.createNewModelID)
            {
                if (!MCreateShapeModel.CreateShapeModelAct(ImageRefIn))
                {
                    Util.Notify("创建模板异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("创建模板异常");
                    }
                    return;
                }
            }
            #endregion

            #region 模板匹配
            bFindShapeMode = MFindShapeMode.FindShapeModeAct(ImageRefIn, MCreateShapeModel, ImageTestIn);
            if (!bFindShapeMode)
            {
                Util.Notify("查找模板异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("查找模板异常");
                }
                return;
            }
            List <HHomMat2D> homMat2Ds = new List <HHomMat2D>();
            homMat2Ds = MFindShapeMode.GetHHomMat2Ds();
            if (MFindShapeMode.row == null || MFindShapeMode.row.Length < 1 || homMat2Ds == null)
            {
                Util.Notify("未找到模板");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("未找到模板");
                }
                base.IsOk     = true;
                base.isRealOk = true;
                return;
            }
            if (bFindShapeMode)
            {
                StringBuilder strb = new StringBuilder();
                strb.Append("查找结果:" + Environment.NewLine);
                int count = MFindShapeMode.row.Length;
                for (int i = 0; i < count; i++)
                {
                    HTuple phi;
                    HOperatorSet.TupleDeg(MFindShapeMode.angle[i], out phi);
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "Row:{0:F2}\r\n" + "Col:{1:F2}\r\n" + "角度:{2:F2}\r\n" + "得分:{3:F2}"
                        , MFindShapeMode.row[i].D, MFindShapeMode.column[i].D, phi.D, MFindShapeMode.score[i].D);
                    strb.Append(mes + Environment.NewLine);
                }
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strb.ToString());
                }
                Util.Notify(strb.ToString());
            }


            #endregion

            #region 抓取点
            bGrabPointSetting = mGrabPointSetting.setTarget(homMat2Ds);
            if (!bGrabPointSetting)
            {
                Util.Notify("抓取点异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("抓取点异常");
                }
            }
            else
            {
                StringBuilder strd = new StringBuilder();
                for (int i = 0; i < MGrabPointSetting.GrabRowTarget.Length; i++)
                {
                    string mes = string.Format(
                        "第" + (i + 1) + "个抓取点像素坐标:\r\n" +
                        "Row: {0:F2}\r\n" +
                        "Col: {1:F2}",
                        MGrabPointSetting.GrabRowTarget[i].D,
                        MGrabPointSetting.GrabColTarget[i].D);
                    strd.Append(mes + Environment.NewLine);
                }
                Util.Notify(strd.ToString());
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strd.ToString());
                }
            }
            #endregion

            #region 防呆
            if (bFangDai_Enable)
            {
                bFangDai_Result = MFangDai.FangDai_Act(ImageGray, homMat2Ds);
                if (bFangDai_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("防呆:" + Environment.NewLine);
                    int count = MFangDai.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}"

                                                   , MFangDai.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("防呆检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("防呆检测异常");
                    }
                }
            }
            else
            {
                bFangDai_Result = true;
            }

            #endregion

            RuningFinish  = true;
            base.IsOk     = bFindShapeMode && bGrabPointSetting && bFangDai_Result;
            base.isRealOk = true;
            if (ImageGray != null && ImageGray.IsInitialized())
            {
                ImageGray.Dispose();
            }
        }
Example #15
0
        public Image<byte> Detect(Image<byte> image)
        {
            // Detect faces
            var dtor = this.detectors[this.detectorType];
            dtor.Detect(image);

            // Detect face feature points
            Image<byte> gray = new ImageGray(image);
            var ator = this.alignmentors[this.alignmentType];
            var faceRects = from f in dtor.Results select f.Rect;
            ator.Align(gray, faceRects);

            // Render the result
            int colorIndex = ((int)this.detectorType - 1) * this.Alignmentors.Count() + ((int)this.alignmentType - 1);
            using (var render = Renderer.Create(new ImageARGB(image)))
            {
                results = ator.Results;
                foreach (var shape in ator.Results)
                {
                    render.DrawPoints(shape, this.radias, this.colors[colorIndex]);
                    foundFace = true;
                }

                return render.Image;
            }
        }
Example #16
0
        private async Task <Tuple <Image <byte>, IList <PointF> > > PrepBitmapAsync(SoftwareBitmap bitmap)
        {
            if (bitmap.PixelHeight % 2 != 0)
            {
                var resized = new SoftwareBitmap(bitmap.BitmapPixelFormat, bitmap.PixelWidth, bitmap.PixelHeight + 1);
                bitmap.CopyTo(resized);
                bitmap = resized;
            }

            Rectangle firstFace;

            try
            {
                var detector = await FaceDetector.CreateAsync();

                var formats         = FaceDetector.GetSupportedBitmapPixelFormats();
                var convertedBitmap = SoftwareBitmap.Convert(bitmap, formats.First());
                var detected        = await detector.DetectFacesAsync(convertedBitmap);

                var faces = detected
                            .Select(x => x.FaceBox)
                            .Select(x => new Rectangle((int)x.X, (int)x.X + (int)x.Width, (int)x.Y, (int)x.Y + (int)x.Height));
                if (!faces.Any())
                {
                    return(null);
                }
                firstFace = faces.First();
            }
            catch (Exception)
            {
                Debugger.Break();
                throw;
            }

            IList <PointF> points;
            var            image = ConvertTo.Image.FromSoftwareBitmap(bitmap);

            try
            {
                if (alignmentor == null)
                {
                    using (var stream = ResourceManager.GetStream(ResourceKey.AsmAlignment))
                    {
                        alignmentor = FaceAlignmentorFactory.Create(FaceAlignmentType.Asm87Points, stream);
                    }
                }
                var grayImage = new ImageGray(image);
                points = alignmentor.Align(grayImage, firstFace).ToList();
                if (!points.Any())
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                Debugger.Break();
                throw;
            }

            return(new Tuple <Image <byte>, IList <PointF> >(image, points));
        }