Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using OpenCL accelerated functions with OpenCV3 in Python

OpenCV3 introduced its T-API (Transparent API) which gives the user the possibility to use functions which are GPU (or other OpenCL enabled device) accelerated, I'm struggling to find how to tap into that with Python.

With C++ there are calls like ocl::setUseOpenCL(true); that enable OpenCL acceleration when you use UMat instead of Mat objects. However I found no documentation whatsoever for Python.

Does anybody have any sample code, links or guides on how to achieve OpenCL acceleration with OpenCV3 in Python?

UPDATE:

After some further digging I've found this in modules/core/include/opencv2/core/ocl.hpp:

CV_EXPORTS_W bool haveOpenCL();
CV_EXPORTS_W bool useOpenCL();
CV_EXPORTS_W bool haveAmdBlas();
CV_EXPORTS_W bool haveAmdFft();
CV_EXPORTS_W void setUseOpenCL(bool flag);
CV_EXPORTS_W void finish();

Which I managed to call from Python:

print(cv2.ocl.haveOpenCL())
cv2.ocl.setUseOpenCL(True)
print(cv2.ocl.useOpenCL())

And it produces the following output:

True
True

However it still runs the same, I suppose I'm still not using OpenCL because I don't use UMat in Python.

like image 957
Arqu Avatar asked Aug 13 '15 14:08

Arqu


1 Answers

The Transparent API is supported in OpenCV 3.2 and above. Here is an example code.

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();

More details can be found at OpenCV Transparent API

like image 147
Satya Mallick Avatar answered Oct 17 '22 03:10

Satya Mallick