def test_group_dots_hor_lines(self): dot_dist = prep.calc_size_distance(self.mat_dots, ratio=0.9)[1] hor_slope = prep.calc_hor_slope(self.mat_dots, ratio=1.0) list_lines = prep.group_dots_hor_lines(self.mat_dots, hor_slope, dot_dist, ratio=0.1, num_dot_miss=3, accepted_ratio=0.9) num = np.sum(np.asarray([len(line) for line in list_lines])) self.assertTrue(num == self.num_dots)
def test_remove_residual_dots_hor(self): mat1 = np.copy(self.mat_dots) mat1[9:11, 42:44] = np.float32(1.0) list_lines = prep.group_dots_hor_lines(mat1, 0.0, 10.0, ratio=0.3, num_dot_miss=3, accepted_ratio=0.8) num1 = np.sum(np.asarray([len(line) for line in list_lines])) list_lines2 = prep.remove_residual_dots_hor(list_lines, 0.0, 1.5) num2 = np.sum(np.asarray([len(line) for line in list_lines2])) self.assertTrue(num1 == num2 + 1)
def calc_distor_coef(mat, num_coef, perspective=False): # Pre-processing mat1 = prep.binarization(mat) (dot_size, dot_dist) = prep.calc_size_distance(mat1) mat1 = prep.select_dots_based_size(mat1, dot_size) mat1 = prep.select_dots_based_ratio(mat1) hor_slope = prep.calc_hor_slope(mat1) ver_slope = prep.calc_ver_slope(mat1) list_hor_lines = prep.group_dots_hor_lines(mat1, hor_slope, dot_dist) list_ver_lines = prep.group_dots_ver_lines(mat1, ver_slope, dot_dist) list_hor_lines = prep.remove_residual_dots_hor(list_hor_lines, hor_slope) list_ver_lines = prep.remove_residual_dots_ver(list_ver_lines, ver_slope) if perspective is True: try: list_hor_lines, list_ver_lines = proc.regenerate_grid_points_parabola( list_hor_lines, list_ver_lines, perspective=perspective) except AttributeError: raise ValueError("Perspective correction only available " "from Discorpy 1.4!!!") # Processing (xcenter, ycenter) = proc.find_cod_coarse(list_hor_lines, list_ver_lines) list_fact = proc.calc_coef_backward(list_hor_lines, list_ver_lines, xcenter, ycenter, num_coef) return xcenter, ycenter, list_fact
subpixel=True) io.save_plot_points(output_base + "/ref_points_horizontal.png", list_points_hor_lines, height, width, color="red") io.save_plot_points(output_base + "/ref_points_vertical.png", list_points_ver_lines, height, width, color="blue") # Group points into lines list_hor_lines = prep.group_dots_hor_lines(list_points_hor_lines, slope_hor, dist_hor, ratio=0.1, num_dot_miss=2, accepted_ratio=0.8) list_ver_lines = prep.group_dots_ver_lines(list_points_ver_lines, slope_ver, dist_ver, ratio=0.1, num_dot_miss=2, accepted_ratio=0.8) # Remove residual dots list_hor_lines = prep.remove_residual_dots_hor(list_hor_lines, slope_hor, 2.0) list_ver_lines = prep.remove_residual_dots_ver(list_ver_lines, slope_ver, 2.0) # Save output for checking io.save_plot_image(output_base + "/horizontal_lines.png", list_hor_lines, height, width)
# Segment dots mat1 = prep.binarization(mat0) # Calculate the median dot size and distance between them. (dot_size, dot_dist) = prep.calc_size_distance(mat1) # Remove non-dot objects mat1 = prep.select_dots_based_size(mat1, dot_size) # Remove non-elliptical objects mat1 = prep.select_dots_based_ratio(mat1) io.save_image(output_base + "/segmented_dots.jpg", mat1) # Calculate the slopes of horizontal lines and vertical lines. hor_slope = prep.calc_hor_slope(mat1) ver_slope = prep.calc_ver_slope(mat1) print("Horizontal slope: {0}. Vertical slope: {1}".format(hor_slope, ver_slope)) # Group points to horizontal lines list_hor_lines = prep.group_dots_hor_lines(mat1, hor_slope, dot_dist) # Group points to vertical lines list_ver_lines = prep.group_dots_ver_lines(mat1, ver_slope, dot_dist) # Optional: remove horizontal outliners list_hor_lines = prep.remove_residual_dots_hor(list_hor_lines, hor_slope) # Optional: remove vertical outliners list_ver_lines = prep.remove_residual_dots_ver(list_ver_lines, ver_slope) # Save output for checking io.save_plot_image(output_base + "/horizontal_lines.png", list_hor_lines, height, width) io.save_plot_image(output_base + "/vertical_lines.png", list_ver_lines, height, width) # Check residual of dots on lines list_hor_data = post.calc_residual_hor(list_hor_lines, 0.0, 0.0) list_ver_data = post.calc_residual_ver(list_ver_lines, 0.0, 0.0)
slope_ver, dist_ver, ratio=1.0, sensitive=0.1) list_points_ver_lines = lprep.get_cross_points_ver_lines(mat0, slope_hor, dist_hor, ratio=1.0, sensitive=0.1) io.save_plot_points(output_base + "/hor_points.png", list_points_hor_lines, height, width) io.save_plot_points(output_base + "/ver_points.png", list_points_ver_lines, height, width) print("4-> Group points into lines !!!!") list_hor_lines = prep.group_dots_hor_lines(list_points_hor_lines, slope_hor, dist_hor) list_ver_lines = prep.group_dots_ver_lines(list_points_ver_lines, slope_ver, dist_ver) # Optional: remove residual dots list_hor_lines = prep.remove_residual_dots_hor(list_hor_lines, slope_hor, 2.0) list_ver_lines = prep.remove_residual_dots_ver(list_ver_lines, slope_ver, 2.0) print("5-> Correct perspective effect !!!!") # Optional: correct perspective effect. list_hor_lines, list_ver_lines = proc.regenerate_grid_points_parabola( list_hor_lines, list_ver_lines, perspective=True) io.save_plot_image(output_base + "/hor_lines.png", list_hor_lines, height, width) io.save_plot_image(output_base + "/ver_lines.png", list_ver_lines, height, width)
# Calculate the horizontal slope and the vertical slope of the grid using the # middle part of the image (30%). hor_slope = prep.calc_hor_slope(mat1, ratio=0.3) ver_slope = prep.calc_ver_slope(mat1, ratio=0.3) print("Horizontal slope: {0}\nVertical slope: {1}".format( hor_slope, ver_slope)) # Group dots into lines. The method searches nearby dots and decide if they # belong to the same line or not. The search-range in x-direction is # defined by num_dot_miss and the search-range in y-direction is defined # by the slope and the acceptable variation. Only lines with the number of # dots >= 70% of the maximum number of dots on a line are kept. list_hor_lines = prep.group_dots_hor_lines(mat1, hor_slope, dot_dist, ratio=0.3, num_dot_miss=6, accepted_ratio=0.7) list_ver_lines = prep.group_dots_ver_lines(mat1, ver_slope, dot_dist, ratio=0.3, num_dot_miss=6, accepted_ratio=0.7) io.save_plot_image(output_base + "/group_horizontal_dots.png", list_hor_lines, height, width) io.save_plot_image(output_base + "/group_vertical_dots.png", list_ver_lines, height, width) # Optional step: Remove residual dots. # The method uses coordinates of dots on each line for parabolic fit, then
mat0 = io.load_image(file_path) # Load image (height, width) = mat0.shape # Segment dots mat1 = prep.binarization(mat0) # Calculate the median dot size and distance between them. (dot_size, dot_dist) = prep.calc_size_distance(mat1) # Remove non-dot objects mat1 = prep.select_dots_based_size(mat1, dot_size) # Remove non-elliptical objects mat1 = prep.select_dots_based_ratio(mat1) io.save_image(output_base + "/segmented_dots.jpg", mat1) # Calculate the slopes of horizontal lines and vertical lines. hor_slope = prep.calc_hor_slope(mat1) ver_slope = prep.calc_ver_slope(mat1) # Group points into lines list_hor_lines = prep.group_dots_hor_lines(mat1, hor_slope, dot_dist, accepted_ratio=0.8) list_ver_lines = prep.group_dots_ver_lines(mat1, ver_slope, dot_dist, accepted_ratio=0.8) # Optional: remove outliners list_hor_lines = prep.remove_residual_dots_hor(list_hor_lines, hor_slope) list_ver_lines = prep.remove_residual_dots_ver(list_ver_lines, ver_slope) # Save output for checking io.save_plot_image(output_base + "/horizontal_lines.png", list_hor_lines, height, width) io.save_plot_image(output_base + "/vertical_lines.png", list_ver_lines, height, width) list_hor_data = post.calc_residual_hor(list_hor_lines, 0.0, 0.0) list_ver_data = post.calc_residual_ver(list_ver_lines, 0.0, 0.0) io.save_residual_plot(output_base + "/hor_residual_before_correction.png", list_hor_data, height, width) io.save_residual_plot(output_base + "/ver_residual_before_correction.png",