zOpenCV: High-performance Zig bindings for OpenCV 4.x β bridging the full power of OpenCV and opencv_contrib modules with Zig's zero-cost safety guarantees.
| Metric | Value |
|---|---|
| Version | 0.1.0 |
| Zig | 0.16.0-dev.2510+bcb5218a2 |
| OpenCV | 4.13.0 |
| API Functions | 651 |
| Tests | 560 passing β |
| Examples | 22 |
| Core Modules | 15 |
| Contrib Modules | 13 |
- β Type-safe β Idiomatic Zig API with compile-time type checking
- β
Memory-safe β RAII-style resource management with
defer - β Zero-cost β Direct C API calls with minimal overhead
- β Comprehensive β 651 functions across 28 modules
- β Well-tested β 560 unit and integration tests
- β Exception-safe β All C++ exceptions caught and mapped to Zig errors
- Zig 0.16.0-dev.2510+bcb5218a2
- OpenCV 4.13.0 with contrib modules
# macOS
brew install opencv
# Ubuntu/Debian
sudo apt-get install libopencv-dev
# Fedora
sudo dnf install opencv-develgit clone https://github.com/coderonion/zopencv
cd zopencv
zig build # Build library
zig build test # Run core tests
zig build test -Dcontrib=true # Run all tests (including contrib)
zig build examples -Dcontrib=true # Build all examples
zig build example-hello_opencv # Build a specific exampleconst cv = @import("zopencv");
pub fn main() !void {
// Read image
var img = try cv.imgcodecs.imread("input.jpg", .color);
defer img.deinit();
// Convert to grayscale
var gray = try cv.Mat.init();
defer gray.deinit();
cv.imgproc.cvtColor(img, &gray, .bgr2gray);
// Detect edges
var edges = try cv.Mat.init();
defer edges.deinit();
cv.imgproc.canny(gray, &edges, 50, 150, 3, false);
// Save result
try cv.imgcodecs.imwrite("edges.jpg", edges);
}Add zOpenCV as a dependency in your project β C wrapper compilation and OpenCV linking are handled automatically.
Local path (for development):
.dependencies = .{
.zopencv = .{
.path = "../zopencv",
},
},Git URL (for release):
.dependencies = .{
.zopencv = .{
.url = "https://github.com/coderonion/zopencv/archive/v0.1.0.tar.gz",
.hash = "HASH_VALUE",
},
},Tip
How to get the hash: First, add the .url field without .hash, then run zig build. Zig will download the package, compute the hash, and display the correct .hash = "..." value in the error output. Copy that value into your build.zig.zon.
Option A β Simple (hardcoded flags):
const zopencv = b.dependency("zopencv", .{
.target = target,
.optimize = optimize,
.contrib = true, // opencv_contrib modules (default: false)
});
exe.root_module.addImport("zopencv", zopencv.module("zopencv"));Option B β Dynamic (forward to CLI):
Expose the contrib flag as your project's build option, so users can toggle it at build time:
const enable_contrib = b.option(bool, "contrib", "Enable opencv_contrib modules") orelse false;
const zopencv = b.dependency("zopencv", .{
.target = target,
.optimize = optimize,
.contrib = enable_contrib,
});
exe.root_module.addImport("zopencv", zopencv.module("zopencv"));zig build # core modules only
zig build -Dcontrib=true # include opencv_contrib modulesconst cv = @import("zopencv");
pub fn main() !void {
var img = try cv.imgcodecs.imread("input.jpg", .color);
defer img.deinit();
// ...
}| Module | Functions | Description |
|---|---|---|
| core | 171 | Mat, Point, Rect, Size, Scalar, matrix operations |
| imgproc | 115 | Filters, edge detection, morphology, drawing, contours |
| features2d | 41 | Feature detection (ORB, SIFT, AKAZE), matching (BFMatcher) |
| photo | 39 | Denoising, inpainting, HDR, tone mapping, artistic effects |
| calib3d | 37 | Camera calibration, stereo vision, pose estimation |
| videoio | 29 | VideoCapture, VideoWriter, FourCC |
| video | 27 | Optical flow, background subtraction, tracking |
| dnn | 21 | Neural network inference (ONNX, TF, Caffe, Darknet, Torch, TFLite) |
| highgui | 19 | Windows, trackbars, mouse events, ROI selection |
| objdetect | 19 | CascadeClassifier, HOGDescriptor, QRCodeDetector |
| ml | 17 | SVM, KNearest classifiers |
| stitching | 13 | Image stitching / panorama |
| imgcodecs | 9 | imread, imwrite, imencode, imdecode, multi-page I/O |
| flann | 5 | Fast nearest neighbor search (KD-tree, K-means) |
| Module | Functions | Description |
|---|---|---|
| xphoto | 10 | White balance, oil painting, inpainting |
| ximgproc | 9 | Thinning, Niblack threshold, extended morphology |
| img_hash | 9 | Perceptual hashing (PHash, AverageHash, MarrHildreth) |
| face | 8 | LBPH face recognition |
| bgsegm | 8 | Background segmentation (CNT, GMG) |
| text | 7 | OCR via Tesseract, text detection (SWT) |
| dnn_superres | 7 | DNN-based super resolution |
| xfeatures2d | 6 | SURF descriptors, BRIEF |
| tracking | 6 | KCF object tracking |
| bioinspired | 6 | Bio-inspired retina model |
| aruco | 6 | ArUco marker detection and generation |
| saliency | 4 | Saliency detection (SpectralResidual, FineGrained) |
| optflow | 3 | Dense optical flow (DualTVL1) |
22 working examples in the examples/ directory. See examples/README.md for the full categorized index.
# Build and run
zig build example-hello_opencv && ./zig-out/bin/hello_opencv
zig build example-edge_detection && ./zig-out/bin/edge_detection
zig build example-face_detection && ./zig-out/bin/face_detection| Category | Examples |
|---|---|
| Getting Started | hello_opencv, image_io |
| Image Processing | edge_detection, threshold, morphology, histogram, contours, drawing, color_spaces, template_matching |
| Features & Matching | feature_matching, realtime_features |
| Object Detection | face_detection |
| Video | camera_capture, video_capture, video_writer, optical_flow, background_subtraction |
| Contrib | aruco_demo, img_hash_demo, photo_demo |
Comprehensive documentation is available in the docs/ directory:
- Documentation Index β Full navigation guide
- API Reference β Complete Zig β OpenCV cross-reference table (651 functions)
- Core Module Docs β Mat, basic types, matrix operations
- Image Processing Docs β Filters, transforms, drawing, contours
- Contrib Module Docs β All 13 contrib module references
Each module has its own detailed README in docs/<module>/README.md.
zig build test # Core module tests
zig build test -Dcontrib=true # All tests (560 tests, including contrib)
zig build test-unit # Unit tests only (fast, no I/O)
zig build test-integration # Integration tests (uses real images/video)
zig build test-contrib # Contrib module tests onlyTest coverage includes:
- Unit tests β Each module's core functionality, error handling, edge cases
- Integration tests β Multi-module workflows with real images and video
- Memory safety β Create/deinit cycles, empty Mat handling, zero-size operations
zopencv/
βββ src/ # Zig API layer (651 public functions)
β βββ main.zig # Root module β re-exports all modules
β βββ core.zig # Mat, Point, Rect, Size, Scalar (171 fns)
β βββ imgproc.zig # Image processing (115 fns)
β βββ contrib/ # 13 contrib module bindings
β βββ ... # 12 more module files
βββ c_api/ # C++ wrapper layer (C ABI bridge)
β βββ core.h / core.cpp
β βββ contrib/ # Contrib C wrappers
β βββ ...
βββ examples/ # 22 working examples
βββ test/ # 560 tests
β βββ unit/ # Per-module unit tests
β βββ integration/ # Multi-module integration tests
βββ docs/ # Comprehensive API documentation
βββ build.zig # Build configuration
βββ build.zig.zon # Package manifest
Data Flow: Zig API β C API (extern "C") β OpenCV C++ β Back through same path
All fallible functions return Zig error unions:
pub const Error = error{
NullPointer, // C API returned NULL (failed load, allocation, etc.)
MatCreationFailed, // Mat allocation failed
MatIsEmpty, // Operation on empty Mat
InvalidMatType, // Wrong Mat type for operation
InvalidIndex, // Index out of bounds
ImWriteFailed, // Image write failed
};All C++ exceptions from OpenCV are caught in the C API layer and converted to NULL returns, which the Zig layer maps to error.NullPointer.
- β Star and Fork this repository
- Create a feature branch (
git checkout -b feature/new-module) - Implement C++ wrapper in
c_api/ - Create Zig API bindings in
src/ - Add unit tests in
test/unit/and integration tests intest/integration/ - Create an example in
examples/ - Update documentation in
docs/ - Submit a Pull Request
MIT License
Built with gratitude on the shoulders of giants: