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

Changed live_wire to a module to implement into GUI

parent d60e2b78
No related branches found
No related tags found
No related merge requests found
...@@ -9,23 +9,44 @@ from skimage.graph import route_through_array ...@@ -9,23 +9,44 @@ from skimage.graph import route_through_array
#### Helper functions #### #### Helper functions ####
def load_image(path, type): def compute_cost_image(path, sigma=3):
"""
Load an image in either gray or color mode (then convert color to gray). ### Load image
""" image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
if type == 'gray':
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) # Apply histogram equalization
if img is None: image_contrasted = exposure.equalize_adapthist(image, clip_limit=0.01)
raise FileNotFoundError(f"Could not read {path}")
elif type == 'color': # Apply smoothing
img = cv2.imread(path, cv2.IMREAD_COLOR) smoothed_img = gaussian(image_contrasted, sigma=sigma)
if img is None:
raise FileNotFoundError(f"Could not read {path}") # Apply Canny edge detection
else: canny_img = canny(smoothed_img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else: # Create cost image
raise ValueError("type must be 'gray' or 'color'") cost_img = 1.0 / (canny_img + 1e-5) # Invert edges: higher cost where edges are stronger
return img
return cost_img
def find_path(cost_image, points):
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
def downscale(img, points, scale_percent): def downscale(img, points, scale_percent):
""" """
...@@ -89,68 +110,3 @@ def export_path(path_coords, path_name): ...@@ -89,68 +110,3 @@ def export_path(path_coords, path_name):
""" """
np.save(path_name, path_coords) np.save(path_name, path_coords)
return None return None
#### Main Script ####
def main():
# Define input parameters
image_path = 'agamodon_slice.png'
image_type = 'gray' # 'gray' or 'color'
downscale_factor = 100 # % of original size
points_path = 'agamodonPoints.npy'
# Load image
image = load_image(image_path, image_type)
# Load seed and target points
points = np.int0(np.round(np.load(points_path))) # shape: (2, 2), i.e. [[x_seed, y_seed], [x_target, y_target]]
# Downscale image and points
scaled_image, scaled_points = downscale(image, points, downscale_factor)
seed, target = scaled_points # Each is (x, y)
# Convert to row,col for scikit-image (which uses (row, col) = (y, x))
seed_rc = (seed[1], seed[0])
target_rc = (target[1], target[0])
# Compute cost image
cost_image, canny_img = compute_cost(scaled_image)
# Find path using route_through_array
# route_through_array expects: route_through_array(image, start, end, fully_connected=True/False)
start_time = time.time()
path_rc, cost = route_through_array(
cost_image,
start=seed_rc,
end=target_rc,
fully_connected=True
)
end_time = time.time()
print(f"Elapsed time for pathfinding: {end_time - start_time:.3f} seconds")
# Convert single-channel image to BGR for coloring
color_img = cv2.cvtColor(scaled_image, cv2.COLOR_GRAY2BGR)
# Draw path. `path_rc` is a list of (row, col).
# If you want to mark it in red, do (0,0,255) because OpenCV uses BGR format.
color_img = backtrack_pixels_on_image(color_img, path_rc, bgr_color=(0, 0, 255))
# Export path
export_path(path_rc, 'agamodonPath.npy')
# Display results
plt.figure(figsize=(20, 8))
plt.subplot(1, 2, 1)
plt.title("Cost image")
plt.imshow(cost_image, cmap='gray')
plt.subplot(1, 2, 2)
plt.title("Path from Seed to Target")
# Convert BGR->RGB for pyplot
plt.imshow(color_img[..., ::-1])
plt.show()
if __name__ == "__main__":
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment