public override void GenerateMapping(int w, int h, Shape original, Shape warped, Dictionary<Point, Point> mapping) { double[,] X = new double[4, 4]; double[] Y = new double[4]; X[0, 0] = 1; X[1, 0] = warped.GetTopLeftPixel().GetX(); X[2, 0] = warped.GetTopLeftPixel().GetY(); X[3, 0] = X[1, 0] * X[2, 0]; X[0, 1] = 1; X[1, 1] = warped.GetTopRightPixel().GetX(); X[2, 1] = warped.GetTopRightPixel().GetY(); X[3, 1] = X[1, 1] * X[2, 1]; X[0, 2] = 1; X[1, 2] = warped.GetBottomRightPixel().GetX(); X[2, 2] = warped.GetBottomRightPixel().GetY(); X[3, 2] = X[1, 2] * X[2, 2]; X[0, 3] = 1; X[1, 3] = warped.GetBottomLeftPixel().GetX(); X[2, 3] = warped.GetBottomLeftPixel().GetY(); X[3, 3] = X[1, 3] * X[2, 3]; Y[0] = original.GetTopLeftPixel().GetX(); Y[1] = original.GetTopRightPixel().GetX(); Y[2] = original.GetBottomRightPixel().GetX(); Y[3] = original.GetBottomLeftPixel().GetX(); Solver.Solve(X, Y); double a0 = Y[0], a1 = Y[1], a2 = Y[2], a3 = Y[3]; Y[0] = original.GetTopLeftPixel().GetY(); Y[1] = original.GetTopRightPixel().GetY(); Y[2] = original.GetBottomRightPixel().GetY(); Y[3] = original.GetBottomLeftPixel().GetY(); Solver.Solve(X, Y); double b0 = Y[0], b1 = Y[1], b2 = Y[2], b3 = Y[3]; List<Pixel> warpedPixels = warped.GetPixels(); foreach (Pixel pixel in warpedPixels) { int originalX = Math.Min(w - 1, Math.Max(0, Convert.ToInt32(a0 + a1 * pixel.GetX() + a2 * pixel.GetY() + a3 * pixel.GetX() * pixel.GetY()))); int originalY = Math.Min(h - 1, Math.Max(0, Convert.ToInt32(b0 + b1 * pixel.GetX() + b2 * pixel.GetY() + b3 * pixel.GetX() * pixel.GetY()))); mapping.Add(new Point(pixel.GetX(), pixel.GetY()), new Point(originalX, originalY)); } }
public static List<Shape> FindSquare(Bitmap logoImage, string outName) { List<Shape> ListSquares = new List<Shape>(); int w = logoImage.Width; int h = logoImage.Height; int size = w * h; allPixels = new Boolean[size]; for (int i=0; i<size; i+=1) { Point point = GetPoint(w, i); allPixels[i] = logoImage.GetPixel(point.X, point.Y).A > 0; } int start = 0; int squareCount = 0; while (allPixels.Contains(true)) { for (int i = start; i < size; i += 1) { if (allPixels[i]) { start = i + 1; squareCount += 1; Point randomPoint = GetPoint(w, i); Console.WriteLine(string.Format("Random pixel was {0},{1}", randomPoint.X, randomPoint.Y)); List<Pixel> square = BFS(logoImage, randomPoint.X, randomPoint.Y, squareCount); Pixel[] corners = GetCorners(logoImage, square); Shape shape = new Shape(square, corners); shape.CalculateCorners(); ListSquares.Add(shape); Console.WriteLine(string.Format("Square size was {0}", square.Count)); if (square.Count == 1) { Console.WriteLine("Found bad shape!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } break; } } } if (outName != null) { DrawLines(logoImage, ListSquares); logoImage.Save(outName); } Console.WriteLine(string.Format("Complete! Found {0} shapes", ListSquares.Count)); return ListSquares; }
public abstract void GenerateMapping(int w, int h, Shape origShape, Shape warpedShape, Dictionary<Point, Point> mapping);
public static List<Shape> GetShapeFromPoints(List<List<int>> points, string hexcolor, int width, int height, string outName) { List<Pixel> square = new List<Pixel>(); ColorConverter cc = new ColorConverter(); Color color = (Color) cc.ConvertFromString(hexcolor); Point p0 = new Point(points.ElementAt(0).ElementAt(0), points.ElementAt(0).ElementAt(1)); Point p1 = new Point(points.ElementAt(1).ElementAt(0), points.ElementAt(1).ElementAt(1)); Point p2 = new Point(points.ElementAt(2).ElementAt(0), points.ElementAt(2).ElementAt(1)); Point p3 = new Point(points.ElementAt(3).ElementAt(0), points.ElementAt(3).ElementAt(1)); Pixel[] corners = new Pixel[4]; corners[0] = new Pixel(color, p0.X, p0.Y); corners[1] = new Pixel(color, p1.X, p1.Y); corners[2] = new Pixel(color, p2.X, p2.Y); corners[3] = new Pixel(color, p3.X, p3.Y); for (int x=0; x< width; x+=1) { for(int y=0;y< height;y+=1) { if (PointChecker.PointInQuad(new Point(x, y), p0, p1, p2, p3)) { square.Add(new Pixel(color, x, y)); } } } Shape shape = new Shape(square, corners); return new List<Shape>() { shape }; }
public override void GenerateMapping(int w, int h, Shape original, Shape warped, Dictionary<Point, Point> mapping) { double xOff = original.GetTopLeftPixel().GetX(); double yOff = original.GetTopLeftPixel().GetY(); double offsetX = original.GetTopRightPixel().GetX() - original.GetTopLeftPixel().GetX(); double offsetY = original.GetBottomRightPixel().GetY() - original.GetTopRightPixel().GetY(); double oTopLeftX = 0; double oTopLeftY = 0; double oTopRightX = (original.GetTopRightPixel().GetX() - xOff) / offsetX; double oTopRightY = (original.GetTopRightPixel().GetY() - yOff) / offsetY; double oBottomRightX = (original.GetBottomRightPixel().GetX() - xOff) / offsetX; double oBottomRightY = (original.GetBottomRightPixel().GetY() - yOff) / offsetY; double oBottomLeftX = (original.GetBottomLeftPixel().GetX() - xOff) / offsetX; double oBottomLeftY = (original.GetBottomLeftPixel().GetY() - yOff) / offsetY; double wTopLeftX = (warped.GetTopLeftPixel().GetX() - xOff) / offsetX; double wTopLeftY = (warped.GetTopLeftPixel().GetY() - yOff) / offsetY; double wTopRightX = (warped.GetTopRightPixel().GetX() - xOff) / offsetX; double wTopRightY = (warped.GetTopRightPixel().GetY() - yOff) / offsetY; double wBottomRightX = (warped.GetBottomRightPixel().GetX() - xOff) / offsetX; double wBottomRightY = (warped.GetBottomRightPixel().GetY() - yOff) / offsetY; double wBottomLeftX = (warped.GetBottomLeftPixel().GetX() - xOff) / offsetX; double wBottomLeftY = (warped.GetBottomLeftPixel().GetY() - yOff) / offsetY; double[,] X = new double[4, 4]; double[] Y = new double[4]; X[0, 0] = 1; X[1, 0] = oTopLeftX; X[2, 0] = oTopLeftY; X[3, 0] = X[1, 0] * X[2, 0]; X[0, 1] = 1; X[1, 1] = oTopRightX; X[2, 1] = oTopRightY; X[3, 1] = X[1, 1] * X[2, 1]; X[0, 2] = 1; X[1, 2] = oBottomRightX; X[2, 2] = oBottomRightY; X[3, 2] = X[1, 2] * X[2, 2]; X[0, 3] = 1; X[1, 3] = oBottomLeftX; X[2, 3] = oBottomLeftY; X[3, 3] = X[1, 3] * X[2, 3]; Y[0] = wTopLeftX; Y[1] = wTopRightX; Y[2] = wBottomRightX; Y[3] = wBottomLeftX; Solver.Solve(X, Y); double a0 = Y[0], a1 = Y[1], a2 = Y[2], a3 = Y[3]; Y[0] = wTopLeftY; Y[1] = wTopRightY; Y[2] = wBottomRightY; Y[3] = wBottomLeftY; Solver.Solve(X, Y); double b0 = Y[0], b1 = Y[1], b2 = Y[2], b3 = Y[3]; double A = (b2 * a3) - (b3 * a2); double B_One = (b0 * a3 - b3 * a0) + (b2 * a1 - b1 * a2); double C_One = b0 * a1 - b1 * a0; List<Pixel> warpedPixels = warped.GetPixels(); foreach (Pixel pixel in warpedPixels) { double pX = (pixel.GetX() - xOff) / offsetX; double pY = (pixel.GetY() - yOff) / offsetY; double B = B_One + (b3 * pX - a3 * pY); double C = C_One + (b1 * pX - a1 * pY); double q = -0.5 * (B + Math.Sqrt(Math.Pow(B, 2) - 4 * A * C)); double originalY_ = C / q; double originalX_ = (pX - a0 - a2 * originalY_) / (a1 + a3 * originalY_); int originalX = Math.Min(w - 1, Math.Max(0, Convert.ToInt32(Math.Max(0, Math.Min(w, originalX_ * offsetX + xOff))))); int originalY = Math.Min(h - 1, Math.Max(0, Convert.ToInt32(Math.Max(0, Math.Min(h, originalY_ * offsetY + yOff))))); mapping.Add(new Point(pixel.GetX(), pixel.GetY()), new Point(originalX, originalY)); } }