Artificial Intelligence 8 min read

Master Drawing with OpenCV in Rust: Lines, Shapes, and Text Made Easy

This tutorial walks you through using OpenCV's Rust bindings to draw lines, geometric shapes, and custom text on images, detailing each function's parameters and providing complete, ready‑to‑run code examples for practical image processing tasks.

Architecture Development Notes
Architecture Development Notes
Architecture Development Notes
Master Drawing with OpenCV in Rust: Lines, Shapes, and Text Made Easy

Drawing Lines

OpenCV's imgproc module provides the line function. It requires the image, start point, end point, color (as Scalar ), thickness, line type, and shift. Example code shows how to draw a thin blue line, a thick purple line, and a thick 8‑connected red line.

<code>use opencv::{
    core::{Point, Scalar, Size_},
    highgui::{imshow, wait_key},
    imgproc::{arrowed_line, draw_marker, line, LINE_4, LINE_8, LINE_AA, MARKER_CROSS, MARKER_DIAMOND, MARKER_SQUARE, MARKER_STAR, MARKER_TILTED_CROSS, MARKER_TRIANGLE_DOWN, MARKER_TRIANGLE_UP},
    prelude::*,
    Result,
};

fn main() -> Result<()> {
    let mut img = Mat::new_rows_cols_with_default(400, 400, CV_8UC3, Scalar::from((255.0, 255.0, 255.0)))?;

    // Blue thin line
    line(&mut img, Point::new(50, 50), Point::new(200, 50), Scalar::from((0.0, 0.0, 255.0)), 1, LINE_AA, 0)?;

    // Purple thick line
    line(&mut img, Point::new(50, 100), Point::new(200, 100), Scalar::from((255.0, 0.0, 255.0)), 3, LINE_AA, 0)?;

    // Red thick 8‑connected line
    line(&mut img, Point::new(50, 150), Point::new(200, 150), Scalar::from((255.0, 0.0, 0.0)), 10, LINE_8, 0)?;

    imshow("Line", &img)?;
    wait_key(0)?;
    Ok(())
}
</code>

Drawing Shapes

The library also offers rectangle , circle , ellipse , and polylines for rectangles, circles, ellipses, and polygons. Each function takes the target image, geometric parameters, color, thickness, line type, and optional shift. Sample code demonstrates drawing a blue rectangle, a filled red circle, a green ellipse, and a purple polygon.

<code>use opencv::{
    core::{Point, Rect, Scalar, Size_},
    highgui::{imshow, wait_key},
    imgproc::{circle, ellipse, line, polylines, rectangle, LINE_AA},
    prelude::*,
    Result,
};

fn main() -> Result<()> {
    let mut img = Mat::new_rows_cols_with_default(400, 400, CV_8UC3, Scalar::from((255.0, 255.0, 255.0)))?;

    // Blue rectangle
    rectangle(&mut img, Rect::new(50, 50, 100, 50), Scalar::from((0.0, 0.0, 255.0)), 2, LINE_AA, 0)?;

    // Red filled circle
    circle(&mut img, Point::new(200, 100), 30, Scalar::from((255.0, 0.0, 0.0)), -1, LINE_AA, 0)?;

    // Green ellipse
    ellipse(&mut img, Point::new(300, 200), Size_::new(60, 30), 20.0, 0.0, 360.0, Scalar::from((0.0, 255.0, 0.0)), 2, LINE_AA, 0)?;

    // Purple polygon
    let mut pts = vec![
        Point::new(100, 250),
        Point::new(150, 300),
        Point::new(200, 250),
        Point::new(150, 200),
    ];
    polylines(&mut img, &pts, true, Scalar::from((255.0, 0.0, 255.0)), 2, LINE_AA, 0)?;

    imshow("Shapes", &img)?;
    wait_key(0)?;
    Ok(())
}
</code>

Adding Text

The put_text function inserts text onto an image. Parameters include the image, text string, origin point, font face, font scale, color, thickness, line type, and a flag for bottom‑left origin. Example code adds “Hello, OpenCV!” and “Welcome to Rust” with different fonts and colors.

<code>use opencv::{
    core::{Point, Scalar},
    highgui::{imshow, wait_key},
    imgproc::{get_text_size, put_text, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_SIMPLEX, LINE_AA},
    prelude::*,
    Result,
};

fn main() -> Result<()> {
    let mut img = Mat::new_rows_cols_with_default(400, 400, CV_8UC3, Scalar::from((255.0, 255.0, 255.0)))?;

    put_text(&mut img, "Hello, OpenCV!", Point::new(50, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar::from((0.0, 0.0, 255.0)), 2, LINE_AA, false)?;
    put_text(&mut img, "Welcome to Rust", Point::new(50, 100), FONT_HERSHEY_PLAIN, 2.0, Scalar::from((255.0, 0.0, 0.0)), 3, LINE_AA, false)?;

    imshow("Text", &img)?;
    wait_key(0)?;
    Ok(())
}
</code>

Summary

This article explains how to use OpenCV in Rust for drawing lines, shapes, and text, covering the relevant functions, their parameters, and providing complete, runnable examples that can be adapted for various image‑processing tasks.

computer visionImage ProcessingRustopencvdrawing
Architecture Development Notes
Written by

Architecture Development Notes

Focused on architecture design, technology trend analysis, and practical development experience sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.