from skimage.feature import canny
from scipy.signal import convolve2d
from compute_disk_size import compute_disk_size
from load_image import load_image
from preprocess_image import preprocess_image
from circle_edge_kernel import circle_edge_kernel

def compute_cost_image(path, user_radius, sigma=3, clip_limit=0.01):

    disk_size = compute_disk_size(user_radius)

    ### Load image
    image = load_image(path)

    # Apply smoothing
    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
    kernel = circle_edge_kernel(k_size=disk_size)
    convolved = convolve2d(binary_img, kernel, mode='same', boundary='fill')

    # Create cost image
    cost_img = (convolved.max() - convolved)**4  # Invert edges: higher cost where edges are stronger

    return cost_img