public bool Overlaps(Oblong s2) { bool rowOverlap = false; for (int i = s2.RowTop; i < s2.RowBottom; i++) { if (this.IncludesRow(i)) { rowOverlap = true; break; } } bool colOverlap = false; for (int i = s2.ColumnLeft; i < s2.ColumnRight; i++) { if (this.IncludesColumn(i)) { colOverlap = true; break; } } if (rowOverlap && colOverlap) { return(true); } return(false); }
public bool AdjacentRows(Oblong s2) { // this.top-s2.bottom adjacency OR this.bottom-s2.top adjacency bool rowAdjacency = Math.Abs(this.RowBottom - s2.RowTop) == 1 || Math.Abs(this.RowTop - s2.RowBottom) == 1; return(rowAdjacency); }
public static int RowOverlap(Oblong o1, Oblong o2) { if (o1.RowBottom < o2.RowTop) { return(0); } if (o2.RowBottom < o1.RowTop) { return(0); } // at this point the two events overlap int overlap = 0; if (o1.IncludesRow(o2.RowTop)) { overlap = o1.RowBottom - o2.RowTop + 1; } else if (o2.IncludesRow(o1.RowTop)) { overlap = o2.RowBottom - o1.RowTop + 1; } return(overlap); }
public static int ColumnOverlap(Oblong o1, Oblong o2) { if (o1.ColumnRight < o2.ColumnLeft) { return(0); } if (o2.ColumnRight < o1.ColumnLeft) { return(0); } // at this point the two events overlap int overlap = 0; if (o1.IncludesColumn(o2.ColumnLeft)) { overlap = o1.ColumnRight - o2.ColumnLeft + 1; } else if (o2.IncludesColumn(o1.ColumnLeft)) { overlap = o2.ColumnRight - o1.ColumnLeft + 1; } return(overlap); }
/// <summary> /// returns a list of shapes that represent the averages of shapes in each category dervied /// from FuzzyART clustering. /// </summary> /// <param name="shapes"> /// </param> /// <param name="categories"> /// </param> /// <param name="categoryCount"> /// </param> /// <returns> /// The <see cref="List"/>. /// </returns> public static List <Oblong> CategoryShapes(List <Oblong> shapes, int[] categories, int categoryCount) { if (shapes == null) { return(null); } if (categories == null) { return(null); } if (categoryCount == 0) { return(null); } var categoryShapes = new List <Oblong>(); for (int c = 0; c < categoryCount; c++) { int r1 = 0; int c1 = 0; int r2 = 0; int c2 = 0; int count = 0; for (int i = 0; i < shapes.Count; i++) { if (categories[i] != c) { continue; // skip shapes not in category c } count++; // keep count of numbers in category c Oblong s = shapes[i]; r1 += s.RowTop; c1 += s.ColumnLeft; r2 += s.RowBottom; c2 += s.ColumnRight; } if (count == 0) { continue; // no shapes assigned to this category } r1 /= count; c1 /= count; r2 /= count; c2 /= count; var shape = new Oblong(r1, c1, r2, c2); shape.Category = c; categoryShapes.Add(shape); } return(categoryShapes); }
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); }
public static Oblong RotateOblongForStandardSpectrogram(Oblong oblong, int rowCount, int colCount) { // Translate time dimension = frames = matrix rows. int newTopRow = rowCount - oblong.ColumnRight; int newBottomRow = rowCount - oblong.ColumnLeft; //Translate freq dimension = freq bins = matrix columns. int newLeftCol = oblong.RowTop; int newRightCol = oblong.RowBottom; return(new Oblong(newTopRow, newBottomRow, newLeftCol, newRightCol)); }
public bool Encloses(Oblong s2) { bool row1Overlap = this.IncludesRow(s2.RowTop); bool row2Overlap = this.IncludesRow(s2.RowBottom); bool col1Overlap = this.IncludesColumn(s2.ColumnLeft); bool col2Overlap = this.IncludesColumn(s2.ColumnRight); if (row1Overlap && row2Overlap && col1Overlap && col2Overlap) { return(true); } return(false); }
public int OverlapArea(Oblong s2) { bool row1Overlap = this.IncludesRow(s2.RowTop); bool row2Overlap = this.IncludesRow(s2.RowBottom); bool col1Overlap = this.IncludesColumn(s2.ColumnLeft); bool col2Overlap = this.IncludesColumn(s2.ColumnRight); if (row1Overlap && row2Overlap && col1Overlap && col2Overlap) { return(s2.Area()); } if (!row1Overlap && !row2Overlap && !col1Overlap && !col2Overlap && this.Overlaps(s2)) { return(this.Area()); } return(0); }
public int ColumnShift(Oblong s2) { int dx = s2.colCentroid() - this.colCentroid(); return(dx); }
/// <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); }
public static Oblong Clone(Oblong s) { return(new Oblong(s.RowTop, s.ColumnLeft, s.RowBottom, s.ColumnRight)); }
/// <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(); }
public int RowShift(Oblong s2) { int dy = s2.RowCentroid() - this.RowCentroid(); return(dy); }