Esempio n. 1
0
        void RecursiveFill(ref FloodFillStruct floodFill, int index, int width, int height)
        {
            if (index >= floodFillData.Length || floodFillData[index].R <= FINGERTHRESHOLD || floodFill.visitedPixels.ContainsKey(index))
            {
                return;
            }

            floodFill.visitedPixels.Add(index, index);
            int     xP     = (index % width);
            int     yP     = (index / width);
            Vector2 posVec = new Vector2((float)xP / (float)width, (float)yP / (float)height) * 2.0f - Vector2.One;

            posVec.Y          *= -1; //Invert yvec
            floodFill.rect.Min = Vector2.Min(floodFill.rect.Min, posVec);
            floodFill.rect.Max = Vector2.Max(floodFill.rect.Max, posVec);
            if (xP - 1 >= 0)
            {
                RecursiveFill(ref floodFill, index - 1, width, height);
            }
            if (xP + 1 < width)
            {
                RecursiveFill(ref floodFill, index + 1, width, height);
            }
            if (yP - 1 >= 0)
            {
                RecursiveFill(ref floodFill, index - width, width, height);
            }
            if (yP + 1 < height)
            {
                RecursiveFill(ref floodFill, index + width, width, height);
            }
        }
Esempio n. 2
0
        BoundingRect[] FloodFillBounds(Texture2D image)
        {
            if (floodFillData == null)
            {
                floodFillData = new Color[image.Width * image.Height];
            }

            image.GetData <Color>(floodFillData);

            List <FloodFillStruct> floodFills = new List <FloodFillStruct>();

            for (int i = 0; i < floodFillData.Length; i++)
            {
                if (floodFillData[i].R > FINGERTHRESHOLD)
                {
                    bool process = true;
                    for (int j = 0; j < floodFills.Count; j++)
                    {
                        if (floodFills[j].visitedPixels.ContainsKey(i))
                        {
                            process = false;
                            break;
                        }
                    }

                    if (process)
                    {
                        FloodFillStruct fill = new FloodFillStruct(0);
                        RecursiveFill(ref fill, i, image.Width, image.Height);
                        floodFills.Add(fill);
                    }
                }
            }

            List <BoundingRect> rects = new List <BoundingRect>();
            int detectArea            = (int)((float)(image.Width * image.Height) * MINPIXELBOUNDS);

            for (int i = 0; i < floodFills.Count; i++)
            {
                if (floodFills[i].visitedPixels.Count >= detectArea)
                {
                    rects.Add(floodFills[i].rect);
                }
            }

            return(rects.ToArray());
        }
        void RecursiveFill(ref FloodFillStruct floodFill, int index, int width, int height)
        {
            if(index >= floodFillData.Length || floodFillData[index].R <= FINGERTHRESHOLD || floodFill.visitedPixels.ContainsKey(index) )
                return;

            floodFill.visitedPixels.Add(index, index);
            int xP = (index % width);
            int yP = (index / width);
            Vector2 posVec = new Vector2((float)xP / (float)width, (float)yP / (float)height) * 2.0f - Vector2.One;
            posVec.Y *= -1; //Invert yvec
            floodFill.rect.Min = Vector2.Min(floodFill.rect.Min, posVec);
            floodFill.rect.Max = Vector2.Max(floodFill.rect.Max, posVec);
            if (xP - 1 >= 0)
            {
                RecursiveFill(ref floodFill, index - 1, width, height);
            }
            if (xP + 1 < width)
            {
                RecursiveFill(ref floodFill, index + 1, width, height);
            }
            if (yP - 1 >= 0)
            {
                RecursiveFill(ref floodFill, index - width, width, height);
            }
            if (yP + 1 < height)
            {
                RecursiveFill(ref floodFill, index + width, width, height);
            }
        }
        BoundingRect[] FloodFillBounds(Texture2D image)
        {
            if (floodFillData == null)
            {
                floodFillData = new Color[image.Width * image.Height];
            }

            image.GetData<Color>(floodFillData);

            List<FloodFillStruct> floodFills = new List<FloodFillStruct>();

            for (int i = 0; i < floodFillData.Length; i++)
            {
                if (floodFillData[i].R > FINGERTHRESHOLD)
                {
                    bool process = true;
                    for (int j = 0; j < floodFills.Count; j++)
                    {
                        if (floodFills[j].visitedPixels.ContainsKey(i))
                        {
                            process = false;
                            break;
                        }
                    }

                    if (process)
                    {
                        FloodFillStruct fill = new FloodFillStruct(0);
                        RecursiveFill(ref fill, i, image.Width, image.Height);
                        floodFills.Add(fill);
                    }

                }
            }

            List<BoundingRect> rects = new List<BoundingRect>();
            int detectArea = (int)((float)(image.Width * image.Height) * MINPIXELBOUNDS);

            for (int i = 0; i < floodFills.Count; i++)
            {
                if (floodFills[i].visitedPixels.Count >= detectArea)
                    rects.Add(floodFills[i].rect);
            }

            return rects.ToArray();
        }