OpenCV  4.2.0
Open Source Computer Vision
G-API Drawing and composition functionality

Functions for in-graph drawing. More...


 Drawing primitives
 Drawing operations and functions

Detailed Description

Functions for in-graph drawing.

This is a Work in Progress functionality and APIs may change in the future releases.

G-API can do some in-graph drawing with a generic operations and a set of rendering primitives. In contrast with traditional OpenCV, in G-API user need to form a rendering list of primitives to draw. This list can be built manually or generated within a graph. This list is passed to special operations or functions where all primitives are interpreted and applied to the image.

For example, in a complex pipeline a list of detected objects can be translated in-graph to a list of cv::gapi::wip::draw::Rect primitives to highlight those with bounding boxes, or a list of detected faces can be translated in-graph to a list of cv::gapi::wip::draw::Mosaic primitives to hide sensitive content or protect privacy.

Like any other operations, rendering in G-API can be reimplemented by different backends. Currently only an OpenCV-based backend is available.

In addition to the graph-level operations, there are also regular (immediate) OpenCV-like functions are available – see cv::gapi::wip::draw::render(). These functions are just wrappers over regular G-API and build the rendering graphs on the fly, so take compilation arguments as parameters.

Currently this API is more machine-oriented than human-oriented. The main purpose is to translate a set of domain-specific objects to a list of primitives to draw. For example, in order to generate a picture like this:


Rendering list needs to be generated as follows:

#include <opencv2/imgproc.hpp> // cv::FONT*, cv::LINE*, cv::FILLED
#include <opencv2/highgui.hpp> // imwrite
#include <opencv2/gapi.hpp>
int main(int argc, char *argv[])
if (argc < 2) {
std::cerr << "Filename requried" << std::endl;
return 1;
const auto font = cv::FONT_HERSHEY_DUPLEX;
const auto blue = cv::Scalar{ 255, 0, 0}; // B/G/R
const auto green = cv::Scalar{ 0, 255, 0};
const auto coral = cv::Scalar{0x81,0x81,0xF1};
const auto white = cv::Scalar{ 255, 255, 255};
cv::Mat test(cv::Size(480, 160), CV_8UC3, white);
namespace draw = cv::gapi::wip::draw;
std::vector<draw::Prim> prims;
prims.emplace_back(draw::Circle{ // CIRCLE primitive
{400,72}, // Position (a cv::Point)
32, // Radius
coral, // Color
cv::FILLED, // Thickness/fill type
cv::LINE_8, // Line type
0 // Shift
prims.emplace_back(draw::Text{ // TEXT primitive
"Hello from G-API!", // Text
{64,96}, // Position (a cv::Point)
font, // Font
1.0, // Scale (size)
blue, // Color
2, // Thickness
cv::LINE_8, // Line type
false // Bottom left origin flag
prims.emplace_back(draw::Rect{ // RECTANGLE primitive
{16,48,400,72}, // Geometry (a cv::Rect)
green, // Color
2, // Thickness
cv::LINE_8, // Line type
0 // Shift
prims.emplace_back(draw::Mosaic{ // MOSAIC primitive
{320,96,128,32}, // Geometry (a cv::Rect)
16, // Cell size
0 // Decimation
draw::render(test, prims);
cv::imwrite(argv[1], test);
return 0;