public static DirectionGraph BuildSuperGraph(Image image) { DirectionalImage dirimage = CreateDirections(image); DirectionGraph graph = CreateGraph(dirimage); DirectionGraph supergraph = CreateSuperGraph(graph); return(supergraph); }
private static DirectionalImage CreateDirections(Image image) { Bitmap bimage = new Bitmap(image); ApplyFilter(new GrayscaleBT709(), ref bimage); int width = bimage.Width; int height = bimage.Height; int dirwidth = (width - WINDOWSIZE - 1) / WINDOWSIZE; int dirheight = (height - WINDOWSIZE - 1) / WINDOWSIZE; DirectionalImage directions = new DirectionalImage(dirwidth, dirheight); int heightpos = WINDOWSIZE; int dirposheight = 0; bool foundfirst = false; while (heightpos < height - WINDOWSIZE) { int widthpos = WINDOWSIZE; int dirposwidth = 0; while (widthpos < width - WINDOWSIZE) { int linepointx = widthpos; int linepointy = heightpos; byte direction; if (Findline(ref linepointx, ref linepointy, bimage)) //managed to find line in window { Vector tangent = Tangent(linepointx, linepointy, bimage); direction = Computedirection(tangent); if (!foundfirst) { foundfirst = true; directions.startline = dirposheight; } directions.endline = dirposheight; } else { direction = 9; } directions.arr[dirposwidth, dirposheight] = direction; widthpos += WINDOWSIZE; dirposwidth++; } heightpos += WINDOWSIZE; dirposheight++; } //writetofile(directions.arr, "image1"); for (int i = 1; i <= 3; ++i) removenoise(directions); //writetofile(directions.arr, "image2"); return directions; }
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; }
private static void removenoise(DirectionalImage directions) { byte curr, u, d, l, r; int changecount = 0; for (int j = Math.Max(directions.startline, 1); j < directions.arr.GetLength(1) - 1; j++) { for (int i = 1; i < directions.arr.GetLength(0) - 1; i++) { curr = directions.arr[i, j]; u = directions.arr[i - 1, j]; d = directions.arr[i + 1, j]; l = directions.arr[i, j - 1]; r = directions.arr[i, j + 1]; int dircount = 0; if (u != 9) ++dircount; if (d != 9) ++dircount; if (l != 9) ++dircount; if (r != 9) ++dircount; if (curr == 9) { if ((l != 9 && r != 9) || (u != 9 && d != 9)) //it's a noise!! { directions.arr[i, j] = (byte)Math.Round((u + d + l + r - 9.0 * (4 - dircount)) / dircount); changecount++; } } else //curr is not 9 { if (curr != u && curr != r && curr != d && curr != l) { if (dircount == 0) //only 9s around { directions.arr[i, j] = 9; changecount++; } else//not all 9s { if (dircount == 1 && u + l + r + d - 27 != curr) //3 nines & something strange around { directions.arr[i, j] = 9; changecount++; } else { directions.arr[i, j] = (byte)Math.Round((u + d + l + r - 9.0 * (4 - dircount)) / dircount); changecount++; } } } } } } }
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); }
private static void removenoise(DirectionalImage directions) { byte curr, u, d, l, r; int changecount = 0; for (int j = Math.Max(directions.startline, 1); j < directions.arr.GetLength(1) - 1; j++) { for (int i = 1; i < directions.arr.GetLength(0) - 1; i++) { curr = directions.arr[i, j]; u = directions.arr[i - 1, j]; d = directions.arr[i + 1, j]; l = directions.arr[i, j - 1]; r = directions.arr[i, j + 1]; int dircount = 0; if (u != 9) { ++dircount; } if (d != 9) { ++dircount; } if (l != 9) { ++dircount; } if (r != 9) { ++dircount; } if (curr == 9) { if ((l != 9 && r != 9) || (u != 9 && d != 9)) //it's a noise!! { directions.arr[i, j] = (byte)Math.Round((u + d + l + r - 9.0 * (4 - dircount)) / dircount); changecount++; } } else //curr is not 9 { if (curr != u && curr != r && curr != d && curr != l) { if (dircount == 0) //only 9s around { directions.arr[i, j] = 9; changecount++; } else//not all 9s { if (dircount == 1 && u + l + r + d - 27 != curr) //3 nines & something strange around { directions.arr[i, j] = 9; changecount++; } else { directions.arr[i, j] = (byte)Math.Round((u + d + l + r - 9.0 * (4 - dircount)) / dircount); changecount++; } } } } } } }
private static DirectionalImage CreateDirections(Image image) { Bitmap bimage = new Bitmap(image); ApplyFilter(new GrayscaleBT709(), ref bimage); int width = bimage.Width; int height = bimage.Height; int dirwidth = (width - WINDOWSIZE - 1) / WINDOWSIZE; int dirheight = (height - WINDOWSIZE - 1) / WINDOWSIZE; DirectionalImage directions = new DirectionalImage(dirwidth, dirheight); int heightpos = WINDOWSIZE; int dirposheight = 0; bool foundfirst = false; while (heightpos < height - WINDOWSIZE) { int widthpos = WINDOWSIZE; int dirposwidth = 0; while (widthpos < width - WINDOWSIZE) { int linepointx = widthpos; int linepointy = heightpos; byte direction; if (Findline(ref linepointx, ref linepointy, bimage)) //managed to find line in window { Vector tangent = Tangent(linepointx, linepointy, bimage); direction = Computedirection(tangent); if (!foundfirst) { foundfirst = true; directions.startline = dirposheight; } directions.endline = dirposheight; } else { direction = 9; } directions.arr[dirposwidth, dirposheight] = direction; widthpos += WINDOWSIZE; dirposwidth++; } heightpos += WINDOWSIZE; dirposheight++; } //writetofile(directions.arr, "image1"); for (int i = 1; i <= 3; ++i) { removenoise(directions); } //writetofile(directions.arr, "image2"); return(directions); }