Welcome to K230 Vision Module repository
8. Hough Line Detection
Hough Line Detection
Hough Line DetectionPrefaceExample ResultsPrinciple ExplanationStandard Hough LineProbabilistic Hough Line SegmentCode OverviewInitialize Camera and Display ModuleSet Detection ModeDownsample and Extract EdgesStandard Hough LineProbabilistic Hough Line SegmentResource ReleaseParameter Adjustment Notes
Preface
This tutorial is applicable to firmware version: CanMV_K230_YAHBOOM_micropython_v1.8-13. Download from Jianan official website https://download.kendryte.com/developer/releases/canmv_k230_micropython/daily_build/

Example Results
Run the example code for this section 06.camera_hough_lines.py.
This section demonstrates standard Hough line and probabilistic Hough line segment detection on a 320x240 grayscale edge map, then maps the detection results back to the 640x480 original image for display.

Principle Explanation
Standard Hough Line
The standard Hough transform uses polar coordinates to represent lines:
x·cosθ + y·sinθ = ρEach edge point votes in the ρ-θ parameter space, and candidates exceeding the vote threshold are returned as lines. The return values are ρ and θ, which need to be converted to two endpoints on the image.
Probabilistic Hough Line Segment
HoughLinesP() directly returns (x1, y1, x2, y2) line segment endpoints, and limits results through minimum line length and maximum gap. It is more suitable for drawing finite line segments in actual images.
Code Overview
Initialize Camera and Display Module
xxxxxxxxxxW, 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 = 0mode=0: Standard HoughLines(), source code default.
mode=1: Probabilistic HoughLinesP().
Downsample and Extract Edges
xxxxxxxxxxsmall = cv2.resize(img_np, (320, 240))gray = cv2.cvtColor(small, cv2.COLOR_BGR2GRAY)edges = cv2.Canny(gray, 50, 150)Canny lower threshold is 50, upper threshold is 150. Hough transform only processes edge maps, not directly processing color images.
Standard Hough Line
xlines = cv2.HoughLines(edges, 1, np.pi / 180, 120)
if lines is not None: for i in range(lines.shape[0]): if i >= 30: break rho = lines[i, 0] theta = lines[i, 1] a = np.cos(theta) b = np.sin(theta) x0 = a * rho * 2 y0 = b * rho * 2 cv2.line( img_np, (int(x0 + 1000 * (-b)), int(y0 + 1000 * a)), (int(x0 - 1000 * (-b)), int(y0 - 1000 * a)), (0, 255, 0), 2 )Distance resolution is 1 pixel, angle resolution is 1 degree, and accumulator threshold is 120. x0, y0 multiplied by 2 maps half-resolution coordinates back to the original image.
Probabilistic Hough Line Segment
xxxxxxxxxxlines = cv2.HoughLinesP( edges, 1, np.pi / 180, 70, minLineLength=30, maxLineGap=8)
if lines is not None: for i in range(lines.shape[0]): if i >= 30: break cv2.line( img_np, (int(lines[i, 0]) * 2, int(lines[i, 1]) * 2), (int(lines[i, 2]) * 2, int(lines[i, 3]) * 2), (0, 0, 255), 2 )Minimum line length 30 and maximum gap 8 are both pixel values on the 320x240 image.
Resource Release
xxxxxxxxxxfinally: sensor.stop() Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100)Parameter Adjustment Notes
Canny Thresholds: Increase when edge noise is high, decrease when weak lines are missing. Stabilize the edge map first, then modify Hough parameters.
Standard Hough Threshold 120: Higher returns fewer but stronger lines.
Probabilistic Hough Threshold 70: Controls the minimum vote count for line segment candidates.
minLineLength: Filters short lines; actual length on original image is approximately twice the current value.
maxLineGap: Allows gaps on the same line; too large will connect unrelated line segments.
Result Upper Limit: Source code draws at most 30 lines, but printed lines.shape[0] may be greater than 30. Pay attention when reading serial data.
Scene Constraints: Use ROI or angle range filtering to suppress background strong lines such as table edges and screen borders.