Skip to content
Snippets Groups Projects
Commit b50c1a85 authored by Christian's avatar Christian
Browse files

Cleaned up in functions

parent 409c3002
No related branches found
No related tags found
No related merge requests found
import time
import cv2
import numpy as np
import matplotlib.pyplot as plt
......@@ -8,62 +7,40 @@ from skimage.feature import canny
from skimage.graph import route_through_array
from scipy.signal import convolve2d
'''
### Canny Edge cost image
def compute_cost_image(path, sigma=3):
### Disk live wire cost image
### Load image
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
def compute_disk_size(user_radius, upscale_factor=1.2):
return int(np.ceil(upscale_factor * 2 * user_radius + 1) // 2 * 2 + 1)
def load_image(path):
return cv2.imread(path, cv2.IMREAD_GRAYSCALE)
def preprocess_image(image, sigma=3, clip_limit=0.01):
# Apply histogram equalization
image_contrasted = exposure.equalize_adapthist(image, clip_limit=0.01)
image_contrasted = exposure.equalize_adapthist(image, clip_limit=clip_limit)
# Apply smoothing
smoothed_img = gaussian(image_contrasted, sigma=sigma)
# Apply Canny edge detection
canny_img = canny(smoothed_img)
# Create cost image
cost_img = 1.0 / (canny_img + 1e-5) # Invert edges: higher cost where edges are stronger
return smoothed_img
return cost_img
def find_path(cost_image, points):
def compute_cost_image(path, user_radius, sigma=3, clip_limit=0.01):
if len(points) != 2:
raise ValueError("Points should be a list of 2 points: seed and target.")
seed_rc, target_rc = points
path_rc, cost = route_through_array(
cost_image,
start=seed_rc,
end=target_rc,
fully_connected=True
)
return path_rc
'''
### Disk live wire cost image
def compute_cost_image(path, sigma=3, disk_size=15):
disk_size = compute_disk_size(user_radius)
### Load image
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
# Apply histogram equalization
image_contrasted = exposure.equalize_adapthist(image, clip_limit=0.01)
image = load_image(path)
# Apply smoothing
smoothed_img = gaussian(image_contrasted, sigma=sigma)
smoothed_img = preprocess_image(image, sigma=sigma, clip_limit=clip_limit)
# Apply Canny edge detection
canny_img = canny(smoothed_img)
# Do disk thing
binary_img = canny_img
k_size = 17
kernel = circle_edge_kernel(k_size=disk_size)
convolved = convolve2d(binary_img, kernel, mode='same', boundary='fill')
......@@ -128,12 +105,7 @@ def circle_edge_kernel(k_size=5, radius=None):
return kernel
# Other functions
# Other functions (to be implemented?)
def downscale(img, points, scale_percent):
"""
Downsample `img` to `scale_percent` size and scale the given points accordingly.
......@@ -161,38 +133,3 @@ def downscale(img, points, scale_percent):
scaled_target_xy = (int(target_xy[0] * scale_x), int(target_xy[1] * scale_y))
return downsampled_img, (scaled_seed_xy, scaled_target_xy)
\ No newline at end of file
def compute_cost(image, sigma=3.0, epsilon=1e-5):
"""
Smooth the image, run Canny edge detection, then invert the edge map into a cost image.
"""
# Apply histogram equalization
image_contrasted = exposure.equalize_adapthist(image, clip_limit=0.01)
# Apply smoothing
smoothed_img = gaussian(image_contrasted, sigma=sigma)
# Apply Canny edge detection
canny_img = canny(smoothed_img)
# Create cost image
cost_img = 1.0 / (canny_img + epsilon) # Invert edges: higher cost where edges are stronger
return cost_img, canny_img
def backtrack_pixels_on_image(img_color, path_coords, bgr_color=(0, 0, 255)):
"""
Color the path on the (already converted BGR) image in the specified color.
`path_coords` should be a list of (row, col) or (y, x).
"""
for (row, col) in path_coords:
img_color[row, col] = bgr_color
return img_color
def export_path(path_coords, path_name):
"""
Export the path to a np array.
"""
np.save(path_name, path_coords)
return None
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment