Overview
G-API (Graph API) is OpenCV’s graph-based framework for building efficient, portable image processing pipelines. It provides:- Lazy evaluation: Build computation graph, execute later
- Backend abstraction: CPU, GPU, neural network accelerators
- Performance optimization: Automatic fusion and optimization
- Heterogeneous execution: Mix different backends
Key Concepts
Computation Graph
G-API separates graph construction from execution:- Build graph - Define operations
- Compile - Optimize for target backend
- Execute - Run on actual data
Data Types
- GMat: Graph matrix (image/matrix)
- GScalar: Graph scalar value
- GArray: Graph array of values
- GOpaque: Graph opaque type
- GFrame: Graph video frame
Basic Example
Available Operations
Image Processing
Core Operations
Computation
GComputation Class
Compilation
Backends
CPU Backend (Default)
OpenCL Backend
Fluid Backend (Cache-Efficient)
Heterogeneous Execution
Streaming Mode
Video Processing
Camera Processing
Custom Operations
Define Custom Kernel
Performance Optimization
Operation Fusion
G-API automatically fuses operations:Memory Optimization
Complete Example: Edge Detection Pipeline
Best Practices
Build Once, Run Many
Compile graph once, execute on multiple inputs
Choose Right Backend
Use Fluid for cache-efficiency, OpenCL for GPU
Use Streaming
Streaming mode for video processing
Custom Kernels
Implement custom operations when needed
Advantages Over Traditional API
| Feature | Traditional API | G-API |
|---|---|---|
| Optimization | Manual | Automatic |
| Portability | Backend-specific | Backend-agnostic |
| Efficiency | Per-operation | Graph-level |
| Memory | Allocates intermediate buffers | Optimizes memory |
See Also
- Core Module - Basic operations
- ImgProc Module - Image processing
- G-API Tutorial
