Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn SimpleOCV

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tencent_Ncnn_SimpleOCV.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Computer Vision, Image Processing
Last Updated 2026-02-09 19:00 GMT

Overview

Provides a minimal OpenCV-compatible replacement library defining core cv:: namespace data structures (Mat, Scalar, Point, Size, Rect) and image I/O functions (imread, imwrite, imdecode, imencode, resize), allowing ncnn applications to work without linking against the full OpenCV library.

Description

The simpleocv module consists of a header (simpleocv.h, 492 lines) and an implementation (simpleocv.cpp, 483 lines), guarded by the NCNN_SIMPLEOCV compile flag.

The header defines template classes within the cv namespace that mirror the OpenCV API:

  • Scalar_<T>: A 4-element value type (typedef as cv::Scalar for uchar).
  • Point_<T>: An (x, y) coordinate type (typedef as cv::Point for int, cv::Point2f for float).
  • Size_<T>: A (width, height) dimension type (typedef as cv::Size for int).
  • Rect_<T>: An (x, y, width, height) rectangle type (typedef as cv::Rect for int).
  • cv::Mat: A reference-counted pixel data matrix supporting create(), clone(), resize(), region-of-interest via operator()(Rect), and properties like rows, cols, channels(), type(), empty(), and total(). Reference counting uses NCNN_XADD for atomic operations.

OpenCV-compatible enums are defined: IMREAD_UNCHANGED, IMREAD_GRAYSCALE, IMREAD_COLOR, CV_8UC1/3/4, and IMWRITE_JPEG_QUALITY.

The implementation uses stb_image (via STB_IMAGE_IMPLEMENTATION) for decoding and stb_image_write (via STB_IMAGE_WRITE_IMPLEMENTATION) for encoding. Format support is restricted to JPEG, PNG, BMP, and PNM via STBI_ONLY_* defines. imread performs RGB-to-BGR channel swapping to match OpenCV's default color ordering. imwrite reverses the conversion before writing. ARM NEON acceleration is enabled via STBI_NEON.

Drawing functions (rectangle, circle, line, putText, getTextSize) and image processing functions (resize, warpAffine, cvtColor) are also declared, delegating to ncnn's pixel drawing and transformation functions.

Usage

Use simpleocv when building ncnn examples or applications that need basic image loading, saving, and simple drawing without the heavy OpenCV dependency. Code can be written with familiar OpenCV-style syntax (cv::Mat, cv::imread, etc.) while keeping binary size minimal.

Code Reference

Source Location

Signature

namespace cv {

// Image I/O
Mat imread(const std::string& path, int flags = IMREAD_COLOR);
Mat imdecode(const std::vector<uchar>& buf, int flags = IMREAD_COLOR);
bool imwrite(const std::string& path, const Mat& m,
    const std::vector<int>& params = std::vector<int>());

// Image processing
void resize(const Mat& src, Mat& dst, const Size& size,
    float sw = 0.f, float sh = 0.f, int flags = 0);

// Drawing
void rectangle(Mat& img, Point pt1, Point pt2,
    const Scalar& color, int thickness = 1);
void rectangle(Mat& img, Rect rec,
    const Scalar& color, int thickness = 1);
void circle(Mat& img, Point center, int radius,
    const Scalar& color, int thickness = 1);
void line(Mat& img, Point p0, Point p1,
    const Scalar& color, int thickness = 1);
void putText(Mat& img, const std::string& text, Point org,
    int fontFace, double fontScale, Scalar color, int thickness = 1);
Size getTextSize(const std::string& text, int fontFace,
    double fontScale, int thickness, int* baseLine);

} // namespace cv

Import

#include "simpleocv.h"

I/O Contract

Inputs

Name Type Required Description
path const std::string& For imread/imwrite File path to image
flags int No Read mode: IMREAD_UNCHANGED (-1), IMREAD_GRAYSCALE (0), IMREAD_COLOR (1)
buf const std::vector<uchar>& For imdecode Memory buffer containing encoded image data
m / img cv::Mat For imwrite/drawing The image matrix
params const std::vector<int>& No Encoding parameters (e.g., JPEG quality)

Outputs

Name Type Description
return (imread) cv::Mat Decoded image in BGR format (matching OpenCV convention)
return (imwrite) bool True on success, false on failure
dst (resize) cv::Mat Resized output image

Usage Examples

Loading and Saving Images

#include "simpleocv.h"

// Load a color image (BGR format, matching OpenCV)
cv::Mat img = cv::imread("input.jpg", cv::IMREAD_COLOR);

// Resize for model input
cv::Mat resized;
cv::resize(img, resized, cv::Size(224, 224));

// Draw detection results
cv::rectangle(img, cv::Point(50, 50), cv::Point(150, 130),
    cv::Scalar(0, 255, 0), 2);
cv::putText(img, "person", cv::Point(50, 45),
    cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0));

// Save result
cv::imwrite("output.png", img);

Decoding from Memory Buffer

#include "simpleocv.h"

// Decode image from a memory buffer
std::vector<unsigned char> buffer = /* loaded from network or file */;
cv::Mat img = cv::imdecode(buffer, cv::IMREAD_COLOR);

Related Pages

Page Connections

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