Skip to main content

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:
  1. Build graph - Define operations
  2. Compile - Optimize for target backend
  3. 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

FeatureTraditional APIG-API
OptimizationManualAutomatic
PortabilityBackend-specificBackend-agnostic
EfficiencyPer-operationGraph-level
MemoryAllocates intermediate buffersOptimizes memory

See Also