Commit ·
03593fc
1
Parent(s): 6a3cd3b
Deploy Canny pipeline demo
Browse files- README.md +19 -5
- __pycache__/canny_stages.cpython-312.pyc +0 -0
- app.py +121 -0
- canny_stages.py +190 -0
- examples/coins.png +0 -0
- examples/fruits.jpg +0 -0
- examples/shapes.png +0 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,10 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
|
|
|
|
|
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Canny Edge Detection Pipeline
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.49.0
|
| 8 |
+
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Canny Edge Detection — Step by Step
|
| 14 |
+
|
| 15 |
+
Upload any image to see all seven stages of the Canny edge detector — grayscale,
|
| 16 |
+
Gaussian blur, Sobel gradients (magnitude + direction), non-maximum suppression,
|
| 17 |
+
double threshold, and hysteresis.
|
| 18 |
+
|
| 19 |
+
This Space runs the **NumPy reference implementation**. The full project also
|
| 20 |
+
includes a **custom CUDA pipeline (6 kernels) that runs 5–11× faster** than
|
| 21 |
+
NumPy on the same workloads, validated to a pixel-for-pixel match (IoU = 1.0000).
|
| 22 |
+
|
| 23 |
+
Source, benchmarks, and the CUDA implementation:
|
| 24 |
+
**https://github.com/AneeshB20/canny-edge-detection-cuda**
|
__pycache__/canny_stages.cpython-312.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio app for the Canny pipeline walkthrough.
|
| 2 |
+
|
| 3 |
+
Live demo of the NumPy implementation. The CUDA version (5-11x faster
|
| 4 |
+
depending on image size) lives in the GitHub repo:
|
| 5 |
+
https://github.com/AneeshB20/canny-edge-detection-cuda
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import time
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
from canny_stages import canny_pipeline_with_stages
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
GITHUB_URL = "https://github.com/AneeshB20/canny-edge-detection-cuda"
|
| 20 |
+
EXAMPLES_DIR = Path(__file__).parent / "examples"
|
| 21 |
+
|
| 22 |
+
EXAMPLE_IMAGES = [
|
| 23 |
+
str(EXAMPLES_DIR / "fruits.jpg"),
|
| 24 |
+
str(EXAMPLES_DIR / "shapes.png"),
|
| 25 |
+
str(EXAMPLES_DIR / "coins.png"),
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Captions for the 8 panels we display. Keep in sync with canny_stages keys.
|
| 30 |
+
STAGE_CAPTIONS = {
|
| 31 |
+
"original": "Original image (input).",
|
| 32 |
+
"grayscale": "Step 1: Convert to grayscale. Canny operates on single-channel intensity values.",
|
| 33 |
+
"gaussian": "Step 2: Gaussian blur. Removes noise that would create false edges.",
|
| 34 |
+
"magnitude": "Step 3: Sobel gradient magnitude. Bright = strong intensity change = potential edge.",
|
| 35 |
+
"direction": "Step 4: Gradient direction. Color shows edge orientation - needed for thinning.",
|
| 36 |
+
"nms": "Step 5: Non-maximum suppression. Thick edges thinned to 1-pixel width by keeping only local maxima along the gradient direction.",
|
| 37 |
+
"threshold": "Step 6: Double threshold. White = strong edges, gray = weak edges, black = suppressed.",
|
| 38 |
+
"edges": "Step 7: Hysteresis edge tracking. Weak edges kept only if connected to strong edges. Final result.",
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
STAGE_ORDER = ["original", "grayscale", "gaussian", "magnitude",
|
| 42 |
+
"direction", "nms", "threshold", "edges"]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def run_pipeline(image, sigma, low_thresh, high_thresh):
|
| 46 |
+
"""Gradio callback. Returns (gallery items, info markdown)."""
|
| 47 |
+
if image is None:
|
| 48 |
+
return [], "**Upload an image first.**"
|
| 49 |
+
if high_thresh < low_thresh:
|
| 50 |
+
return [], "**`high_thresh` must be >= `low_thresh`.**"
|
| 51 |
+
|
| 52 |
+
t0 = time.perf_counter()
|
| 53 |
+
stages = canny_pipeline_with_stages(
|
| 54 |
+
image, sigma=float(sigma),
|
| 55 |
+
low_thresh=float(low_thresh), high_thresh=float(high_thresh),
|
| 56 |
+
)
|
| 57 |
+
elapsed_ms = (time.perf_counter() - t0) * 1000.0
|
| 58 |
+
|
| 59 |
+
# Substitute sigma into the blur caption so the user sees what they picked.
|
| 60 |
+
captions = dict(STAGE_CAPTIONS)
|
| 61 |
+
captions["gaussian"] = (
|
| 62 |
+
f"Step 2: Gaussian blur (sigma = {float(sigma):.2f}). "
|
| 63 |
+
"Removes noise that would create false edges."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
gallery = [(stages[k], captions[k]) for k in STAGE_ORDER]
|
| 67 |
+
|
| 68 |
+
h, w = stages["edges"].shape[:2]
|
| 69 |
+
note = (
|
| 70 |
+
f"**Processing time:** {elapsed_ms:.1f} ms on {h}×{w} pixels (NumPy implementation).\n\n"
|
| 71 |
+
f"The CUDA version of this same pipeline runs **5-11x faster** than NumPy "
|
| 72 |
+
f"depending on image size - see the benchmark plots in the "
|
| 73 |
+
f"[GitHub repo]({GITHUB_URL})."
|
| 74 |
+
)
|
| 75 |
+
return gallery, note
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Canny Edge Detection - Step by Step") as demo:
|
| 79 |
+
gr.Markdown("# Canny Edge Detection - Step by Step")
|
| 80 |
+
gr.Markdown(
|
| 81 |
+
"Upload an image to see every stage of the Canny pipeline. "
|
| 82 |
+
"Built from scratch - no `cv2.Canny()`. "
|
| 83 |
+
f"[View the source on GitHub]({GITHUB_URL})."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column(scale=1):
|
| 88 |
+
input_image = gr.Image(label="Input image", type="numpy", height=300)
|
| 89 |
+
sigma = gr.Slider(0.5, 5.0, value=1.4, step=0.1,
|
| 90 |
+
label="Gaussian sigma (blur strength)")
|
| 91 |
+
low_thresh = gr.Slider(5, 100, value=25, step=1,
|
| 92 |
+
label="Low threshold")
|
| 93 |
+
high_thresh = gr.Slider(10, 200, value=70, step=1,
|
| 94 |
+
label="High threshold")
|
| 95 |
+
run_btn = gr.Button("Run Canny pipeline", variant="primary")
|
| 96 |
+
|
| 97 |
+
gr.Examples(
|
| 98 |
+
examples=[[p] for p in EXAMPLE_IMAGES if Path(p).exists()],
|
| 99 |
+
inputs=[input_image],
|
| 100 |
+
label="Try an example",
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Column(scale=2):
|
| 104 |
+
output_gallery = gr.Gallery(
|
| 105 |
+
label="Pipeline stages",
|
| 106 |
+
columns=2,
|
| 107 |
+
rows=4,
|
| 108 |
+
object_fit="contain",
|
| 109 |
+
height="auto",
|
| 110 |
+
)
|
| 111 |
+
output_info = gr.Markdown()
|
| 112 |
+
|
| 113 |
+
run_btn.click(
|
| 114 |
+
run_pipeline,
|
| 115 |
+
inputs=[input_image, sigma, low_thresh, high_thresh],
|
| 116 |
+
outputs=[output_gallery, output_info],
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
demo.launch()
|
canny_stages.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Self-contained NumPy Canny implementation exposing every intermediate stage.
|
| 2 |
+
|
| 3 |
+
This is a port of the project's canny_numpy.py for the Hugging Face Spaces
|
| 4 |
+
demo. The math is identical to the version shipping in the GitHub repo
|
| 5 |
+
(github.com/AneeshB20/canny-edge-detection-cuda) - only the wrapper changes
|
| 6 |
+
so we can display the seven intermediate images instead of just the final
|
| 7 |
+
binary edge map.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import cv2
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# ----------------------------------------------------------------------------
|
| 17 |
+
# Pipeline primitives (vectorized NumPy).
|
| 18 |
+
# ----------------------------------------------------------------------------
|
| 19 |
+
|
| 20 |
+
def convolve2d(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
|
| 21 |
+
"""Zero-padded 2D cross-correlation. Output shape = input shape."""
|
| 22 |
+
image = image.astype(np.float32)
|
| 23 |
+
kh, kw = kernel.shape
|
| 24 |
+
pad_h, pad_w = kh // 2, kw // 2
|
| 25 |
+
padded = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), mode="constant")
|
| 26 |
+
H, W = image.shape
|
| 27 |
+
out = np.zeros((H, W), dtype=np.float32)
|
| 28 |
+
for i in range(kh):
|
| 29 |
+
for j in range(kw):
|
| 30 |
+
out += kernel[i, j] * padded[i:i + H, j:j + W]
|
| 31 |
+
return out
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def gaussian_kernel(size: int = 5, sigma: float = 1.0) -> np.ndarray:
|
| 35 |
+
ax = np.arange(size, dtype=np.float32) - (size // 2)
|
| 36 |
+
xx, yy = np.meshgrid(ax, ax)
|
| 37 |
+
k = np.exp(-(xx ** 2 + yy ** 2) / (2.0 * sigma ** 2))
|
| 38 |
+
k /= k.sum()
|
| 39 |
+
return k.astype(np.float32)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def gaussian_blur(image: np.ndarray, size: int = 5, sigma: float = 1.0) -> np.ndarray:
|
| 43 |
+
return convolve2d(image, gaussian_kernel(size, sigma))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def sobel_gradients(image: np.ndarray):
|
| 47 |
+
Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32)
|
| 48 |
+
Ky = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.float32)
|
| 49 |
+
gx = convolve2d(image, Kx)
|
| 50 |
+
gy = convolve2d(image, Ky)
|
| 51 |
+
magnitude = np.sqrt(gx ** 2 + gy ** 2)
|
| 52 |
+
angle = np.arctan2(gy, gx)
|
| 53 |
+
return gx, gy, magnitude, angle
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def non_max_suppression(magnitude: np.ndarray, angle: np.ndarray) -> np.ndarray:
|
| 57 |
+
angle_deg = (np.rad2deg(angle) + 180.0) % 180.0
|
| 58 |
+
out = np.zeros_like(magnitude, dtype=np.float32)
|
| 59 |
+
|
| 60 |
+
center = magnitude[1:-1, 1:-1]
|
| 61 |
+
a = angle_deg[1:-1, 1:-1]
|
| 62 |
+
|
| 63 |
+
nw = magnitude[0:-2, 0:-2]; n = magnitude[0:-2, 1:-1]; ne = magnitude[0:-2, 2:]
|
| 64 |
+
w = magnitude[1:-1, 0:-2]; e = magnitude[1:-1, 2:]
|
| 65 |
+
sw = magnitude[2:, 0:-2]; s = magnitude[2:, 1:-1]; se = magnitude[2:, 2:]
|
| 66 |
+
|
| 67 |
+
horiz = (a < 22.5) | (a >= 157.5)
|
| 68 |
+
diag_a = (a >= 22.5) & (a < 67.5)
|
| 69 |
+
vert = (a >= 67.5) & (a < 112.5)
|
| 70 |
+
diag_b = (a >= 112.5) & (a < 157.5)
|
| 71 |
+
|
| 72 |
+
keep = np.zeros_like(center, dtype=bool)
|
| 73 |
+
keep |= horiz & (center >= w ) & (center >= e )
|
| 74 |
+
keep |= diag_a & (center >= nw) & (center >= se)
|
| 75 |
+
keep |= vert & (center >= n ) & (center >= s )
|
| 76 |
+
keep |= diag_b & (center >= ne) & (center >= sw)
|
| 77 |
+
|
| 78 |
+
out[1:-1, 1:-1] = np.where(keep, center, 0.0)
|
| 79 |
+
return out
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
WEAK = np.float32(75.0)
|
| 83 |
+
STRONG = np.float32(255.0)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def double_threshold(nms: np.ndarray, low_thresh: float, high_thresh: float) -> np.ndarray:
|
| 87 |
+
out = np.zeros_like(nms, dtype=np.float32)
|
| 88 |
+
out[nms >= high_thresh] = STRONG
|
| 89 |
+
out[(nms >= low_thresh) & (nms < high_thresh)] = WEAK
|
| 90 |
+
return out
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def hysteresis(classified: np.ndarray) -> np.ndarray:
|
| 94 |
+
out = classified.copy()
|
| 95 |
+
H, W = out.shape
|
| 96 |
+
padded = np.zeros((H + 2, W + 2), dtype=bool)
|
| 97 |
+
while True:
|
| 98 |
+
weak_mask = (out == WEAK)
|
| 99 |
+
if not weak_mask.any():
|
| 100 |
+
break
|
| 101 |
+
padded[1:-1, 1:-1] = (out == STRONG)
|
| 102 |
+
any_strong_neighbor = (
|
| 103 |
+
padded[0:-2, 0:-2] | padded[0:-2, 1:-1] | padded[0:-2, 2: ] |
|
| 104 |
+
padded[1:-1, 0:-2] | padded[1:-1, 2: ] |
|
| 105 |
+
padded[2:, 0:-2] | padded[2:, 1:-1] | padded[2:, 2: ]
|
| 106 |
+
)
|
| 107 |
+
promote = weak_mask & any_strong_neighbor
|
| 108 |
+
if not promote.any():
|
| 109 |
+
break
|
| 110 |
+
out[promote] = STRONG
|
| 111 |
+
out[out == WEAK] = 0.0
|
| 112 |
+
return out
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# ----------------------------------------------------------------------------
|
| 116 |
+
# Display helpers.
|
| 117 |
+
# ----------------------------------------------------------------------------
|
| 118 |
+
|
| 119 |
+
def _to_u8(arr: np.ndarray) -> np.ndarray:
|
| 120 |
+
"""Min-max stretch to [0, 255] uint8 for display."""
|
| 121 |
+
a = arr.astype(np.float32)
|
| 122 |
+
lo, hi = float(a.min()), float(a.max())
|
| 123 |
+
if hi > lo:
|
| 124 |
+
a = (a - lo) / (hi - lo) * 255.0
|
| 125 |
+
return np.clip(a, 0, 255).astype(np.uint8)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def _direction_to_rgb(angle: np.ndarray, magnitude: np.ndarray) -> np.ndarray:
|
| 129 |
+
"""Color-map gradient direction. Hue = angle, Value = magnitude so flat
|
| 130 |
+
regions stay dark (their direction is meaningless noise)."""
|
| 131 |
+
# Angle in (-pi, pi] -> [0, 180] for OpenCV HSV (which uses H in 0..179).
|
| 132 |
+
hue = ((np.rad2deg(angle) + 180.0) % 180.0).astype(np.uint8)
|
| 133 |
+
sat = np.full_like(hue, 255)
|
| 134 |
+
val = _to_u8(magnitude)
|
| 135 |
+
hsv = np.stack([hue, sat, val], axis=-1)
|
| 136 |
+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
# ----------------------------------------------------------------------------
|
| 140 |
+
# Public entry point used by app.py.
|
| 141 |
+
# ----------------------------------------------------------------------------
|
| 142 |
+
|
| 143 |
+
def canny_pipeline_with_stages(
|
| 144 |
+
image_rgb: np.ndarray,
|
| 145 |
+
sigma: float = 1.4,
|
| 146 |
+
low_thresh: float = 25.0,
|
| 147 |
+
high_thresh: float = 70.0,
|
| 148 |
+
gaussian_size: int = 5,
|
| 149 |
+
) -> dict[str, np.ndarray]:
|
| 150 |
+
"""Run the full Canny pipeline and return every intermediate stage as a
|
| 151 |
+
display-ready uint8 image.
|
| 152 |
+
|
| 153 |
+
Args:
|
| 154 |
+
image_rgb: (H, W, 3) RGB uint8 (what Gradio hands us) OR (H, W) grayscale.
|
| 155 |
+
sigma: std dev of the Gaussian blur.
|
| 156 |
+
low_thresh: lower magnitude threshold.
|
| 157 |
+
high_thresh: upper magnitude threshold.
|
| 158 |
+
gaussian_size: Gaussian kernel side (odd, default 5).
|
| 159 |
+
|
| 160 |
+
Returns:
|
| 161 |
+
dict with keys: original, grayscale, gaussian, magnitude, direction,
|
| 162 |
+
nms, threshold, edges. All values are uint8 arrays directly viewable.
|
| 163 |
+
"""
|
| 164 |
+
if image_rgb.ndim == 3 and image_rgb.shape[2] == 3:
|
| 165 |
+
gray = (0.299 * image_rgb[..., 0]
|
| 166 |
+
+ 0.587 * image_rgb[..., 1]
|
| 167 |
+
+ 0.114 * image_rgb[..., 2]).astype(np.float32)
|
| 168 |
+
original_disp = image_rgb
|
| 169 |
+
elif image_rgb.ndim == 2:
|
| 170 |
+
gray = image_rgb.astype(np.float32)
|
| 171 |
+
original_disp = np.stack([image_rgb] * 3, axis=-1)
|
| 172 |
+
else:
|
| 173 |
+
raise ValueError(f"unexpected image shape {image_rgb.shape}")
|
| 174 |
+
|
| 175 |
+
blurred = gaussian_blur(gray, gaussian_size, sigma)
|
| 176 |
+
gx, gy, magnitude, angle = sobel_gradients(blurred)
|
| 177 |
+
thin = non_max_suppression(magnitude, angle)
|
| 178 |
+
classified = double_threshold(thin, low_thresh, high_thresh)
|
| 179 |
+
edges = hysteresis(classified)
|
| 180 |
+
|
| 181 |
+
return {
|
| 182 |
+
"original": original_disp,
|
| 183 |
+
"grayscale": _to_u8(gray),
|
| 184 |
+
"gaussian": _to_u8(blurred),
|
| 185 |
+
"magnitude": _to_u8(magnitude),
|
| 186 |
+
"direction": _direction_to_rgb(angle, magnitude),
|
| 187 |
+
"nms": _to_u8(thin),
|
| 188 |
+
"threshold": classified.astype(np.uint8), # already 0/75/255 -> directly displayable
|
| 189 |
+
"edges": edges.astype(np.uint8), # already 0/255
|
| 190 |
+
}
|
examples/coins.png
ADDED
|
examples/fruits.jpg
ADDED
|
examples/shapes.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
opencv-python-headless
|
| 3 |
+
matplotlib
|