/// <summary> /// returns an ideally spaced grid over the actual detected spots /// </summary> /// <param name="grid"></param> /// <returns></returns> private static grid2D OverlayIdealGrid(calibrationDot[,] grid, List<calibrationDot> corners, ref int grid_offset_x, ref int grid_offset_y, int random_seed) { grid2D overlay_grid = null; int grid_tx = -1; int grid_ty = -1; int grid_bx = -1; int grid_by = -1; int offset_x = 0; int offset_y = 0; bool found = false; int max_region_area = 0; // try searching horizontally and vertically // then pick the result with the greatest area for (int test_orientation = 0; test_orientation < 2; test_orientation++) { bool temp_found = false; int temp_grid_tx = -1; int temp_grid_ty = -1; int temp_grid_bx = -1; int temp_grid_by = -1; int temp_offset_x = 0; int temp_offset_y = 0; switch (test_orientation) { case 0: { while ((temp_offset_y < 5) && (!temp_found)) { temp_offset_x = 0; while ((temp_offset_x < 3) && (!temp_found)) { temp_grid_tx = temp_offset_x; temp_grid_ty = temp_offset_y; temp_grid_bx = grid.GetLength(0) - 1 - temp_offset_x; temp_grid_by = grid.GetLength(1) - 1 - temp_offset_y; if ((temp_grid_bx < grid.GetLength(0)) && (temp_grid_tx < grid.GetLength(0)) && (temp_grid_by < grid.GetLength(1)) && (temp_grid_ty < grid.GetLength(1)) && (temp_grid_ty >= 0) && (temp_grid_by >= 0) && (temp_grid_tx >= 0) && (temp_grid_bx >= 0)) { if ((grid[temp_grid_tx, temp_grid_ty] != null) && (grid[temp_grid_bx, temp_grid_ty] != null) && (grid[temp_grid_bx, temp_grid_by] != null) && (grid[temp_grid_tx, temp_grid_by] != null)) { temp_found = true; } } temp_offset_x++; } temp_offset_y++; } break; } case 1: { while ((temp_offset_x < 3) && (!temp_found)) { temp_offset_y = 0; while ((temp_offset_y < 5) && (!temp_found)) { temp_grid_tx = temp_offset_x; temp_grid_ty = temp_offset_y; temp_grid_bx = grid.GetLength(0) - 1 - temp_offset_x; temp_grid_by = grid.GetLength(1) - 1 - temp_offset_y; if ((temp_grid_bx < grid.GetLength(0)) && (temp_grid_tx < grid.GetLength(0)) && (temp_grid_by < grid.GetLength(1)) && (temp_grid_ty < grid.GetLength(1)) && (temp_grid_ty >= 0) && (temp_grid_by >= 0) && (temp_grid_tx >= 0) && (temp_grid_bx >= 0)) { if ((grid[temp_grid_tx, temp_grid_ty] != null) && (grid[temp_grid_bx, temp_grid_ty] != null) && (grid[temp_grid_bx, temp_grid_by] != null) && (grid[temp_grid_tx, temp_grid_by] != null)) { temp_found = true; } } temp_offset_y++; } temp_offset_x++; } break; } } temp_offset_y = temp_grid_ty - 1; while (temp_offset_y >= 0) { if ((temp_offset_y < grid.GetLength(1)) && (temp_offset_y >= 0)) { if ((grid[temp_grid_tx, temp_offset_y] != null) && (grid[temp_grid_bx, temp_offset_y] != null)) { temp_grid_ty = temp_offset_y; temp_offset_y--; } else break; } else break; } temp_offset_y = temp_grid_by + 1; while (temp_offset_y < grid.GetLength(1)) { if ((temp_offset_y < grid.GetLength(1)) && (temp_offset_y >= 0)) { if ((grid[temp_grid_tx, temp_offset_y] != null) && (grid[temp_grid_bx, temp_offset_y] != null)) { temp_grid_by = temp_offset_y; temp_offset_y++; } else break; } else break; } if (temp_found) { int region_area = (temp_grid_bx - temp_grid_tx) * (temp_grid_by - temp_grid_ty); if (region_area > max_region_area) { max_region_area = region_area; found = true; grid_tx = temp_grid_tx; grid_ty = temp_grid_ty; grid_bx = temp_grid_bx; grid_by = temp_grid_by; offset_x = temp_offset_x; offset_y = temp_offset_y; } } } if (found) { // record the positions of the corners corners.Add(grid[grid_tx, grid_ty]); corners.Add(grid[grid_bx, grid_ty]); corners.Add(grid[grid_bx, grid_by]); corners.Add(grid[grid_tx, grid_by]); double dx, dy; double x0 = grid[grid_tx, grid_ty].x; double y0 = grid[grid_tx, grid_ty].y; double x1 = grid[grid_bx, grid_ty].x; double y1 = grid[grid_bx, grid_ty].y; double x2 = grid[grid_tx, grid_by].x; double y2 = grid[grid_tx, grid_by].y; double x3 = grid[grid_bx, grid_by].x; double y3 = grid[grid_bx, grid_by].y; polygon2D perimeter = new polygon2D(); perimeter.Add((float)x0, (float)y0); perimeter.Add((float)x1, (float)y1); perimeter.Add((float)x3, (float)y3); perimeter.Add((float)x2, (float)y2); int grid_width = grid_bx - grid_tx; int grid_height = grid_by - grid_ty; int min_hits = 0; double min_dx = 0, min_dy = 0; // try various perimeter sizes double min_dist = double.MaxValue; int max_perim_size_tries = 100; polygon2D best_perimeter = perimeter; MersenneTwister rnd = new MersenneTwister(random_seed); for (int perim_size = 0; perim_size < max_perim_size_tries; perim_size++) { // try a small range of translations for (int nudge_x = -10; nudge_x <= 10; nudge_x++) { for (int nudge_y = -5; nudge_y <= 5; nudge_y++) { // create a perimeter at this scale and translation polygon2D temp_perimeter = perimeter.Scale(1.0f + (perim_size * 0.1f / max_perim_size_tries)); temp_perimeter = temp_perimeter.ScaleSideLength(0, 0.95f + ((float)rnd.NextDouble() * 0.1f)); temp_perimeter = temp_perimeter.ScaleSideLength(2, 0.95f + ((float)rnd.NextDouble() * 0.1f)); for (int i = 0; i < temp_perimeter.x_points.Count; i++) { temp_perimeter.x_points[i] += nudge_x; temp_perimeter.y_points[i] += nudge_y; } // create a grid based upon the perimeter grid2D temp_overlay_grid = new grid2D(grid_width, grid_height, temp_perimeter, 0, false); // how closely does the grid fit the actual observations ? double temp_min_dist = min_dist; BestFit(grid_tx, grid_ty, grid, temp_overlay_grid, ref min_dist, ref min_dx, ref min_dy, ref min_hits, ref grid_offset_x, ref grid_offset_y); // record the closest fit if (temp_min_dist < min_dist) { best_perimeter = temp_perimeter; overlay_grid = temp_overlay_grid; } } } } if (min_hits > 0) { dx = min_dx; dy = min_dy; Console.WriteLine("dx: " + dx.ToString()); Console.WriteLine("dy: " + dy.ToString()); x0 += dx; y0 += dy; x1 += dx; y1 += dy; x2 += dx; y2 += dy; x3 += dx; y3 += dy; perimeter = new polygon2D(); perimeter.Add((float)x0, (float)y0); perimeter.Add((float)x1, (float)y1); perimeter.Add((float)x3, (float)y3); perimeter.Add((float)x2, (float)y2); overlay_grid = new grid2D(grid_width, grid_height, perimeter, 0, false); } } return (overlay_grid); }
/// <summary> /// returns a polygon representing the periphery of a square region /// </summary> /// <param name="edges"> /// edges within the square region <see cref="List`1"/> /// </param> /// <param name="erode_dilate"> /// erosion or dilation level <see cref="System.Int32"/> /// </param> /// <param name="perim"> /// returned periphery <see cref="polygon2D"/> /// </param> /// <returns> /// set of edges around the periphery <see cref="List`1"/> /// </returns> private static List<List<int>> GetSquarePeriphery(List<int> edges, int erode_dilate, ref polygon2D perim) { List<List<int>> result = new List<List<int>>(); perim = new polygon2D(); // find the bounding box for all edges int tx = 99999; int ty = 99999; int bx = -99999; int by = -99999; for (int i = edges.Count-2; i >= 0; i -= 2) { int x = edges[i]; int y = edges[i+1]; if (x < tx) tx = x; if (y < ty) ty = y; if (x > bx) bx = x; if (y > by) by = y; } int w = bx - tx; int h = by - ty; if ((w > 0) && (h > 0)) { int[] left = new int[h+1]; int[] right = new int[h+1]; int[] top = new int[w+1]; int[] bottom = new int[w+1]; for (int i = edges.Count - 2; i >= 0; i -= 2) { int x = edges[i]; int x2 = x - tx; int y = edges[i+1]; int y2 = y - ty; // left side if ((left[y2] == 0) || (x < left[y2])) left[y2] = x; // right side if ((right[y2] == 0) || (x > right[y2])) right[y2] = x; // top if ((top[x2] == 0) || (y < top[x2])) top[x2] = y; // bottom if ((bottom[x2] == 0) || (y > bottom[x2])) bottom[x2] = y; } #if SHOW_TIMINGS stopwatch timer_best_fit = new stopwatch(); timer_best_fit.Start(); #endif // find a best fit line for the left side int best_start = 0; int best_end = 0; int hits = BestFitLine(left, ref best_start, ref best_end); float left_x0 = left[best_start]; float left_y0 = ty + best_start; float left_x1 = left[best_end]; float left_y1 = ty + best_end; /* BestFitLineAverage(left, ref left_x0, ref left_y0, ref left_x1, ref left_y1); left_y0 += ty; left_y1 += ty; */ // find a best fit line for the right side best_start = 0; best_end = 0; hits = BestFitLine(right, ref best_start, ref best_end); float right_x0 = right[best_start]; float right_y0 = ty + best_start; float right_x1 = right[best_end]; float right_y1 = ty + best_end; /* BestFitLineAverage(right, ref right_x0, ref right_y0, ref right_x1, ref right_y1); right_y0 += ty; right_y1 += ty; */ // find a best fit line for the top side best_start = 0; best_end = 0; hits = BestFitLine(top, ref best_start, ref best_end); float top_x0 = tx + best_start; float top_y0 = top[best_start]; float top_x1 = tx + best_end; float top_y1 = top[best_end]; /* BestFitLineAverage(top, ref top_x0, ref top_y0, ref top_x1, ref top_y1); top_x0 += tx; top_x1 += tx; */ // find a best fit line for the bottom side best_start = 0; best_end = 0; hits = BestFitLine(bottom, ref best_start, ref best_end); float bottom_x0 = tx + best_start; float bottom_y0 = bottom[best_start]; float bottom_x1 = tx + best_end; float bottom_y1 = bottom[best_end]; /* BestFitLineAverage(bottom, ref bottom_x0, ref bottom_y0, ref bottom_x1, ref bottom_y1); bottom_x0 += tx; bottom_x1 += tx; */ #if SHOW_TIMINGS timer_best_fit.Stop(); if (timer_best_fit.time_elapsed_mS > 20) Console.WriteLine("GetSquarePeriphery: best fit " + timer_best_fit.time_elapsed_mS.ToString() ); #endif // find the intersection between the left side and the top side float ix=0; float iy = 0; geometry.intersection(left_x1, left_y1, left_x0, left_y0, top_x1, top_y1, top_x0, top_y0, ref ix, ref iy); perim.Add(ix, iy); // find the intersection between the right side and the top side ix = 0; iy = 0; geometry.intersection(right_x1, right_y1, right_x0, right_y0, top_x0, top_y0, top_x1, top_y1, ref ix, ref iy); perim.Add(ix, iy); // find the intersection between the right side and the bottom side ix = 0; iy = 0; geometry.intersection(right_x1, right_y1, right_x0, right_y0, bottom_x0, bottom_y0, bottom_x1, bottom_y1, ref ix, ref iy); perim.Add(ix, iy); // find the intersection between the left side and the bottom side ix = 0; iy = 0; geometry.intersection(left_x1, left_y1, left_x0, left_y0, bottom_x0, bottom_y0, bottom_x1, bottom_y1, ref ix, ref iy); perim.Add(ix, iy); // left and right List<int> left_edges = new List<int>(); List<int> right_edges = new List<int>(); for (int y = h; y >= 0; y--) { if (left[y] != 0) { left_edges.Add(left[y]); left_edges.Add(ty + y); } if (right[y] != 0) { right_edges.Add(right[y]); right_edges.Add(ty + y); } } // top and bottom List<int> top_edges = new List<int>(); List<int> bottom_edges = new List<int>(); for (int x = w; x >= 0; x--) { if (top[x] != 0) { top_edges.Add(tx + x); top_edges.Add(top[x]); } if (bottom[x] != 0) { bottom_edges.Add(tx + x); bottom_edges.Add(bottom[x]); } } float aspect_check = perim.getShortestSide() / perim.getLongestSide(); if (aspect_check > 0.2f) { result.Add(left_edges); result.Add(right_edges); result.Add(top_edges); result.Add(bottom_edges); } else perim = null; } // shrink the perimeter according to the erosion/dilation value if ((perim != null) && (erode_dilate != 0)) { if (perim.x_points != null) { float shrink_percent = (erode_dilate*2) / (perim.getPerimeterLength()/4.0f); perim = perim.Scale(1.0f - shrink_percent); } else perim = null; } return(result); }