• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Learn OpenCV

OpenCV, PyTorch, Keras, Tensorflow examples and tutorials

  • Home
  • Getting Started
    • Installation
    • PyTorch
    • Keras & Tensorflow
    • Resource Guide
  • Courses
    • Opencv Courses
    • CV4Faces (Old)
  • Resources
  • AI Consulting
  • About

OpenCV Transparent API

Satya Mallick
January 28, 2018 44 Comments
how-to OpenCV 3

January 28, 2018 By 44 Comments

A master wordsmith can tell a heart breaking story in just a few words.

For sale: baby shoes, never worn.

A great artist can do so much with so little! The same holds true for great programmers and engineers. They always seem to eek out that extra ounce of performance from their machines. This is what often differentiates a great product from a mediocre one and an exceptional programmer from a run of the mill coder. Such mastery appears magical, but dig a bit deeper and you will notice that the knowledge was available to everyone. Few chose to utilize it.

In this post we will unlock the most easy and probably the most important performance trick you can use in OpenCV 3. It is called the Transparent API ( T-api or TAPI ).

What is Transparent API ( T-API or TAPI ) ?

The Transparent API is an easy way to seamlessly add hardware acceleration to your OpenCV code with minimal change to existing code. You can make your code almost an order of magnitude faster by making a laughably small change.

Using Transparent API is super easy. You can get significant performance boost by changing ONE keyword.

Don’t trust me ? Here is an example of standard OpenCV code that does not utilize the transparent API. It reads an image, converts it to grayscale, applies Gaussian blur, and finally does Canny edge detection.

C++

#include "opencv2/opencv.hpp"
using namespace cv;

int main(int argc, char** argv)
{
    Mat img, gray;
    img = imread("image.jpg", IMREAD_COLOR);
    
    cvtColor(img, gray, COLOR_BGR2GRAY);
    GaussianBlur(gray, gray,Size(7, 7), 1.5);
    Canny(gray, gray, 0, 50);
    
    imshow("edges", gray);
    waitKey();
    return 0;
}

Python

import cv2

img = cv2.imread("image.jpg", cv2.IMREAD_COLOR)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 1.5)
gray = cv2.Canny(gray, 0, 50)

cv2.imshow("edges", gray)
cv2.waitKey();

Let’s see how the same code looks with Transparent API.

OpenCV Transparent API example

I have modified the code above slightly to utilize the Transparent API. The difference between the standard OpenCV code and one utilizing TAPI is highlighted below. Notice that all we had to do was to copy the Mat image to UMat ( Unified Matrix ) class and use standard OpenCV functions thereafter.
C++

#include "opencv2/opencv.hpp"
using namespace cv;

int main(int argc, char** argv)
{
    UMat img, gray;
    imread("image.jpg", IMREAD_COLOR).copyTo(img);
    
    cvtColor(img, gray, COLOR_BGR2GRAY);
    GaussianBlur(gray, gray,Size(7, 7), 1.5);
    Canny(gray, gray, 0, 50);
    
    imshow("edges", gray);
    waitKey();
    return 0;
}

Python

import cv2

img = cv2.UMat(cv2.imread("image.jpg", cv2.IMREAD_COLOR))
imgUMat = cv2.UMat(img)
gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 1.5)
gray = cv2.Canny(gray, 0, 50)

cv2.imshow("edges", gray)
cv2.waitKey();

On my Macbook Pro this small change makes the code run 5x faster.

Note: It makes sense to use the Transparent API only when you are doing a few expensive operations on the image. Otherwise the overhead of moving the image to the GPU dominates the timing.

Let us quickly summarize the steps needed to use transparent API

  1. Convert Mat to UMat. There are a couple of ways of doing this in C++.

    C++

    Mat mat = imread("image.jpg", IMREAD_COLOR); 
    // Copy Mat to UMat
    UMat umat; 
    mat.copyTo(umat);
    

    Alternatively, you can use getUMat

    Mat mat = imread("image.jpg", IMREAD_COLOR); 
    // Get umat from mat. 
    UMat umat = mat.getUMat( flag );
    

    Python

    mat = cv2.imread("image.jpg", cv2.IMREAD_COLOR)
    umat = cv2.UMat(mat)
    

    flag can take values ACCESS_READ, ACCESS_WRITE, ACCESS_RW and ACCESS_FAST. At this point it is not clear what ACCESS_FAST does, but I will update this post once I figure it out.

  2. Use standard OpenCV functions that you would use with Mat.
  3. If necessary, convert UMat back to Mat.. Most of the time you do not need to do this. Here is how you do it in case you need to.
    C++

    Mat mat = umat.getMat( flag );
    

    Python

    mat = cv2.UMat.get(umat)
    

    where umat is a UMat image. flag is the same as described
    above.

