Ejemplo n.º 1
0
        /// <summary>
        /// Find all connected-components in the image and return related information.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<ConnectedComponent> Find_All_Connected_Components(Bitmap source)
        {
            int i = 0, j = 0;
            int tw = 0, th = 0;

            int width = source.Width;
            int height = source.Height;

            // The array which tells whether the point at position[i,j] has been visited.
            bool[,] visited = new bool[width, height];

            List<ConnectedComponent> ccResult = new List<ConnectedComponent>();

            try
            {
                for (i = 0; i < width; i++)
                {
                    for (j = 0; j < height; j++)
                    {
                        if (Judge_White_Point(source.GetPixel(i, j)) && !visited[i, j])
                        {
                            visited[i, j] = true;

                            ConnectedComponent currentCC = new ConnectedComponent();

                            currentCC.PointSet.Add(new iPoint(i, j));
                            try
                            {
                                BFS(i, j, ref visited, currentCC, source);
                            }
                            catch (Exception ea)
                            {
                                throw new Exception(ea.Message);
                            }

                            Format_Connected_Component(currentCC);

                            tw = currentCC.Right_Bottom.Width_Position - currentCC.Left_Top.Width_Position;
                            th = currentCC.Right_Bottom.Height_Position - currentCC.Left_Top.Height_Position;

                            if (tw < width / 4 && (tw >= Config.CC_Size_Threshold || th >= Config.CC_Size_Threshold))
                            {
                                ccResult.Add(currentCC);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return ccResult;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Write one connected component to certain bmp image.
        /// </summary>
        /// <param name="ccImage"></param>
        /// <param name="sourceImage"></param>
        public static Bitmap Save_Connected_Component_As_Bmp(ConnectedComponent ccImage, Bitmap sourceImage)
        {
            int width = ccImage.Right_Bottom.Width_Position - ccImage.Left_Top.Width_Position;
            int height = ccImage.Right_Bottom.Height_Position - ccImage.Left_Top.Height_Position;

            Bitmap ccBmp = new Bitmap(width + 1, height + 1, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics ccGraphics = Graphics.FromImage(ccBmp);

            // Set the background color as black.
            ccGraphics.Clear(Color.FromArgb(0, 0, 0));

            ccGraphics.Dispose();

            foreach (iPoint point in ccImage.PointSet)
            {
                ccBmp.SetPixel(point.Width_Position - ccImage.Left_Top.Width_Position,
                               point.Height_Position - ccImage.Left_Top.Height_Position,
                               sourceImage.GetPixel(point.Width_Position, point.Height_Position)
                );
            }

            return ccBmp;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Using breadth first search to find all white connected components.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="visited"></param>
        /// <param name="ccResult"></param>
        public static void BFS(int width, int height, ref bool[,] visited, ConnectedComponent ccResult, Bitmap source)
        {
            int i = 0;
            int len = 4;
            int tx = 0, ty = 0;

            int[] dirX = new int[] { 0, 1, 0, -1 };
            int[] dirY = new int[] { 1, 0, -1, 0 };

            visited[width, height] = true;

            Queue<iPoint> pointQueue = new Queue<iPoint>();

            try
            {
                pointQueue.Enqueue(new iPoint(width, height));
                while (pointQueue.Count > 0)
                {
                    iPoint tmp = pointQueue.Dequeue();
                    ccResult.PointSet.Add(new iPoint(tmp.Width_Position, tmp.Height_Position));
                    for (i = 0; i < len; i++)
                    {
                        tx = tmp.Width_Position + dirX[i];
                        ty = tmp.Height_Position + dirY[i];
                        if (tx >= 0 && tx < source.Width &&
                            ty >= 0 && ty < source.Height &&
                            !visited[tx, ty] &&
                            Judge_White_Point(source.GetPixel(tx, ty)))
                        {
                            visited[tx, ty] = true;
                            pointQueue.Enqueue(new iPoint(tx, ty));
                        }
                    }
                }
            }
            catch (Exception ecc)
            {
                throw new Exception(ecc.Message);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// According to the point information to find the left-top and right-bottom point.
        /// </summary>
        /// <param name="currentCC"></param>
        public static void Format_Connected_Component(ConnectedComponent currentCC)
        {
            int minWidth = int.MaxValue;
            int maxWidth = int.MinValue;
            int minHeight = int.MaxValue;
            int maxHeight = int.MinValue;

            foreach (iPoint point in currentCC.PointSet)
            {
                if (point.Width_Position < minWidth)
                {
                    minWidth = point.Width_Position;
                }
                if (point.Width_Position > maxWidth)
                {
                    maxWidth = point.Width_Position;
                }
                if (point.Height_Position < minHeight)
                {
                    minHeight = point.Height_Position;
                }
                if (point.Height_Position > maxHeight)
                {
                    maxHeight = point.Height_Position;
                }
            }

            currentCC.Left_Top = new iPoint(minWidth, minHeight);
            currentCC.Right_Bottom = new iPoint(maxWidth, maxHeight);
        }
Ejemplo n.º 5
0
 public ConnectedComponent(ConnectedComponent _cc)
 {
     iniParameter(_cc.Left_Top, _cc.Right_Bottom, _cc.PointSet);
 }