示例#1
0
        public double CentroidDistance(Oblong s2)
        {
            int[]  c1   = this.Centroid();
            int[]  c2   = s2.Centroid();
            int    dx   = c2[1] - c1[1];
            int    dy   = c2[0] - c1[0];
            double dist = Math.Sqrt((dx * dx) + (dy * dy));

            return(dist);
        }
示例#2
0
        /// <summary>
        /// assume that the input matrix is purely binary, i.e. zeros and ones
        /// </summary>
        /// <param name="matrix">
        /// </param>
        /// <returns>
        /// The <see cref="ArrayList"/>.
        /// </returns>
        public static ArrayList ShapesDetect(double[,] matrix)
        {
            var shapes = new ArrayList();
            var random = new RandomNumber();

            int mHeight = matrix.GetLength(0);
            int mWidth  = matrix.GetLength(1);

            for (int x = 5; x < mWidth; x++)
            {
                for (int y = 0; y < mHeight - 1; y++)
                {
                    if (matrix[y, x] != 1.0)
                    {
                        continue; // not in an object
                    }

                    if (matrix[y + 1, x] != 1.0)
                    {
                        y++;
                        continue; // shape must be > 2 pixels wide
                    }

                    // explore shape in y dimension
                    int rowWidth = 0;
                    while (rowWidth + y < mHeight && matrix[y + rowWidth, x] == 1.0)
                    {
                        rowWidth++;
                    }

                    rowWidth--;                       // back off one place
                    int yCentre = y + (rowWidth / 2); // position in centre of shape

                    if (InExistingShape(yCentre, x, shapes))
                    {
                        continue;
                    }

                    // explore shape in x dimension
                    int upDist = 0;
                    while (x + upDist < mWidth && matrix[yCentre, x + upDist] == 1.0)
                    {
                        upDist++;
                    }

                    if (matrix[yCentre, x + 1] == 0.0)
                    {
                        upDist = 1;
                    }

                    int dnDist = 0;
                    while (x - dnDist > 0 && matrix[yCentre, x - dnDist] == 1.0)
                    {
                        dnDist++;
                    }

                    dnDist--; // pull back one

                    // initialise possible shape.
                    int col1     = x - dnDist + 1;
                    int colWidth = upDist + dnDist - 2;
                    var shape    = new Oblong(y, col1, y + rowWidth - 1, col1 + colWidth - 1);
                    shape.RandomNumber = random.GetInt(200); // set random number for id and color purposes

                    int[] centroid = shape.Centroid();

                    // LoggedConsole.WriteLine("Centroid=" + centroid[0] + ", " + centroid[1]);
                    // LoggedConsole.WriteLine("RowWidth=" + shape.RowWidth + "  ColWidth=" + shape.ColWidth);
                    shapes.Add(shape);

                    // more to end of shape
                    y = shape.RowBottom;
                }

                x += 4; // jump through 5 lines at a time.
            }

            LoggedConsole.WriteLine("Number of shapes=" + shapes.Count);

            shapes = ProcessShapes(shapes);

            // Console.ReadLine();
            return(shapes);
        }
