private static void FindNeighbours(DirectionGraph graph, DirectionNode[,] nodes) { int count = graph.Nodes.Count; int[,] adjmatrix = new int[count, count]; Dictionary <DirectionNode, int> dict = new Dictionary <DirectionNode, int>(40); int k = 0; foreach (DirectionNode node in graph.Nodes) { dict.Add(node, k); ++k; } int width = nodes.GetLength(0); int height = nodes.GetLength(1); for (int j = 1; j < height; j++) { for (int i = 1; i < width; i++) { DirectionNode curr = nodes[i, j]; if (curr == null || curr.Weight < MINNOISECOUNT) { continue; } DirectionNode l = nodes[i - 1, j]; DirectionNode u = nodes[i, j - 1]; int num1, num2; if (l != null && l != curr && l.Weight >= MINNOISECOUNT) { dict.TryGetValue(l, out num1); dict.TryGetValue(curr, out num2); adjmatrix[num1, num2]++; adjmatrix[num2, num1]++; } if (u != null && u != curr && u.Weight >= MINNOISECOUNT) { dict.TryGetValue(u, out num1); dict.TryGetValue(curr, out num2); adjmatrix[num1, num2]++; adjmatrix[num2, num1]++; } } } graph.perimeterarray = adjmatrix; }
private static void FindNeighbours(DirectionGraph graph, DirectionNode[,] nodes) { int count = graph.Nodes.Count; int[,] adjmatrix = new int[count, count]; Dictionary<DirectionNode, int> dict = new Dictionary<DirectionNode, int>(40); int k = 0; foreach (DirectionNode node in graph.Nodes) { dict.Add(node, k); ++k; } int width = nodes.GetLength(0); int height = nodes.GetLength(1); for (int j = 1; j < height; j++) { for (int i = 1; i < width; i++) { DirectionNode curr = nodes[i, j]; if (curr == null || curr.Weight < MINNOISECOUNT) continue; DirectionNode l = nodes[i - 1, j]; DirectionNode u = nodes[i, j - 1]; int num1, num2; if (l != null && l != curr && l.Weight >= MINNOISECOUNT) { dict.TryGetValue(l, out num1); dict.TryGetValue(curr, out num2); adjmatrix[num1, num2]++; adjmatrix[num2, num1]++; } if (u != null && u != curr && u.Weight >= MINNOISECOUNT) { dict.TryGetValue(u, out num1); dict.TryGetValue(curr, out num2); adjmatrix[num1, num2]++; adjmatrix[num2, num1]++; } } } graph.perimeterarray = adjmatrix; }
private static DirectionGraph CreateGraph(DirectionalImage directions) { DirectionGraph graph = new DirectionGraph(); int width = directions.arr.GetLength(0); int height = directions.arr.GetLength(1); DirectionNode[,] tempnodes = new DirectionNode[width, height]; DirectionNode dirnode; #region First line and first column if (directions.arr[0, 0] != 9) { dirnode = new DirectionNode(); dirnode.direction = directions.arr[0, 0]; dirnode.add(new Point(0, 0)); graph.add(dirnode); tempnodes[0, 0] = dirnode; } for (int i = 1; i < width; i++) { byte curr = directions.arr[i, 0]; if (curr == 9) continue; byte l = directions.arr[i - 1, 0]; if (curr == l) { tempnodes[i, 0] = tempnodes[i - 1, 0]; tempnodes[i - 1, 0].add(new Point(i, 0)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, 0)); graph.add(dirnode); tempnodes[i, 0] = dirnode; } } int a = 1; for (int j = 1; j < height; j++) { byte curr = directions.arr[0, j]; if (curr == 9) continue; byte u = directions.arr[0, j - 1]; if (curr == u) { tempnodes[0, j] = tempnodes[0, j - 1]; tempnodes[0, j - 1].add(new Point(0, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(0, j)); graph.add(dirnode); tempnodes[0, j] = dirnode; } } #endregion int b = 0; #region Main for (int j = 1; j < directions.arr.GetLength(1); j++) { for (int i = 1; i < directions.arr.GetLength(0); i++) { byte curr = directions.arr[i, j]; if (curr == 9) continue; byte l = directions.arr[i - 1, j]; byte u = directions.arr[i, j - 1]; if (curr == l && curr == u && tempnodes[i, j - 1] != tempnodes[i - 1, j]) { DirectionNode input, output; if (tempnodes[i - 1, j].Weight > tempnodes[i, j - 1].Weight) { input = tempnodes[i, j - 1]; output = tempnodes[i - 1, j]; } else { output = tempnodes[i, j - 1]; input = tempnodes[i - 1, j]; } foreach (Point point in input.PointList) { tempnodes[point.x, point.y] = output; output.add(point); } tempnodes[i, j] = output; input.PointList.Clear(); output.add(new Point(i, j)); } else if (curr == l) { tempnodes[i, j] = tempnodes[i - 1, j]; tempnodes[i - 1, j].add(new Point(i, j)); } else if (curr == u) { tempnodes[i, j] = tempnodes[i, j - 1]; tempnodes[i, j - 1].add(new Point(i, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, j)); graph.add(dirnode); tempnodes[i, j] = dirnode; } } } #endregion DirectionGraph endgraph = new DirectionGraph(); foreach (DirectionNode node in graph.Nodes) { if (node.Weight >= MINNOISECOUNT) endgraph.add(node); } #region computeweights foreach (DirectionNode node in endgraph.Nodes) { node.ComputeCenter(); } #endregion FindNeighbours(endgraph, tempnodes); //tempnodes - array with references to DirectionNodes return endgraph; }
public void add(DirectionNode p) { Nodes.Add(p); }
private static DirectionGraph CreateGraph(DirectionalImage directions) { DirectionGraph graph = new DirectionGraph(); int width = directions.arr.GetLength(0); int height = directions.arr.GetLength(1); DirectionNode[,] tempnodes = new DirectionNode[width, height]; DirectionNode dirnode; #region First line and first column if (directions.arr[0, 0] != 9) { dirnode = new DirectionNode(); dirnode.direction = directions.arr[0, 0]; dirnode.add(new Point(0, 0)); graph.add(dirnode); tempnodes[0, 0] = dirnode; } for (int i = 1; i < width; i++) { byte curr = directions.arr[i, 0]; if (curr == 9) { continue; } byte l = directions.arr[i - 1, 0]; if (curr == l) { tempnodes[i, 0] = tempnodes[i - 1, 0]; tempnodes[i - 1, 0].add(new Point(i, 0)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, 0)); graph.add(dirnode); tempnodes[i, 0] = dirnode; } } int a = 1; for (int j = 1; j < height; j++) { byte curr = directions.arr[0, j]; if (curr == 9) { continue; } byte u = directions.arr[0, j - 1]; if (curr == u) { tempnodes[0, j] = tempnodes[0, j - 1]; tempnodes[0, j - 1].add(new Point(0, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(0, j)); graph.add(dirnode); tempnodes[0, j] = dirnode; } } #endregion int b = 0; #region Main for (int j = 1; j < directions.arr.GetLength(1); j++) { for (int i = 1; i < directions.arr.GetLength(0); i++) { byte curr = directions.arr[i, j]; if (curr == 9) { continue; } byte l = directions.arr[i - 1, j]; byte u = directions.arr[i, j - 1]; if (curr == l && curr == u && tempnodes[i, j - 1] != tempnodes[i - 1, j]) { DirectionNode input, output; if (tempnodes[i - 1, j].Weight > tempnodes[i, j - 1].Weight) { input = tempnodes[i, j - 1]; output = tempnodes[i - 1, j]; } else { output = tempnodes[i, j - 1]; input = tempnodes[i - 1, j]; } foreach (Point point in input.PointList) { tempnodes[point.x, point.y] = output; output.add(point); } tempnodes[i, j] = output; input.PointList.Clear(); output.add(new Point(i, j)); } else if (curr == l) { tempnodes[i, j] = tempnodes[i - 1, j]; tempnodes[i - 1, j].add(new Point(i, j)); } else if (curr == u) { tempnodes[i, j] = tempnodes[i, j - 1]; tempnodes[i, j - 1].add(new Point(i, j)); } else { dirnode = new DirectionNode(); dirnode.direction = curr; dirnode.add(new Point(i, j)); graph.add(dirnode); tempnodes[i, j] = dirnode; } } } #endregion DirectionGraph endgraph = new DirectionGraph(); foreach (DirectionNode node in graph.Nodes) { if (node.Weight >= MINNOISECOUNT) { endgraph.add(node); } } #region computeweights foreach (DirectionNode node in endgraph.Nodes) { node.ComputeCenter(); } #endregion FindNeighbours(endgraph, tempnodes); //tempnodes - array with references to DirectionNodes return(endgraph); }
public void add(DirectionNode p) { Nodes.Add(p); }