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 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
file_path = "C:/data/dot_pattern_01.jpg" output_base = "./output_demo_01/" num_coef = 5 # Number of polynomial coefficients 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) 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,
print("Median size of dots: {0}\nMedian distance between two dots: {1}".format( dot_size, dot_dist)) # Select dots with size in the range of [dot_size - dot_size*ratio; dot_size + # dot_size*ratio] mat1 = prep.select_dots_based_size(mat1, dot_size, ratio=0.3) io.save_image(output_base + "/cleaned_1_image.tif", mat1) # Select dots with the ratio between the major axis and the minor axis (of a # fitted ellipse) in the range of (1; 1 + ratio). mat1 = prep.select_dots_based_ratio(mat1, ratio=0.5) io.save_image(output_base + "/cleaned_2_image.tif", mat1) # 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,
def test_calc_hor_slope(self): mat_rot = np.int16( np.ceil(ndi.rotate(self.mat_dots, -3.0, reshape=False, order=1))) hor_slope = prep.calc_hor_slope(mat_rot, ratio=1.0) angle = np.rad2deg(np.arctan(hor_slope)) self.assertTrue(np.abs(angle - 3.0) <= 0.2)