def ftm_pyramid(input_file, template_file, max_level = 5): img = iu.get_image(input_file) tpl = iu.get_image(template_file) image = cv.cvtColor(img, cv.COLOR_BGR2GRAY) template = cv.cvtColor(tpl, cv.COLOR_BGR2GRAY) tm_results = temp_match(image, template, max_level) c = 0 flag = False while flag is False and c < np.size(tm_results): current = tm_results[c] min_val, max_val, min_loc, max_loc = cv.minMaxLoc(current) if max_val > 0.9: cv.rectangle(img, max_loc, (max_loc[0] + template.shape[1], max_loc[1] + template.shape[0]), (0,0,255), 2) else: flag = True c = c+1 cv.imshow("Result", img) cv.waitKey() return 0
def delete_lines(input_file, lines, probabilistic=False, line_length=1000, width=5, color=(255, 255, 255)): """ >>> lines = detect_lines(test_image) >>> isinstance(delete_lines(test_image, lines), np.ndarray) True >>> lines = detect_lines(test_image, probabilistic=True) >>> isinstance(delete_lines(test_image, lines, probabilistic=True), np.ndarray) True >>> delete_lines(None, lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> delete_lines("", lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> delete_lines("fakeRoute", lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> delete_lines(test_image, None) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Lines can't be None. >>> delete_lines(test_image, lines, line_length=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Line_length value must be greater than 0. >>> delete_lines(test_image, lines, width=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Width value must be greater than 0. >>> delete_lines(test_image, lines, 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 check_lines(lines) check_line_length(line_length) check_width(width) iu.check_color(color) image = iu.get_image(input_file) return draw_lines(image, lines, probabilistic, line_length, width, color)
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 draw_corners(input_file, corners, radius=5, color=(0, 0, 255), thickness=-1): """ >>> corners = detect_corners(test_image) >>> isinstance(draw_corners(test_image, corners), np.ndarray) True >>> draw_corners(None, corners) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> draw_corners("", corners) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> draw_corners("fakeRoute", corners) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> draw_corners(test_image, None) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Corners can't be None. >>> draw_corners(test_image, []) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Corners can't be void. >>> draw_corners(test_image, corners, 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). >>> draw_corners(test_image, corners, radius=-5) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Radius value must be greater than 0. """ # Checking arguments check_corners(corners) check_radius(radius) iu.check_color(color) img = iu.get_image(input_file) for i in corners: x, y = i.ravel() cv.circle(img, (x, y), radius, color, thickness) return img
def in_contour(input_file, point, squares=True, thresh_val=255, k_size=5, iterations=30): """ >>> isinstance(in_contour(test_image, (200, 200)), np.ndarray) True >>> in_contour(test_image, (0, 0)) -1 >>> in_contour(None, (0,0)) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> in_contour("", (0,0)) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> in_contour("fakeRoute", (0,0)) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> in_contour(test_image, (0,0), thresh_val=-200) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> in_contour(test_image, (0,0), k_size=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Kernel size value must be greater than 0. >>> in_contour(test_image, (0,0), iterations=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Iterations value must be greater than 0. """ image = iu.get_image(input_file) check_threshold(thresh_val) check_kernel(k_size) check_iterations(iterations) contours = detect_contours(image, thresh_val, k_size, iterations) if squares == True: contours = get_squares(contours) contours = join_contours(contours) cnt = -1 for c in contours: if cv.pointPolygonTest(c, point, measureDist=False) >= 0: cnt = c return cnt
def delete_lines(input_file, lines, width=5, color=(255, 255, 255)): image = iu.get_image(input_file) check_lines(lines) check_width(width) check_color(color) return draw_lines(image, lines, width, color)
def get_gray_intensity_analysis(input_file, split_x=3, split_y=3): """ >>> isinstance(get_gray_intensity_analysis(test_image), list) True >>> get_gray_intensity_analysis(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> get_gray_intensity_analysis("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> get_gray_intensity_analysis("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> get_gray_intensity_analysis(test_image, split_x=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: The split value must be greater than 0. >>> get_gray_intensity_analysis(test_image, split_y=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: The split value must be greater than 0. """ # Checking arguments check_split(split_x) check_split(split_y) # Loading the image image = iu.get_image(input_file, 0) results = [] # Calculating split coords coordinates = iu.split_image(image, split_x, split_y) # For each split, get it's gray intensity value. for i, coord in enumerate(coordinates): x1, x2 = coord[1] y1, y2 = coord[0] crop = image[x1:x2, y1:y2] if crop is None or crop == []: raise Exception("Unexpected error while cropping the image.") intensity = get_gray_intensity(crop) results.append(intensity) return results
def ftm_pyramid(input_file, template_file, max_level=5): image = iu.get_image(input_file, 0) template = iu.get_image(template_file, 0) tm_results = temp_match(image, template, max_level) print len(tm_results) for r in tm_results: min_val, max_val, min_loc, max_loc = cv.minMaxLoc(r) if max_val > 0.99: cv.rectangle(image, max_loc, (max_loc[0] + template.shape[1], max_loc[1] + template.shape[0]), (0, 0, 255), 1) cv.imshow("Result", image) cv.waitKey() return 0
def temp_match(input, template, max_level): results = [] input = iu.get_image(input) source_pyr = buildPyramid(input, max_level) template_pyr = buildPyramid(template, max_level) for lvl in range(0, int(max_level), 1): curr_image = source_pyr[lvl] curr_template = template_pyr[lvl] dX = curr_image.shape[1] + 1 - curr_template.shape[1] dY = curr_image.shape[0] + 1 - curr_template.shape[0] result = np.zeros([dX, dY]) #On the first level performs regular template matching. if lvl == 0: result = cv.matchTemplate(curr_image, curr_template, cv.TM_CCORR_NORMED) #On every other level, perform pyramid transformation and template #matching on the predefined ROI areas, obtained using the result of the #previous level. else: mask = cv.pyrUp(r) mask8u = cv.inRange(mask, 0, 255) contours = cv.findContours(mask8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE) #Uses contours to define the region of interest and perform TM on #the areas. for i in range(0, np.size(contours)-1): x, y, w, h = cv.boundingRect(contours[i][0]) tpl_X = curr_template.shape[1] tpl_Y = curr_template.shape[0] #result = cv.matchTemplate(curr_image, curr_template, # cv.TM_CCORR_NORMED) result[y:y+h, x:x+w] = cv.matchTemplate( curr_image[y:y+h+tpl_Y, x:x+w+tpl_X], curr_template, cv.TM_CCORR_NORMED) T, r = cv.threshold(result, 0.94, 1., cv.THRESH_TOZERO) cv.imshow("test", r) cv.waitKey() results.append(r) return results
def detect_corners(input_file, max_corners=10, min_dist=50, trust_val=0.5): """ >>> isinstance(detect_corners(test_image), np.ndarray) True >>> detect_corners(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> detect_corners("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> detect_corners("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> detect_corners(test_image, max_corners=-5) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Max_corners value must be greater than 0. >>> detect_corners(test_image, min_dist=-100) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_dist value must be greater than 0. >>> detect_corners(test_image, trust_val=5) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Trust_val value must be between 0 and 1. """ # Checking arguments check_max_corners(max_corners) check_min_dist(min_dist) check_trust_val(trust_val) image = iu.get_image(input_file) contours = detect_contours(image) blank = image blank[:] = 255 img = draw_contours(blank, contours, 10, color=(0, 0, 0)) img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) corners = cv.goodFeaturesToTrack(img, max_corners, trust_val, min_dist) return corners
def build_pyramid(input_file, max_level): image = iu.get_image(input_file) results = [image] aux = image.copy() for i in range(0, max_level): aux = cv.pyrDown(aux, (aux.shape[1]/2, aux.shape[0]/2)) results = [aux] + results return results
def buildPyramid(input_file, max_level): image = iu.get_image(input_file) results = [image] aux = image for i in range(0,max_level): aux = cv.pyrDown(aux) results = [aux] + results return results
def build_pyramid(input_file, max_level): image = iu.get_image(input_file) results = [image] aux = image.copy() for i in range(0, max_level): aux = cv.pyrDown(aux, (aux.shape[1] / 2, aux.shape[0] / 2)) results = [aux] + results return results
def get_model(input_file, split_x=3, split_y=3): """ >>> isinstance(get_model(test_image), list) True >>> get_model(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> get_model("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> get_model("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> get_model(test_image, split_x=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: The split value must be greater than 0. >>> get_model(test_image, split_y=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: The split value must be greater than 0. """ # Checking arguments check_split(split_x) check_split(split_y) # Loading the image image = iu.get_image(input_file, 0) mean = math.ceil(np.mean(image)) results = get_gray_intensity_analysis(input_file, split_x, split_y) for i, r in enumerate(results): if r <= mean: results[i] = 1 else: results[i] = 0 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 draw_lines(input_file, lines, width=5, color=(0, 0, 255)): image = iu.get_image(input_file) check_lines(lines) check_width(width) check_color(color) if np.size(lines[0]) == 1: x1, y1, x2, y2 = lines cv.line(image, (x1, y1), (x2, y2), color, width) else: for x1, y1, x2, y2 in lines[0]: cv.line(image, (x1, y1), (x2, y2), color, width) return image
def draw_contours(input_file, contours, thickness=0, color=(0, 0, 255)): """ >>> contours = detect_contours(test_image) >>> isinstance(draw_contours(test_image, contours), np.ndarray) True >>> draw_contours(None, contours) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> draw_contours("", contours) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> draw_contours("fakeRoute", contours) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> draw_contours(test_image, None) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Contours can't be None. >>> draw_contours(test_image, []) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Contours can't be void. >>> draw_contours(test_image, contours, 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 check_contours(contours) iu.check_color(color) image = iu.get_image(input_file) for cnt in contours: cv.drawContours(image, [cnt], 0, color, thickness) return image
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
def apply(input_file, thresh_values=[250, 245, 240, 230, 225, 220]): # If thresh is not a list of values transform it. if not isinstance(thresh_values, list): thresh_values = [thresh_values] # Checking arguments and raising expected exceptions check_arguments(thresh_values) image = iu.get_image(input_file) results = [] for i in thresh_values: th, img_thresh = cv.threshold(image, float(i), 255, cv.THRESH_BINARY) results = results + [img_thresh] return results
def clean(input_file, thresh_val = [225, 220, 215, 210, 205, 200], window_size = 3): # Ensures that window_size parameter is integer window_size = int(window_size) # Checking arguments and raising expected exceptions check_arguments(window_size) # Loading the image image = iu.get_image(input_file) # Gray-scale and Gaussian Blur image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image = cv.GaussianBlur(image, (window_size, window_size), 0) # Applying threshold list results = th.apply(image, thresh_val) return results
def delete_all_lines(input_file, width=5, color=(255, 255, 255), min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi/180, threshold=200, min_line_length=30, max_line_gap=20): image = iu.get_image(input_file) check_width(width) check_color(color) check_canny_args(min_val, max_val, aperture_size) check_houghlines_p_args(rho, theta, threshold, min_line_length, max_line_gap) lines = detect_lines(image, min_val, max_val, aperture_size, rho, theta, threshold, min_line_length, max_line_gap) while lines is not None: image = delete_lines(image, lines, width, color) lines = detect_lines(image, rho, theta, threshold, min_line_length, max_line_gap) return image
def detect_lines(input_file, min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi/180, threshold=200, min_line_length=30, max_line_gap=20): if min_val < max_val: image = iu.get_image(input_file) check_canny_args(min_val, max_val, aperture_size) check_houghlines_p_args(rho, theta, threshold, min_line_length, max_line_gap) gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) edges = cv.Canny(gray, min_val, max_val, aperture_size) lines = cv.HoughLinesP(edges, rho, theta, threshold, min_line_length, max_line_gap) else: lines = None return lines
def clean(input_file, thresh_val = [250, 245, 240, 230, 225, 220], window_size = 5, kernel_size = 5): # Ensures that both quality and window_size parameters are integers window_size = int(window_size) kernel_size = int(kernel_size) #Checking arguments and raising expected exceptions check_arguments(window_size, kernel_size) # Loading the image image = iu.get_image(input_file) # Applying Grayscale, Gaussian and median blur and erode image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image = cv.GaussianBlur(image, (window_size, window_size), 0) image = cv.medianBlur(image, window_size) image = cv.erode(image, (kernel_size, kernel_size)) # Applying threshold list results = th.apply(image, thresh_val) return results
def get_gray_intensity(input_file): """ >>> isinstance(get_gray_intensity(test_image), float) True >>> get_gray_intensity(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> get_gray_intensity("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> get_gray_intensity("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. """ image = iu.get_image(input_file, 0) return math.floor(np.mean(image))
def clean(input_file, thresh_val=200, window_size=5, kernel_size=5): """ >>> isinstance(clean(test_image), np.ndarray) True >>> clean(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> clean("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> clean("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> clean(test_image, thresh_val=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> clean(test_image, thresh_val=260) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> clean(test_image, window_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Window size value must be greater than 0. >>> clean(test_image, window_size=2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Window size value must be odd. >>> clean(test_image, kernel_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Kernel size value must be greater than 0. >>> clean(test_image, kernel_size=2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Kernel size value must be odd. """ # Checking arguments check_kernel_size(kernel_size) check_threshold(thresh_val) check_window_size(window_size) # Loading the image image = iu.get_image(input_file, 0) # Applying Gaussian and median blur and erode image = cv.GaussianBlur(image, (window_size, window_size), 0) image = cv.medianBlur(image, window_size) image = cv.erode(image, (kernel_size, kernel_size)) return th.adaptive_threshold(image, thresh_val)
def delete_all_lines(input_file, probabilistic=False, min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi / 180, threshold=200, min_line_length=30, max_line_gap=20, line_length=1000, width=5, color=(255, 255, 255)): """ >>> isinstance(delete_all_lines(test_image), np.ndarray) True >>> isinstance(delete_all_lines(test_image, probabilistic=True), np.ndarray) True >>> delete_all_lines(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> delete_all_lines("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> delete_all_lines("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> delete_all_lines(test_image, min_val=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be between 0 and 255. >>> delete_all_lines(test_image, max_val=300) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Max_val value must be between 0 and 255. >>> delete_all_lines(test_image, min_val=100, max_val=30) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be lesser than max_val. >>> delete_all_lines(test_image, aperture_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Aperture_size value must be greater than 0. >>> delete_all_lines(test_image, rho=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Rho value must be greater than 0. >>> delete_all_lines(test_image, theta=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Theta value must be greater than 0. >>> delete_all_lines(test_image, threshold=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold value must be greater than 0. >>> delete_all_lines(test_image, line_length=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Line_length value must be greater than 0. >>> delete_all_lines(test_image, width=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Width value must be greater than 0. >>> delete_all_lines(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 if probabilistic is False: check_line_length(line_length) check_canny_args(min_val, max_val, aperture_size) check_houghlines_args(rho, theta, threshold, min_line_length, max_line_gap, probabilistic) check_width(width) iu.check_color(color) image = iu.get_image(input_file) lines = detect_lines(image, probabilistic, min_val, max_val, aperture_size, rho, theta, threshold, min_line_length, max_line_gap) while lines is not None: image = delete_lines(image, lines, probabilistic, line_length, width, color) lines = detect_lines(image, probabilistic, rho, theta, threshold, min_line_length, max_line_gap) return image
def adaptive_threshold(input_file, max_val=255, thresh_type=0, block_size=11, c=5, mode=0): """ >>> isinstance(adaptive_threshold(test_image), np.ndarray) True >>> isinstance(adaptive_threshold(test_image, mode=1), np.ndarray) True >>> adaptive_threshold(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> adaptive_threshold("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> adaptive_threshold("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> adaptive_threshold(test_image, max_val=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> adaptive_threshold(test_image, max_val=260) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> adaptive_threshold(test_image, thresh_type=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. >>> adaptive_threshold(test_image, thresh_type=6) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. >>> adaptive_threshold(test_image, block_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Block size value must be greater than 0. >>> adaptive_threshold(test_image, block_size=2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Block size value must be odd. >>> adaptive_threshold(test_image, c='a') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Constraint must be integer. >>> adaptive_threshold(test_image, c='a') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Constraint must be integer. >>> adaptive_threshold(test_image, mode=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Mode values are: 0 (Gaussian) and 1 (Mean). """ # Checking arguments check_threshold(max_val) check_thresh_type(thresh_type) check_block_size(block_size) check_c(c) if mode == 0: mode = cv.ADAPTIVE_THRESH_GAUSSIAN_C elif mode == 1: mode = cv.ADAPTIVE_THRESH_MEAN_C else: raise ValueError("Mode values are: 0 (Gaussian) and 1 (Mean).") # Loading image image = iu.get_image(input_file, 0) return cv.adaptiveThreshold(image, maxValue=max_val, thresholdType=thresh_type, blockSize=block_size, C=c, adaptiveMethod=mode)
def threshold(input_file, thresh_val=200, new_value=255, thresh_type=0): """ >>> isinstance(threshold(test_image), np.ndarray) True >>> threshold(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> threshold("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> threshold("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> threshold(test_image, thresh_val=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> threshold(test_image, thresh_val=260) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> threshold(test_image, new_value=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> threshold(test_image, new_value=260) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: All threshold values must be between 0 and 255. >>> threshold(test_image, thresh_type=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. >>> threshold(test_image, thresh_type=6) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. """ # Checking arguments check_threshold(thresh_val) check_threshold(new_value) check_thresh_type(thresh_type) # Loading image image = iu.get_image(input_file, 0) # Thresholding th, img_thresh = cv.threshold(image, thresh_val, new_value, thresh_type) return img_thresh
path, 1024, 1024, args.resolution, args.resolution) elif 'celeba' in args.dataset.lower(): if args.resolution == None: args.resolution = 128 output_path = './stats/celeba_{}x{}_fid_stats.npz'.format( args.resolution, args.resolution) image_list = sorted(glob.glob(args.data_path))[:n_samples] image_loader = lambda path: get_celeba_image( path, 256, 256, args.resolution, args.resolution) elif 'lsun' in args.dataset.lower(): if args.resolution == None: args.resolution = 256 output_path = './stats/lsun_{}x{}_fid_stats.npz'.format( args.resolution, args.resolution) image_list = sorted(glob.glob(args.data_path))[:n_samples] image_loader = lambda path: get_image(path, 256, 256, args.resolution, args.resolution) else: raise NotImplementedError() assert n_samples <= len(image_list) print("%d images found and loaded" % len(image_list)) print("create inception graph..", end=" ", flush=True) fid.create_inception_graph( inception_path) # load the graph into the current TF graph print("ok") print("calculte FID stats..", end=" ", flush=True) feature_map = np.zeros((n_samples, 2048)) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True with tf.Session(config=config) as sess:
def draw_lines(input_file, lines, probabilistic=False, line_length=1000, width=5, color=(0, 0, 255)): """ >>> lines = detect_lines(test_image) >>> isinstance(draw_lines(test_image, lines), np.ndarray) True >>> linesp = detect_lines(test_image, probabilistic=True) >>> isinstance(draw_lines(test_image, linesp, probabilistic=True), np.ndarray) True >>> draw_lines(None, lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> draw_lines("", lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> draw_lines("fakeRoute", lines) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> draw_lines(test_image, None) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Lines can't be None. >>> draw_lines(test_image, lines, line_length=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Line_length value must be greater than 0. >>> draw_lines(test_image, lines, width=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Width value must be greater than 0. >>> draw_lines(test_image, lines, 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 check_lines(lines) check_line_length(line_length) check_width(width) iu.check_color(color) # Loading the image image = iu.get_image(input_file) if np.size(lines[0]) == 1: l_array = [lines] else: l_array = lines[0] for l in l_array: x1, y1, x2, y2 = get_line_coordinates(l, probabilistic, line_length) p1 = (x1, y1) p2 = (x2, y2) cv.line(image, p1, p2, color, width) return image
def detect_lines(input_file, probabilistic=False, min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi / 180, threshold=200, min_line_length=30, max_line_gap=20): """ >>> isinstance(detect_lines(test_image), np.ndarray) True >>> isinstance(detect_lines(test_image, probabilistic=True), np.ndarray) True >>> detect_lines(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> detect_lines("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> detect_lines("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> detect_lines(test_image, min_val=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be between 0 and 255. >>> detect_lines(test_image, max_val=300) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Max_val value must be between 0 and 255. >>> detect_lines(test_image, aperture_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Aperture_size value must be greater than 0. >>> detect_lines(test_image, rho=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Rho value must be greater than 0. >>> detect_lines(test_image, theta=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Theta value must be greater than 0. >>> detect_lines(test_image, threshold=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold value must be greater than 0. """ if min_val < max_val: # Checking arguments check_canny_args(min_val, max_val, aperture_size) check_houghlines_args(rho, theta, threshold, min_line_length, max_line_gap, probabilistic) image = iu.get_image(input_file) if probabilistic is False: edges = cv.Canny(image, min_val, max_val, aperture_size) lines = cv.HoughLines(edges, rho, theta, threshold) else: image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) edges = cv.Canny(image, min_val, max_val, aperture_size) lines = cv.HoughLinesP(edges, rho, theta, threshold, min_line_length, max_line_gap) else: lines = None return lines
def remove_bg(input_file, thresh_val=0, window_size=3, block_size=11, c=5, mode=0, thresh_type=0): """ >>> isinstance(remove_bg(test_image), np.ndarray) True >>> isinstance(remove_bg(test_image, mode=1), np.ndarray) True >>> isinstance(remove_bg(test_image, mode=2), np.ndarray) True >>> remove_bg(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> remove_bg("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> remove_bg("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> remove_bg(test_image, window_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Window size value must be greater than 0. >>> remove_bg(test_image, window_size=2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Window size value must be odd. >>> remove_bg(test_image, block_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Block size value must be greater than 0. >>> remove_bg(test_image, block_size=2) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Block size value must be odd. >>> remove_bg(test_image, c='a') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Constraint must be integer. >>> remove_bg(test_image, thresh_type=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. >>> remove_bg(test_image, thresh_type=6) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold_type value must be between 0 and 4. >>> remove_bg(test_image, mode=-1) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple). >>> remove_bg(test_image, mode=3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple). """ # Checking arguments check_threshold(thresh_val) check_window_size(window_size) check_block_size(block_size) check_c(c) check_mode(mode) # Loading image image = iu.get_image(input_file) # Removing noise by blurring and thresholding image = cv.GaussianBlur(image, (window_size, window_size), 0) result = [] if mode == 2: result = th.threshold(image, thresh_val, thresh_type=thresh_type) else: result = th.adaptive_threshold(image, block_size=block_size, c=c, mode=mode, thresh_type=thresh_type) return result
def delete_all_lines(input_file, probabilistic=False, min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi/180, threshold=200, min_line_length=30, max_line_gap=20, line_length=1000, width=5, color=(255, 255, 255)): """ >>> isinstance(delete_all_lines(test_image), np.ndarray) True >>> isinstance(delete_all_lines(test_image, probabilistic=True), np.ndarray) True >>> delete_all_lines(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> delete_all_lines("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> delete_all_lines("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> delete_all_lines(test_image, min_val=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be between 0 and 255. >>> delete_all_lines(test_image, max_val=300) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Max_val value must be between 0 and 255. >>> delete_all_lines(test_image, min_val=100, max_val=30) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be lesser than max_val. >>> delete_all_lines(test_image, aperture_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Aperture_size value must be greater than 0. >>> delete_all_lines(test_image, rho=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Rho value must be greater than 0. >>> delete_all_lines(test_image, theta=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Theta value must be greater than 0. >>> delete_all_lines(test_image, threshold=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold value must be greater than 0. >>> delete_all_lines(test_image, line_length=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Line_length value must be greater than 0. >>> delete_all_lines(test_image, width=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Width value must be greater than 0. >>> delete_all_lines(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 if probabilistic is False: check_line_length(line_length) check_canny_args(min_val, max_val, aperture_size) check_houghlines_args(rho, theta, threshold, min_line_length, max_line_gap, probabilistic) check_width(width) iu.check_color(color) image = iu.get_image(input_file) lines = detect_lines(image, probabilistic, min_val, max_val, aperture_size, rho, theta, threshold, min_line_length, max_line_gap) while lines is not None: image = delete_lines(image, lines, probabilistic, line_length, width, color) lines = detect_lines(image, probabilistic, rho, theta, threshold, min_line_length, max_line_gap) return image
def detect_lines(input_file, probabilistic=False, min_val=50, max_val=200, aperture_size=3, rho=1, theta=np.pi/180, threshold=200, min_line_length=30, max_line_gap=20): """ >>> isinstance(detect_lines(test_image), np.ndarray) True >>> isinstance(detect_lines(test_image, probabilistic=True), np.ndarray) True >>> detect_lines(None) Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be a None object >>> detect_lines("") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: The input file can't be ''. >>> detect_lines("fakeRoute") Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: Input file not found. >>> detect_lines(test_image, min_val=-10) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Min_val value must be between 0 and 255. >>> detect_lines(test_image, max_val=300) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Max_val value must be between 0 and 255. >>> detect_lines(test_image, aperture_size=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Aperture_size value must be greater than 0. >>> detect_lines(test_image, rho=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Rho value must be greater than 0. >>> detect_lines(test_image, theta=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Theta value must be greater than 0. >>> detect_lines(test_image, threshold=-3) Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: Threshold value must be greater than 0. """ if min_val < max_val: # Checking arguments check_canny_args(min_val, max_val, aperture_size) check_houghlines_args(rho, theta, threshold, min_line_length, max_line_gap, probabilistic) image = iu.get_image(input_file) if probabilistic is False: edges = cv.Canny(image, min_val, max_val, aperture_size) lines = cv.HoughLines(edges, rho, theta, threshold) else: image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) edges = cv.Canny(image, min_val, max_val, aperture_size) lines = cv.HoughLinesP(edges, rho, theta, threshold, min_line_length, max_line_gap) else: lines = None return lines