最大连通域

        //访问标志
        private static int[,] visited ;
        
        
        
        /// <summary>
        /// 求最大连通区域
        /// </summary>
        /// <param name="image">二值化的图像</param>
        public static void GettheMaxBlock(Image<Bgr, Byte> image)
        {
            int comps;
            visited = new int[image.Height, image.Width];
            
            for (int j = 0; j < image.Height; j++)
            {
                for (int k = 0; k < image.Width; k++)
                {
                    visited[j, k] = 0;
                }
            }

            comps = DFSTraverse(image);
            int[] compenents = new int[comps];
            for (int i = 0; i < image.Height; i++)
            {
                for (int j = 0; j < image.Width; j++)
                {
                    if (visited[i, j] != 0)
                        compenents[visited[i, j]]++;
                }

            }

            int MI = MaxIndex(compenents);
            for (int i = 0; i < image.Height; i++)
            {
                for (int j = 0; j < image.Width; j++)
                {
                    if (visited[i, j] != MI)
                    {
                        image[i, j] = BlackBgr;
                    }
                    else
                    {
                        image[i, j] = WhiteBgr;
                    }

                }
            }
        }
        /// <summary>
        /// 非递归深度搜索,二维数组表示的矩阵
        /// </summary>
        /// <param name="Graph"></param>
        /// <returns></returns>
        private static int DFSTraverse(Image<Bgr, Byte> Graph)
        {
            Stack<Point> stack = new Stack<Point>();
            int n = 1;
            for (int i = 0; i < Graph.Height; i++)
            {
                for (int j = 0; j < Graph.Width; j++)
                {
                    Point current = new Point(i, j);
                    if (i < 0 || i > Graph.Height - 1 || j < 0 || j > Graph.Width - 1 || Graph[i, j].Equals(BlackBgr))
                    {

                    }
                    //if white zone but visited, return
                    if (Graph[i, j].Equals(WhiteBgr) && visited[i, j] > 0)
                    {

                    }
                    else
                    {
                        stack.Push(current);
                        visited[i, j] = n;

                        while (stack.Count != 0)
                        {
                            current = stack.Peek();
                            if (!Findneighbors(Graph, current.X, current.Y).IsEmpty)
                            {
                                current = Findneighbors(Graph, current.X, current.Y);
                                stack.Push(current);
                                visited[current.X, current.Y] = n;
                            }
                            else
                            {
                                stack.Pop();
                            }
                        }
                        n++;
                    }


                }

            }
            return n;
        }
        /// <summary>
        /// 8领域寻找连通点
        /// </summary>
        /// <param name="Graph">二值化图像</param>
        /// <param name="x">当前点x坐标</param>
        /// <param name="y">当前点y坐标</param>
        /// <returns></returns>
        private static Point Findneighbors(Image<Bgr, Byte> Graph, int x, int y)
        {

            // 左上   
            if (x - 1 < 0 || x - 1 > Graph.Height - 1 || y - 1 < 0 || y - 1 > Graph.Width - 1)
            {
                return new Point();
            }

            if (Graph[x - 1, y - 1].Equals(WhiteBgr) && visited[x - 1, y - 1] == 0)
            {

                return new Point(x - 1, y - 1);
            }
            // 正上   
            if (x - 1 < 0 || x - 1 > Graph.Height - 1 || y < 0 || y > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x - 1, y].Equals(WhiteBgr) && visited[x - 1, y] == 0)
            {

                return new Point(x - 1, y);
            }
            // 右上    
            if (x - 1 < 0 || x - 1 > Graph.Height - 1 || y + 1 < 0 || y + 1 > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x - 1, y + 1].Equals(WhiteBgr) && visited[x - 1, y + 1] == 0)
            {

                return new Point(x - 1, y + 1);
            }
            // 正右 
            if (x < 0 || x > Graph.Height - 1 || y + 1 < 0 || y + 1 > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x, y + 1].Equals(WhiteBgr) && visited[x, y + 1] == 0)
            {

                return new Point(x, y + 1);
            }
            // 右下  
            if (x + 1 < 0 || x + 1 > Graph.Height - 1 || y + 1 < 0 || y + 1 > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x + 1, y + 1].Equals(WhiteBgr) && visited[x + 1, y + 1] == 0)
            {

                return new Point(x + 1, y + 1);
            }
            // 正下  
            if (x + 1 < 0 || x + 1 > Graph.Height - 1 || y < 0 || y > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x + 1, y].Equals(WhiteBgr) && visited[x + 1, y] == 0)
            {

                return new Point(x + 1, y);
            }
            // 左下
            if (x + 1 < 0 || x + 1 > Graph.Height - 1 || y - 1 < 0 || y - 1 > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x + 1, y - 1].Equals(WhiteBgr) && visited[x + 1, y - 1] == 0)
            {

                return new Point(x + 1, y - 1);
            }
            // 正左  
            if (x < 0 || x > Graph.Height - 1 || y - 1 < 0 || y - 1 > Graph.Width - 1)
            {
                return new Point();
            }
            if (Graph[x, y - 1].Equals(WhiteBgr) && visited[x, y - 1] == 0)
            {

                return new Point(x, y - 1);
            }
            return new Point();
        }
        /// <summary>
        /// 返回数组中最大元素的索引
        /// </summary>
        /// <param name="m_Array">数组</param>
        /// <returns>最大元素的索引</returns>
        private static int MaxIndex(int[] m_Array)
        {
            int m_Index = 0;
            int m_Max = m_Array[0];

            for (int i = 0; i < m_Array.Length; i++)
            {
                if (m_Array[i] > m_Max)
                {
                    m_Max = m_Array[i];
                    m_Index = i;
                }
            }

            return m_Index;
        }

  

原文地址:https://www.cnblogs.com/zhaos/p/2100867.html