def path_tracing(img_filtered, contours): """Find contours in input image using Douglas-Peuker algorithm Parameters ---------- img_filtered : Image Input image file in which to find contours. level : int Value along which to find contours in the array. contours : list of (n,2)-ndarrays Each contour is an ndarray of shape (n, 2), consisting of n (x, y) coordinates along the contour. Output ------- storage : 2D array of float coordinates Output array of detected contour coordinates. """ storage = [] for contour in contours: coords = approximate_polygon(contour, tolerance=0.1) #if isClosed(coords) and polygon_area(coords) > 100: if isClosed(coords) and polygon_area(coords) > 100: storage.append(coords) return storage
def path_tracing(img_filtered, contours): storage = [] for contour in contours: coords = approximate_polygon(contour, tolerance=0.1) #if isClosed(coords) and polygon_area(coords) > 100: if isClosed(coords) and polygon_area(coords) > 100: storage.append(coords) return storage
def max_index(img_filtered): """Find the level index that can yield the contour with largest area Parameters ---------- img_filtered : Image Input image file in which to find contours. Output ------- max_index : int Level index that can yield the contour with largest area """ max_index = 0 max_area = 0 #max_count = 0 # 100 - 110 for i in range(120, 130): area = 0 retval, threshold = cv2.threshold(img_filtered, i, 255, cv2.THRESH_BINARY) sk_img = image_denoise(img_filtered) sk_thre = image_denoise(threshold) contours = path_Marching_Squares(sk_thre, 0.45) for contour in contours: coords = approximate_polygon(contour, tolerance=1) if isClosed(coords) and polygon_area(coords) > 100: area += polygon_area(coords) if area > max_area: max_area = area max_index = i return max_index
def max_index(img_filtered): max_index = 0 max_area = 0 #max_count = 0 # 80 - 120 for i in range(100, 110): area = 0 retval, threshold = cv2.threshold(img_filtered, i, 255, cv2.THRESH_BINARY) sk_img = image_denoise(img_filtered) sk_thre = image_denoise(threshold) contours = path_Marching_Squares(sk_thre, 0.45) for contour in contours: coords = approximate_polygon(contour, tolerance=1) if isClosed(coords) and polygon_area(coords) > 100: area += polygon_area(coords) if area > max_area: max_area = area max_index = i return max_index
def drawContour(img_num, path, center_list): img = cv2.imread(path) threshold = preprocess(img) sk_img = image_denoise(img) sk_thre = image_denoise(threshold) contours = path_Marching_Squares(sk_thre, 0.45) fig, ax = plt.subplots(figsize=(10, 10)) plt.gray() ax.imshow(sk_img) for contour in contours: coords = approximate_polygon(contour, tolerance=0.1) #if isClosed(coords) and polygon_area(coords) > 100: if isClosed(coords) and polygon_area(coords) > 100: ax.plot(coords[:, 1], coords[:, 0], '-r', linewidth=2) for center in center_list: ax.scatter(center[1], center[0], marker='o') ax.axis((0, 256, 0, 256)) # save_path = '../data/auto/segmented_tube' + str(img_num) + '.jpg' # save_path = './sample_results/4/sample%03d.png'% (img_num) # save_path = './test_result/sample%03d.png'% (img_num) save_path = './inverse_result/sample%03d.png' % (img_num) plt.savefig(save_path)
sum_x = 0 sum_y = 0 length = len(points) for i in range(0, length): sum_x += points[i][0] sum_y += points[i][1] return (sum_x / length, sum_y / length) if __name__ == "__main__": path = './sample_images/4/sample109.png' img = cv2.imread(path) threshold = preprocess(img) sk_img = image_denoise(img) sk_thre = image_denoise(threshold) contours = path_Marching_Squares(sk_thre, 0.45) fig, ax = plt.subplots(figsize=(10,10)) plt.gray() ax.imshow(sk_img) # for contour in contours: for i in range(1, 4): contour = contours[i] coords = approximate_polygon(contour, tolerance=0.1) if isClosed(coords) and polygon_area(coords) > 100: ax.plot(coords[:, 1], coords[:, 0], '-r', linewidth=2) ax.axis((0,256,0,256)) plt.show()