Now we know how to use the Transparent API. So what is under the hood that magically improves performance ? The answer is OpenCL. In the section below I briefly explain OpenCL.

What is Open Computing Language (OpenCL) ?

If you are reading this article on a laptop or a desktop computer, it has a graphics card ( either integrated or discrete ) connected to the CPU, which in turn has multiple cores. On the other hand, if you are reading this on a cell phone or tablet, your device probably has a CPU, a GPU, and a Digital Signal Processor ( DSP ). So you have multiple processing units that you can use. The fancy industry words for your computer or mobile device is “heterogeneous platform”.

OpenCL is a framework for writing programs that execute on these heterogenous platforms. The developers of an OpenCL library utilize all OpenCL compatible devices (CPUs, GPUs, DSPs, FPGAs etc) they find on a computer / device and assign the right tasks to the right processor. Keep in mind that as a user of OpenCV library you are not developing any OpenCL library. In fact you are not even a user of the OpenCL library because all the details are hidden behind the transparent API.

What is the difference between OCL Module and Transparent API ?

Short answer : The OCL module is dead. Long live the Transparent API!

OpenCL was supported in OpenCV 2.4 via the OCL module. There were a set of functions defined under the ocl namespace that you could use to call the underlying OpenCL code. Below is an example for reading an image, and using OpenCL to convert it to grayscale.

 
// Example for using OpenCL is OpenCV 2.4
// In OpenCV 3 the OCL module is gone. 
// It is replaced by the much nicer Transparent API

// Initialize OpenCL
std::vector<ocl::Info> param;
ocl::getDevice(param, ocl::CVCL_DEVICE_TYPE_GPU);

// Read image
Mat im = imread("image.jpg"); 

// Convert it to oclMat 
ocl::oclMat ocl_im(im);

// Container for OpenCL gray image. 
ocl::oclMat ocl_gray; 

// BGR2GRAY using OpenCL. 
cv::ocl::cvtColor( ocl_im, ocl_gray, CV_BGR2GRAY );

// Container for OpenCV Mat gray image. 
Mat gray; 

// Convert back to OpenCV Mat
ocl_gray.download(gray);

As you can see it was a lot more cumbersome. With OpenCV 3 the OCL module is gone! All this complexity is hidden behind the so-called transparent API and all you need to do is use UMat instead of Mat and the rest of the code remains unchanged. You just need to write the code once!

Subscribe

If you liked this article, please subscribe to our newsletter and receive a free
Computer Vision Resource guide. In addition to Computer Vision & Machine Learning news we share OpenCV tutorials and examples in C++/Python.

Subscribe Now

Tags: C++ hardware acceleration opencl Python t-api tapi transparent API

Filed Under: how-to, OpenCV 3

About

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field.

In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products. Read More…

Getting Started

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

Resources

Download Code (C++ / Python)

ENROLL IN OFFICIAL OPENCV COURSES

I've partnered with OpenCV.org to bring you official courses in Computer Vision, Machine Learning, and AI.
Learn More

Recent Posts

  • Making A Low-Cost Stereo Camera Using OpenCV
  • Optical Flow in OpenCV (C++/Python)
  • Introduction to Epipolar Geometry and Stereo Vision
  • Depth Estimation using Stereo matching
  • Classification with Localization: Convert any Keras Classifier to a Detector

Disclaimer

All views expressed on this site are my own and do not represent the opinions of OpenCV.org or any entity whatsoever with which I have been, am now, or will be affiliated.

GETTING STARTED

  • Installation
  • PyTorch
  • Keras & Tensorflow
  • Resource Guide

COURSES

  • Opencv Courses
  • CV4Faces (Old)

COPYRIGHT © 2020 - BIG VISION LLC

Privacy Policy | Terms & Conditions

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.AcceptPrivacy policy