stouputils.data_science.data_processing.image.canny module#

canny_image(image: ndarray[Any, dtype[Any]], threshold1: float, threshold2: float, aperture_size: int = 3, sigma: float = 3, stop_at_nms: bool = False, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply Canny edge detection to an image.

Parameters:
  • image (NDArray[Any]) – Image to apply Canny edge detection

  • threshold1 (float) – First threshold for hysteresis (between 0 and 1)

  • threshold2 (float) – Second threshold for hysteresis (between 0 and 1)

  • aperture_size (int) – Aperture size for Sobel operator (3, 5, or 7)

  • sigma (float) – Standard deviation for Gaussian blur

  • stop_at_nms (bool) – Stop at non-maximum suppression step (don’t apply thresholding)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Image with Canny edge detection applied

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[100, 150, 200], [50, 125, 175], [25, 75, 225]])
>>> edges = canny_image(image.astype(np.uint8), 0.1, 0.2)
>>> edges.shape == image.shape[:2]  # Canny returns single channel
True
>>> set(np.unique(edges).tolist()) <= {0, 255}  # Only contains 0 and 255
True
>>> rgb = np.random.randint(0, 256, (3,3,3), dtype=np.uint8)
>>> edges_rgb = canny_image(rgb, 0.1, 0.2)
>>> edges_rgb.shape == rgb.shape[:2]  # Canny returns single channel
True
>>> ## Test invalid inputs
>>> canny_image("not an image", 0.1, 0.2)
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> canny_image(image.astype(np.uint8), 1.5, 0.2)
Traceback (most recent call last):
        ...
AssertionError: threshold1 must be between 0 and 1, got 1.5