def apply_found_rotation_or_skew(self, rot_or_skew_type, rot_or_skew_radians): """ Apply a found page rotation or skew to the detected lines in order to fix their rotation / skew. You can pass the output of find_rotation_or_skew into this method. """ if rot_or_skew_type is None or rot_or_skew_radians is None: return self.lines_hough if rot_or_skew_type == ROTATION: filter_line_dir = None else: # skew filter_line_dir = DIRECTION_HORIZONTAL if rot_or_skew_type == SKEW_Y else DIRECTION_VERTICAL lines_hough_deskewed = [] for rho, theta, theta_norm, line_dir in self.lines_hough: if filter_line_dir is None or (filter_line_dir is not None and line_dir == filter_line_dir): theta += rot_or_skew_radians theta_norm = normalize_angle(theta) lines_hough_deskewed.append((rho, theta, theta_norm, line_dir)) self.lines_hough = lines_hough_deskewed return self.lines_hough
def _generate_hough_lines(self, lines): """ From a list of lines in <lines> detected by cv2.HoughLines, create a list with a tuple per line containing: (rho, theta, normalized theta with 0 <= theta_norm < np.pi, DIRECTION_VERTICAL or DIRECTION_HORIZONTAL) """ lines_hough = [] for l in lines: rho, theta = l[0] # they come like this from OpenCV's hough transform theta_norm = normalize_angle(theta) if abs(PIHLF - theta_norm) > PI4TH: # vertical line_dir = DIRECTION_VERTICAL else: line_dir = DIRECTION_HORIZONTAL lines_hough.append((rho, theta, theta_norm, line_dir)) return lines_hough
def _generate_hough_lines(self, lines): """ From a list of lines in <lines> detected by cv2.HoughLines, create a list with a tuple per line containing: (rho, theta, normalized theta with 0 <= theta_norm < np.pi, DIRECTION_VERTICAL or DIRECTION_HORIZONTAL) """ lines_hough = [] for l in lines: rho, theta = l[0] # they come like this from OpenCV's hough transform theta_norm = normalize_angle(theta) if abs(PIHLF - theta_norm) > PI4TH: # vertical line_dir = DIRECTION_VERTICAL else: line_dir = DIRECTION_HORIZONTAL lines_hough.append((rho, theta, theta_norm, line_dir)) return lines_hough
def apply_found_rotation_or_skew(self, rot_or_skew_type, rot_or_skew_radians): """ Apply a found page rotation or skew to the detected lines in order to fix their rotation / skew. You can pass the output of find_rotation_or_skew into this method. """ if rot_or_skew_type is None or rot_or_skew_radians is None: return self.lines_hough if rot_or_skew_type == ROTATION: filter_line_dir = None else: # skew filter_line_dir = DIRECTION_HORIZONTAL if rot_or_skew_type == SKEW_Y else DIRECTION_VERTICAL lines_hough_deskewed = [] for rho, theta, theta_norm, line_dir in self.lines_hough: if filter_line_dir is None or (filter_line_dir is not None and line_dir == filter_line_dir): theta += rot_or_skew_radians theta_norm = normalize_angle(theta) lines_hough_deskewed.append((rho, theta, theta_norm, line_dir)) self.lines_hough = lines_hough_deskewed return self.lines_hough
def test_normalize_angle(): for i in range(-10, 10): theta = i * np.pi norm = normalize_angle(theta) assert 0 <= norm < 2 * np.pi assert norm / np.pi == i % 2