def detect_contours(input_file, thresh_val=255, k_size=5, iterations=30): """ >>> contours = detect_contours(test_image) >>> isinstance(detect_contours(test_image), list) True >>> detect_contours(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> detect_contours("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> detect_contours("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> detect_contours(test_image, thresh_val=270) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> detect_contours(test_image, k_size=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Kernel size value must be greater than 0. >>> detect_contours(test_image, iterations=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Iterations value must be greater than 0. """ gray = iu.get_image(input_file, 0) # Checking arguments check_threshold(thresh_val) check_kernel(k_size) check_iterations(iterations) gray = iu.pyramid_clean(gray) th2 = th.adaptive_threshold(gray, max_val=thresh_val, mode=cv.ADAPTIVE_THRESH_MEAN_C) th2 = cv.erode(th2, kernel=(k_size, k_size), iterations=iterations) th2 = cv.bitwise_not(th2) contours, h = cv.findContours(th2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) return contours
def get_analysis(input_file, min_dim=1000): original = iu.get_image(input_file) original = iu.pyramid_clean(original) cleaned = cc.delete_border_noise(original) contours = cc.detect_contours(cleaned) contours = cc.delete_small_contours(contours, min_dim) contours = cc.get_squares(contours) contours = cc.join_contours(contours, min_dist=10) blank = np.zeros(original.shape, np.uint8) blank[:] = 255 blank2 = np.zeros(original.shape, np.uint8) blank2[:] = 255 contours_lines = cc.draw_contours(blank, contours, color=(255, 0, 0), thickness=10) filled = cc.draw_contours(blank2, contours, color=(0, 0, 0), thickness=-1) if len(contours) > 0: dimension = 0 area = 0 squares = len(contours) for c in contours: dimension = dimension + cc.get_contour_dimension(c) area = area + cc.get_contour_area(c) else: dimension = -1 area = -1 squares = 0 model = aa.get_model(filled) corners = cc.detect_corners(contours_lines) num_corners = len(corners) results = [input_file, dimension, area, squares, model, num_corners ] return results
def get_analysis(input_file, min_dim=1000): original = iu.get_image(input_file) original = iu.pyramid_clean(original) cleaned = cc.delete_border_noise(original) contours = cc.detect_contours(cleaned) contours = cc.delete_small_contours(contours, min_dim) contours = cc.get_squares(contours) contours = cc.join_contours(contours, min_dist=10) blank = np.zeros(original.shape, np.uint8) blank[:] = 255 blank2 = np.zeros(original.shape, np.uint8) blank2[:] = 255 contours_lines = cc.draw_contours(blank, contours, color=(255, 0, 0), thickness=10) filled = cc.draw_contours(blank2, contours, color=(0, 0, 0), thickness=-1) if len(contours) > 0: dimension = 0 area = 0 squares = len(contours) for c in contours: dimension = dimension + cc.get_contour_dimension(c) area = area + cc.get_contour_area(c) else: dimension = -1 area = -1 squares = 0 model = aa.get_model(filled) corners = cc.detect_corners(contours_lines) num_corners = len(corners) results = [input_file, dimension, area, squares, model, num_corners] return results
def delete_border_noise(input_file, width=20, color=(255, 255, 255)): """ >>> isinstance(delete_border_noise(test_image), np.ndarray) True >>> delete_border_noise(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> delete_border_noise("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> delete_border_noise("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> delete_border_noise(test_image, width=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Width value must be greater than 0. >>> delete_border_noise(test_image, color=(-10, 0, 0)) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Color value must be: (0-255, 0-255, 0-255). """ # Checking arguments iu.check_color(color) check_width(width) image = iu.get_image(input_file) image = iu.pyramid_clean(image) image[0:width, :] = color image[:, 0:width] = color image[:, -width:] = color image[-width:, :] = color return image