示例#3
0
        /// <summary>
        /// MAIN METHOD FOR UNIT TESTING
        /// </summary>
        private static void Main()
        {
            LoggedConsole.WriteLine("TESTING METHODS IN CLASS DataTools");

            // string testDir = @"D:\SensorNetworks\Software\TowseyLib\TestResources\";
            bool doit1 = false;

            if (doit1)
            {
                // test1 method AREA, CENTROID and CENTROID-DISTANCE()
                LoggedConsole.WriteLine("Test Method Name()");
                int r1 = 10;
                int c1 = 10;
                int r2 = 20;
                int c2 = 20;
                var s1 = new Oblong(r1, c1, r2, c2);
                s1.WriteBounds();
                int[] centroid1 = s1.Centroid();
                LoggedConsole.WriteLine("Centroid1: r=" + centroid1[0] + "  c=" + centroid1[1]);
                LoggedConsole.WriteLine("Area 1=" + s1.Area());
                LoggedConsole.WriteLine();

                r1 = 17;
                c1 = 16;
                r2 = 23;
                c2 = 24;
                var s2 = new Oblong(r1, c1, r2, c2);
                s2.WriteBounds();
                int[] centroid2 = s2.Centroid();
                LoggedConsole.WriteLine("Centroid2: r=" + centroid2[0] + "  c=" + centroid2[1]);
                LoggedConsole.WriteLine("Area 2=" + s2.Area());
                double dist = s1.CentroidDistance(s2);
                LoggedConsole.WriteLine("Distance=" + dist);
            }

            // end test1

            bool doit2 = false;

            if (doit2)
            {
                // test2 method IncludesRow(), IncludesColumn(), PointInside()
                LoggedConsole.WriteLine("Test Method Name()");
                int r1 = 10;
                int c1 = 10;
                int r2 = 20;
                int c2 = 20;
                var s1 = new Oblong(r1, c1, r2, c2);
                s1.WriteBounds();
                r1 = 17;
                c1 = 16;
                r2 = 23;
                c2 = 24;
                var s2 = new Oblong(r1, c1, r2, c2);
                s2.WriteBounds();
                r1 = 20;
                c1 = 20;
                r2 = 30;
                c2 = 30;
                var s3 = new Oblong(r1, c1, r2, c2);
                s3.WriteBounds();

                LoggedConsole.WriteLine();
                LoggedConsole.WriteLine("Row10 in s1=" + s1.IncludesRow(10));
                LoggedConsole.WriteLine("Row15 in s1=" + s1.IncludesRow(15));
                LoggedConsole.WriteLine("Row20 in s1=" + s1.IncludesRow(20));
                LoggedConsole.WriteLine("Row25 in s1=" + s1.IncludesRow(25));
                LoggedConsole.WriteLine("Col05 in s1=" + s1.IncludesColumn(5));
                LoggedConsole.WriteLine("Col10 in s1=" + s1.IncludesColumn(10));
                LoggedConsole.WriteLine("Col15 in s1=" + s1.IncludesColumn(15));
                LoggedConsole.WriteLine("Col20 in s1=" + s1.IncludesColumn(20));

                int  py     = 23;
                int  px     = 25;
                bool inside = s1.PointInside(py, px);
                LoggedConsole.WriteLine("\nPoint (" + py + "," + px + ") inside s1 =" + inside);
                inside = s2.PointInside(py, px);
                LoggedConsole.WriteLine("Point (" + py + "," + px + ") inside s2 =" + inside);
                inside = s3.PointInside(py, px);
                LoggedConsole.WriteLine("Point (" + py + "," + px + ") inside s3 =" + inside);

                bool overlapped = s1.Overlaps(s3);
                LoggedConsole.WriteLine("\ns1 and s3 overlap =" + overlapped);
                overlapped = s1.Overlaps(s2);
                LoggedConsole.WriteLine("s1 and s2 overlap =" + overlapped);
            }

            // end test2

            if (true)
            {
                // test Method MergeShapes()
                LoggedConsole.WriteLine("Test MergeShapes()");
                var list = new ArrayList();
                int r1   = 10;
                int c1   = 10;
                int r2   = 20;
                int c2   = 20;
                var s1   = new Oblong(r1, c1, r2, c2);
                s1.WriteBounds();
                list.Add(s1);
                r1 = 17;
                c1 = 16;
                r2 = 23;
                c2 = 24;
                var s2 = new Oblong(r1, c1, r2, c2);
                s2.WriteBounds();
                list.Add(s2);
                r1 = 20;
                c1 = 20;
                r2 = 30;
                c2 = 30;
                var s3 = new Oblong(r1, c1, r2, c2);
                s3.WriteBounds();
                list.Add(s3);

                LoggedConsole.WriteLine(" dy(s2-s1)= " + s1.RowShift(s2));
                LoggedConsole.WriteLine(" dy(s3-s2)= " + s2.RowShift(s3));

                int dyThreshold = 6;
                list = MergeShapesWhoseEndsOverlap(list, dyThreshold);
                LoggedConsole.WriteLine("List size=" + list.Count);
                foreach (Oblong s in list)
                {
                    s.WriteBounds();
                }
            }

            // end test3

            // if (false) //test Method()
            // {
            // LoggedConsole.WriteLine("Test Method Name()");
            // } //end test4
            LoggedConsole.WriteLine("\nFINISHED!!");
            Console.ReadLine();
        }