Welcome to K230 Vision Module repository
13. Edge Detection
Edge Detection
Edge DetectionPrefaceExample ResultsPrinciple ExplanationFirst and Second Order DerivativesCode OverviewInitialize Camera and Display ModuleSet Detection ModeConvert to GrayscaleExecute Edge or Gradient ComputationPackage Single-Channel Result and DisplayResource ReleaseParameter Adjustment Notes
Preface
This tutorial applies to firmware version: CanMV_K230_YAHBOOM_micropython_v1.8-13. You can download it from the Jianan website https://download.kendryte.com/developer/releases/canmv_k230_micropython/daily_build/

Example Results
Run the example code for this section 11.camera_edge_detect.py.
This section first converts the live color image to grayscale, then demonstrates Canny, Sobel, Scharr, and Laplacian edge or gradient operators. The output is displayed as a single-channel grayscale image.



Principle Explanation
First and Second Order Derivatives
Image edges correspond to locations where grayscale changes rapidly. Sobel and Scharr approximate first-order derivatives, detecting gradients in the x or y direction respectively. Laplacian computes second-order derivatives, which is more sensitive to details and noise.
Canny is a multi-stage algorithm that typically includes smoothing, gradient computation, non-maximum suppression, and double threshold linking, outputting relatively thin and continuous binary edges.
| Algorithm | Direction in Example | Feature |
|---|---|---|
| Canny | All directions | Double threshold linking, thin edges |
| Sobel | x direction | General first-order gradient |
| Scharr | x direction | Better rotational symmetry with small kernels |
| Laplacian | All directions | Second-order derivative, sensitive to noise |
Code Overview
Initialize Camera and Display Module
W, H = 640, 480sensor = Sensor(width=1280, height=960, fps=90)sensor.reset()sensor.set_framesize(width=W, height=H)sensor.set_pixformat(Sensor.RGB888)Display.init(Display.ST7701, width=640, height=480, to_ide=True)sensor.run()time.sleep(0.5)Set Detection Mode
xxxxxxxxxxmode = 0| mode | Algorithm | Source Parameters |
|---|---|---|
| 0 | Canny | Low threshold 80, high threshold 200, source default |
| 1 | Sobel | CV_8U, dx=1, dy=0 |
| 2 | Scharr | CV_8U, dx=1, dy=0 |
| 3 | Laplacian | CV_8U |
Convert to Grayscale
xxxxxxxxxximg = sensor.snapshot()img_np = img.to_numpy_ref()gray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)Edge detection usually does not need color information. Grayscale reduces data volume and unifies channel processing.
Execute Edge or Gradient Computation
xxxxxxxxxxif mode == 0: out = cv2.Canny(gray, 80, 200)elif mode == 1: out = cv2.Sobel(gray, cv2.CV_8U, 1, 0)elif mode == 2: out = cv2.Scharr(gray, cv2.CV_8U, 1, 0)else: out = cv2.Laplacian(gray, cv2.CV_8U)The current example directly outputs CV_8U. Negative gradients cannot be fully preserved in unsigned 8-bit output, so results are suitable for visual demonstration but not for direct precise gradient numerical analysis.
Package Single-Channel Result and Display
xxxxxxxxxximg_out = image.Image( W, H, image.GRAYSCALE, alloc=image.ALLOC_REF, data=out)Display.show_image(img_out)Resource Release
xxxxxxxxxxfinally: sensor.stop() Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100)Parameter Adjustment Notes
- Canny Double Threshold: The high threshold determines strong edges, and the low threshold is used to connect weak edges adjacent to strong edges. The low threshold is usually less than the high threshold.
- Pre-denoising: The current script does not have Gaussian preprocessing. When there are many noise edges, you can first add 3×3 or 5×5 Gaussian blur.
- Gradient Direction: Sobel/Scharr with dx=1, dy=0 computes changes in the x direction, highlighting edges close to vertical. Changing to dx=0, dy=1 allows observation of the other direction.
- Output Depth: When you need to preserve positive and negative gradients, use firmware-supported signed or floating-point depths, then take the absolute value and normalize for display.
- Laplacian Noise: Second-order derivatives amplify high-frequency noise, usually relying more on pre-smoothing.
- Performance: You can downsample when only doing detection. When you need to draw edge coordinates back onto the original image, you must synchronously scale the coordinates.