/* * p11 p12 p13 * p21 p p23 * p31 p32 p33 */ internal String ImageToGraph(Bitmap b) { FastBitmap fb = new FastBitmap(b); Height = b.Height; Width = b.Width; //Le imagem orignal e adiciona vertex - nodos for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { Pixel p = new Pixel(x, y, fb.GetPixel(x, y)); g.AddVertex(p); } } //Le imagem orignal e adiciona edges - arestas for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { Pixel p = VertexSearch(x, y, g); Pixel p11 = VertexSearch(x - 1, y - 1, g); if (p11 != null && Pixel.colorIsSimilar(p11.color, p.color)) { var e1 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p11, new EdgeTag()); g.AddEdge(e1); p.valence++; p11.valence++; } Pixel p12 = VertexSearch(x, y - 1, g); if (p12 != null && Pixel.colorIsSimilar(p12.color, p.color)) { var e2 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p12, new EdgeTag()); g.AddEdge(e2); p.valence++; p12.valence++; } Pixel p13 = VertexSearch(x + 1, y - 1, g); if (p13 != null && Pixel.colorIsSimilar(p13.color, p.color)) { var e3 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p13, new EdgeTag()); g.AddEdge(e3); p.valence++; p13.valence++; } Pixel p21 = VertexSearch(x - 1, y, g); if (p21 != null && Pixel.colorIsSimilar(p21.color, p.color)) { var e4 = new TaggedUndirectedEdge <Pixel, EdgeTag>(p, p21, new EdgeTag()); g.AddEdge(e4); p.valence++; p21.valence++; } } } SvgVector svg = new SvgVector(); svg.Height = Height; svg.Width = Width; return(svg.toImageSVG(g, "Graph.svg")); }
/* * Examinar blocos de 2x2 nodos para eliminar a maior quantidade de arestas, e remover cruzamentos ambiguos * 00 10 * 01 11 */ internal String SolveAmbiguities() { for (int y = 0; y < Height - 1; y++) { for (int x = 0; x < Width - 1; x++) { Pixel p00 = VertexSearch(x, y, g); Pixel p10 = VertexSearch(x + 1, y, g); Pixel p01 = VertexSearch(x, y + 1, g); Pixel p11 = VertexSearch(x + 1, y + 1, g); //Console.WriteLine("P= " + x + " " + y + " Valence =" + p00.valence); TaggedUndirectedEdge <Pixel, EdgeTag> edge1 = null; TaggedUndirectedEdge <Pixel, EdgeTag> edge2 = null; g.TryGetEdge(p11, p00, out edge1); g.TryGetEdge(p01, p10, out edge2); if (edge1 != null) { if (edge2 != null) { //Identificada uma ambiguidade if (Pixel.colorIsSimilar(p00.color, p01.color)) { //Se as 4 cores são iguais remove todas as arestas internas if (edge1 != null) { g.RemoveEdge(edge1); } if (edge2 != null) { g.RemoveEdge(edge2); } } //Heuristica da ilha else if (edge1.Source.valence == 1 || edge1.Target.valence == 1) { if (edge2 != null) { g.RemoveEdge(edge2); } } else if (edge2.Source.valence == 1 || edge2.Target.valence == 1) { if (edge1 != null) { g.RemoveEdge(edge1); } } else { //Heuristica da curva if (edge1.Source.valence == 2 || edge1.Target.valence == 2 || edge2.Source.valence == 2 || edge2.Target.valence == 2) { if (CurveSize(edge1) <= CurveSize(edge2)) { if (edge1 != null) { g.RemoveEdge(edge1); } } else { if (edge2 != null) { g.RemoveEdge(edge2); } } } else //Heuristica dos pixels sobrepostos { Pixel p1 = VertexSearch(edge1.Source.x, edge1.Source.y, g); Pixel p2 = VertexSearch(edge2.Source.x, edge2.Source.y, g); //Pixel p3 = VertexSearch(edge1.Target.x, edge1.Target.y, g); //Pixel p4 = VertexSearch(edge2.Target.x, edge2.Target.y, g); Color c1 = p1.color; Color c2 = p2.color; int sumC1 = 0, sumC2 = 0; //Inicializa x e y com -4 posicoes para verificar 3 pixeis em ambas direções int xs = p1.x - 4; int ys = p1.y - 4; while (xs <= p1.x + 3) { while (ys <= p1.y + 3) { Pixel pixel = VertexSearch(x, y, g); if (pixel.color == c1) { sumC1++; } else if (pixel.color == c2) { sumC2++; } ys++; } xs++; } //A cor em maior quantidade representa o fundo, e deve se manter os detalhes conectados if (sumC1 > sumC2) //Remove a cor em menor quantidade { g.RemoveEdge(edge1); } else { g.RemoveEdge(edge2); } } } } } } } SvgVector svg = new SvgVector(); svg.Height = Height; svg.Width = Width; //svg.DrawValence = true; return(svg.toImageSVG(g, "AmbiguitiesSolved.svg")); }