问题
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.
回答1:
Information update
For those of you who see this, OpenCL for OpenCV python version has already been impemented
at 6 Oct 2016
More information
For more information, you can take a look at this issue: T-API python support implemented #6078
回答2:
According to this issue the support for this feature is still lacking currently but is "in progress", I will update when more becomes available.
回答3:
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
来源:https://stackoverflow.com/questions/31990646/using-opencl-accelerated-functions-with-opencv3-in-python