Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Tencent Ncnn Draw Rectangle And Text

From Leeroopedia


Knowledge Sources
Domains Computer_Vision, Visualization
Last Updated 2026-02-09 00:00 GMT

Overview

Concrete tool for rendering bounding boxes and text labels onto pixel buffers provided by ncnn's built-in drawing functions.

Description

ncnn provides built-in pixel drawing functions that require no external dependencies (no OpenCV needed). The draw_rectangle_c3 function renders a rectangle outline or filled rectangle on a 3-channel (RGB/BGR) pixel buffer. The draw_text_c3 function renders text using a built-in bitmap font. Variants exist for 1, 2, 3, and 4 channel images, with optional stride parameter for non-contiguous memory layouts.

These functions operate directly on unsigned char pixel buffers, making them suitable for embedded platforms and applications where OpenCV is not available. Conditional compilation via NCNN_PIXEL_DRAWING controls availability.

Usage

Use for rendering detection results when OpenCV is not available. For desktop applications with OpenCV, cv::rectangle and cv::putText provide richer drawing features.

Code Reference

Source Location

  • Repository: ncnn
  • File: src/mat_pixel_drawing.cpp
  • Lines: L35-42 (draw_rectangle_c3 short form), L314-455 (draw_rectangle_c3 with stride), L1240-1242 (draw_text_c3 short form), L1795-1857 (draw_text_c3 with stride)

Signature

namespace ncnn {

// Draw rectangle on 3-channel pixel buffer
void draw_rectangle_c3(
    unsigned char* pixels,  // pixel buffer (RGB or BGR)
    int w, int h,           // image dimensions
    int rx, int ry,         // rectangle top-left corner
    int rw, int rh,         // rectangle width and height
    unsigned int color,     // packed color (0xRRGGBB)
    int thickness           // line thickness (-1 for filled)
);

// With stride parameter (bytes per row)
void draw_rectangle_c3(
    unsigned char* pixels,
    int w, int h, int stride,
    int rx, int ry, int rw, int rh,
    unsigned int color, int thickness
);

// Draw text on 3-channel pixel buffer
void draw_text_c3(
    unsigned char* pixels,
    int w, int h,
    const char* text,       // text string to render
    int x, int y,           // text position
    int fontpixelsize,      // font size in pixels
    unsigned int color      // text color
);

// With stride parameter
void draw_text_c3(
    unsigned char* pixels,
    int w, int h, int stride,
    const char* text,
    int x, int y,
    int fontpixelsize,
    unsigned int color
);

} // namespace ncnn

Import

#include "mat.h"
// Functions are in ncnn namespace
// Requires NCNN_PIXEL_DRAWING to be enabled at build time

I/O Contract

Inputs

Name Type Required Description
pixels unsigned char* Yes Mutable pixel buffer (3-channel)
w, h int Yes Image dimensions
rx, ry, rw, rh int Yes Rectangle position and size
text const char* Yes (for draw_text) Label string to render
color unsigned int Yes Packed color value
thickness int Yes (for draw_rectangle) Line width or -1 for filled
fontpixelsize int Yes (for draw_text) Font size in pixels

Outputs

Name Type Description
pixels (modified) unsigned char* Input buffer with drawings rendered in-place

Usage Examples

ncnn Built-in Drawing

#include "mat.h"

// Assuming 'pixels' is a 3-channel BGR pixel buffer
unsigned char* pixels = image.data;
int w = image.cols;
int h = image.rows;

for (int i = 0; i < objects.size(); i++)
{
    const Object& obj = objects[i];

    // Draw bounding box
    ncnn::draw_rectangle_c3(pixels, w, h,
        obj.rect.x, obj.rect.y,
        obj.rect.width, obj.rect.height,
        0x00FF00,  // green
        2);        // thickness

    // Draw label
    char text[256];
    sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
    ncnn::draw_text_c3(pixels, w, h,
        text,
        obj.rect.x, obj.rect.y - 12,
        12,         // font size
        0x00FF00);  // green
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment