Beispiel #1
0
        /// <summary>
        /// Find long and connected black lines.
        /// right - left : (width - 1, 0, -1, source)
        /// left - right : (0, width - 1, 1, source)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="inc"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="matrix"></param>
        /// <returns></returns>
        public static iLine Find_Long_Connected_Lines(int start, int end, int inc, int w, int h, byte[,] matrix)
        {
            int trend = 0;
            int i = 0, j = 0;
            int pre_hs = -1, pre_he = -1;
            int las_hs = -1, las_he = -1;
            int s = -1, hs = -1, he = -1;
            iLine iline = new iLine();

            for (i = start; i != end && hs == -1 && he == -1; i += inc)
            {
                for (j = 0; j <= h - 1; j++)
                {
                    if (matrix[i, j] == 1)
                    {
                        if (s == -1) { s = i; }
                        if (hs == -1) { hs = j; }
                    }
                    else
                    {
                        if (hs != -1 && he == -1) { he = j - 1; break; }
                    }
                }
            }

            pre_hs = las_hs = hs;
            pre_he = las_he = he;

            iline.Add(true, s, hs, he);

            hs = he = -1;

            Find_Vertical_Black_Line_Segment(s + inc, ref hs, ref he, trend, las_hs, las_he, w, h, matrix);

            las_hs = hs;
            las_he = he;

            trend = (las_hs + (las_he - las_hs) / 2) - (pre_hs + (pre_he - pre_hs) / 2);

            iline.Add(true, s + inc, hs, he);

            hs = he = -1;

            for (i = s + 2 * inc; i != end; i += inc)
            {
                Find_Vertical_Black_Line_Segment(i, ref hs, ref he, trend, las_hs, las_he, w, h, matrix);

                if (hs == -1 && he == -1)
                {
                    break;
                }

                pre_hs = las_hs;
                pre_he = las_he;
                las_hs = hs;
                las_he = he;
                hs = -1;
                he = -1;
                trend = (las_hs + (las_he - las_hs) / 2) - (pre_hs + (pre_he - pre_hs) / 2);

                iline.Add(true, i, las_hs, las_he);
            }

            Deal_With_ILines(iline, w, h, matrix);

            return iline;
        }
        /// <summary>
        /// Find long and connected black lines.
        /// right - left : (width - 1, 0, -1, source)
        /// left - right : (0, width - 1, 1, source)
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="inc"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static iLine Find_Long_Connected_Lines(int start, int end, int inc, Bitmap source)
        {
            // The line ascend or descend trend
            // which equals to left - right
            // == 0 level
            // > 0 ascend
            // < 0 descend
            int trend = 0;
            int i = 0, j = 0;
            int pre_hs = -1, pre_he = -1;
            int las_hs = -1, las_he = -1;
            int s = -1, hs = -1, he = -1;
            iLine iline = new iLine();

            try
            {
                // From right to left, to find the start position
                // Find the first vertical black line, from right to left or in the reverse direction.
                for (i = start; i != end && hs == -1 && he == -1; i += inc)
                {
                    for (j = 0; j <= source.Height - 1; j++)
                    {
                        Color tmp = source.GetPixel(i, j);
                        if (tmp.R + tmp.G + tmp.B == 0)
                        {
                            // if this vertical single line is the rightmost single black line
                            if (s == -1) { s = i; }

                            // if this point is the topest point of this single black line
                            if (hs == -1) { hs = j; }
                        }
                        else
                        {
                            // if this point is the bottomest point of this single black line
                            if (hs != -1 && he == -1) { he = j - 1; break; }
                        }
                    }
                }

                pre_hs = las_hs = hs;
                pre_he = las_he = he;

                iline.Add(true, s, hs, he);

                hs = he = -1;

                Operations.Find_Vertical_Black_Line_Segment(s + inc, ref hs, ref he, trend, las_hs, las_he, source);

                las_hs = hs;
                las_he = he;

                trend = (las_hs + (las_he - las_hs) / 2) - (pre_hs + (pre_he - pre_hs) / 2);

                iline.Add(true, s + inc, hs, he);

                hs = he = -1;

                for (i = s + 2 * inc; i != end; i += inc)
                {
                    Operations.Find_Vertical_Black_Line_Segment(i, ref hs, ref he, trend, las_hs, las_he, source);

                    if (hs == -1 && he == -1)
                    {
                        break;
                    }

                    pre_hs = las_hs;
                    pre_he = las_he;
                    las_hs = hs;
                    las_he = he;
                    hs = -1;
                    he = -1;
                    trend = (las_hs + (las_he - las_hs) / 2) - (pre_hs + (pre_he - pre_hs) / 2);

                    iline.Add(true, i, las_hs, las_he);
                }

                Deal_With_ILines(iline, source);

                return iline;
            }
            catch (Exception e)
            {
                throw new Exception("Find_Long_Connected_Lines:" + e.Message);
            }
        }