{"id":986615,"date":"2024-12-27T07:46:28","date_gmt":"2024-12-26T23:46:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986615.html"},"modified":"2024-12-27T07:46:42","modified_gmt":"2024-12-26T23:46:42","slug":"python%e5%a6%82%e4%bd%95opencv-gpu%e5%8a%a0%e9%80%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/986615.html","title":{"rendered":"python\u5982\u4f55opencv gpu\u52a0\u901f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063127\/b72ef4d7-90fb-4e4b-83f0-e3d382b9bf86.webp\" alt=\"python\u5982\u4f55opencv gpu\u52a0\u901f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\u7684\u65b9\u5f0f\u6709\uff1a\u5b89\u88c5\u5e26\u6709CUDA\u652f\u6301\u7684OpenCV\u7248\u672c\u3001\u5229\u7528NVIDIA\u7684cuDNN\u5e93\u3001\u4f7f\u7528OpenCV\u4e2d\u7684GPU\u6a21\u5757\u3002<\/strong>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u5f0f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u7684\u5904\u7406\u901f\u5ea6\u3002\u9996\u5148\uff0c\u5b89\u88c5\u5e26\u6709CUDA\u652f\u6301\u7684OpenCV\u7248\u672c\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u8fd9\u9700\u8981\u4e00\u4e2a\u652f\u6301CUDA\u7684NVIDIA\u663e\u5361\uff0c\u5e76\u5b89\u88c5\u76f8\u5e94\u7684CUDA\u5de5\u5177\u5305\u548c\u9a71\u52a8\u3002\u63a5\u4e0b\u6765\uff0c\u5229\u7528NVIDIA\u7684cuDNN\u5e93\uff0c\u8fd9\u4e2a\u5e93\u4e13\u95e8\u7528\u4e8e\u52a0\u901f\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\uff0c\u901a\u8fc7\u4e0eCUDA\u7ed3\u5408\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002\u6700\u540e\uff0cOpenCV\u672c\u8eab\u7684GPU\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u52a0\u901f\u51fd\u6570\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u548c\u89c6\u9891\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5e26\u6709CUDA\u652f\u6301\u7684OpenCV\u7248\u672c<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e2a\u5e26\u6709CUDA\u652f\u6301\u7684OpenCV\u7248\u672c\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u4ece\u6e90\u4ee3\u7801\u7f16\u8bd1OpenCV\uff0c\u5e76\u542f\u7528CUDA\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51c6\u5907\u5de5\u4f5c<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86NVIDIA\u663e\u5361\uff0c\u5e76\u4e14\u5b89\u88c5\u4e86\u6b63\u786e\u7248\u672c\u7684NVIDIA\u9a71\u52a8\u3002<\/li>\n<li>\u4e0b\u8f7d\u5e76\u5b89\u88c5CUDA Toolkit\u548ccuDNN\u5e93\uff0c\u8fd9\u662f\u7528\u4e8e\u652f\u6301GPU\u52a0\u901f\u7684\u5fc5\u8981\u5de5\u5177\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f16\u8bd1OpenCV<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4eceOpenCV\u7684GitHub\u4ed3\u5e93\u4e0b\u8f7d\u6e90\u4ee3\u7801\u3002<\/li>\n<li>\u4f7f\u7528CMake\u914d\u7f6eOpenCV\u7f16\u8bd1\u9009\u9879\uff0c\u786e\u4fdd\u542f\u7528CUDA\u652f\u6301\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>WITH_CUDA=ON<\/code>\u9009\u9879\u6765\u5f00\u542fCUDA\u3002<\/li>\n<li>\u5728\u914d\u7f6e\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fddCMake\u53ef\u4ee5\u627e\u5230CUDA\u548ccuDNN\u7684\u8def\u5f84\u3002<\/li>\n<li>\u8fd0\u884c\u7f16\u8bd1\u547d\u4ee4\uff08\u4f8b\u5982<code>make<\/code>\uff09\u6765\u6784\u5efaOpenCV\u5e93\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5OpenCV<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u5b89\u88c5OpenCV\u5e93\u5230\u4f60\u7684Python\u73af\u5883\u4e2d\u3002<\/li>\n<li>\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cv2.getBuildInformation()<\/code>\u67e5\u770bOpenCV\u7684\u7f16\u8bd1\u4fe1\u606f\uff0c\u786e\u8ba4CUDA\u652f\u6301\u5df2\u542f\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5229\u7528NVIDIA\u7684cuDNN\u5e93<\/p>\n<\/p>\n<p><p>NVIDIA\u7684cuDNN\u5e93\u662f\u4e00\u4e2aGPU\u52a0\u901f\u7684\u6df1\u5ea6\u5b66\u4e60\u5e93\uff0c\u4e0eCUDA\u7ed3\u5408\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u7684\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528cuDNN\u5e93\u7684\u4e00\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7d\u548c\u5b89\u88c5cuDNN<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4eceNVIDIA\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7dcuDNN\u5e93\u3002\u6ce8\u610f\u9009\u62e9\u4e0e\u4f60\u7684CUDA\u7248\u672c\u517c\u5bb9\u7684cuDNN\u7248\u672c\u3002<\/li>\n<li>\u89e3\u538b\u7f29\u4e0b\u8f7d\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5e93\u6587\u4ef6\u590d\u5236\u5230CUDA\u7684\u5b89\u88c5\u76ee\u5f55\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4ee5\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u627e\u5230cuDNN\u5e93\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5c06cuDNN\u7684\u5e93\u8def\u5f84\u6dfb\u52a0\u5230<code>LD_LIBRARY_PATH<\/code>\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5728OpenCV\u4e2d\u4f7f\u7528cuDNN<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u7f16\u8bd1OpenCV\u65f6\uff0c\u786e\u4fddCMake\u80fd\u591f\u627e\u5230cuDNN\u7684\u8def\u5f84\u3002<\/li>\n<li>\u786e\u4fdd<code>opencv_dnn<\/code>\u6a21\u5757\u4e2d\u7684<code>DNN_BACKEND_CUDA<\/code>\u88ab\u542f\u7528\uff0c\u4ee5\u4fbf\u5229\u7528cuDNN\u8fdb\u884c\u52a0\u901f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528OpenCV\u4e2d\u7684GPU\u6a21\u5757<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e13\u95e8\u7528\u4e8eGPU\u52a0\u901f\u7684\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u7684\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684GPU\u6a21\u5757\u548c\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>CUDA\u6a21\u5757<\/strong><\/p>\n<\/p>\n<ul>\n<li>OpenCV\u7684CUDA\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>cv2.cuda_GpuMat<\/code>\u53ef\u4ee5\u7528\u4e8e\u5728GPU\u4e0a\u5b58\u50a8\u548c\u64cd\u4f5c\u56fe\u50cf\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528CUDA\u6a21\u5757\u65f6\uff0c\u9700\u8981\u5c06\u56fe\u50cf\u6570\u636e\u4e0a\u4f20\u5230GPU\uff0c\u5e76\u5728\u5904\u7406\u5b8c\u6210\u540e\u4e0b\u8f7d\u56deCPU\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>OpenCL\u6a21\u5757<\/strong><\/p>\n<\/p>\n<ul>\n<li>OpenCV\u4e5f\u652f\u6301OpenCL\uff0c\u53ef\u4ee5\u5229\u7528<code>cv2.UMat<\/code>\u6765\u81ea\u52a8\u7ba1\u7406\u6570\u636e\u5728CPU\u548cGPU\u4e4b\u95f4\u7684\u4f20\u8f93\u3002<\/li>\n<li>OpenCL\u6a21\u5757\u9002\u7528\u4e8e\u66f4\u5e7f\u6cdb\u7684\u786c\u4ef6\uff0c\u5305\u62ec\u652f\u6301OpenCL\u7684\u975eNVIDIA\u663e\u5361\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528GPU\u52a0\u901f\u65f6\uff0c\u5c3d\u91cf\u51cf\u5c11\u6570\u636e\u5728CPU\u548cGPU\u4e4b\u95f4\u7684\u4f20\u8f93\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<li>\u5728\u8bbe\u8ba1\u7b97\u6cd5\u65f6\uff0c\u5c3d\u91cf\u5c06\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u653e\u5728GPU\u4e0a\u6267\u884c\uff0c\u4ee5\u5145\u5206\u5229\u7528GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b9e\u7528\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\uff0c\u4e0b\u9762\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u5229\u7528OpenCV\u7684CUDA\u6a21\u5757\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u786e\u4fddOpenCV\u652f\u6301CUDA<\/strong><\/h2>\n<p>print(&quot;OpenCV version:&quot;, cv2.__version__)<\/p>\n<p>print(&quot;CUDA support:&quot;, cv2.cuda.getCudaEnabledDeviceCount() &gt; 0)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;image.jpg&#39;, cv2.IMREAD_COLOR)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u4e0a\u4f20\u5230GPU<\/strong><\/h2>\n<p>gpu_image = cv2.cuda_GpuMat()<\/p>\n<p>gpu_image.upload(image)<\/p>\n<h2><strong>\u5728GPU\u4e0a\u8fdb\u884c\u9ad8\u65af\u6a21\u7cca\u5904\u7406<\/strong><\/h2>\n<p>gpu_blurred = cv2.cuda.createGaussianFilter(gpu_image.type(), -1, (15, 15), 0)<\/p>\n<p>blurred_image = gpu_blurred.apply(gpu_image)<\/p>\n<h2><strong>\u5c06\u5904\u7406\u540e\u7684\u56fe\u50cf\u4e0b\u8f7d\u56deCPU<\/strong><\/h2>\n<p>result = blurred_image.download()<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Blurred Image&#39;, result)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5728GPU\u4e0a\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u4efb\u52a1\u9009\u62e9\u5408\u9002\u7684\u52a0\u901f\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u652f\u6301CUDA\u7684OpenCV\u7248\u672c\u3002\u53ef\u4ee5\u901a\u8fc7\u4ece\u6e90\u7801\u7f16\u8bd1OpenCV\u6765\u542f\u7528CUDA\u652f\u6301\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.cuda<\/code>\u6a21\u5757\u6765\u8c03\u7528GPU\u52a0\u901f\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u4f7f\u7528<code>cv2.cuda.GpuMat()<\/code>\u521b\u5efaGPU\u77e9\u9635\u5e76\u5c06\u6570\u636e\u4e0a\u4f20\u5230GPU\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528GPU\u52a0\u901f\u7684OpenCV\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>OpenCV\u63d0\u4f9b\u4e86\u591a\u79cdGPU\u52a0\u901f\u7684\u529f\u80fd\uff0c\u5305\u62ec\u56fe\u50cf\u5904\u7406\u3001\u7279\u5f81\u68c0\u6d4b\u3001\u5bf9\u8c61\u8ddf\u8e2a\u548c\u89c6\u9891\u5206\u6790\u7b49\u3002\u5e38\u7528\u7684GPU\u52a0\u901f\u51fd\u6570\u5305\u62ec\u56fe\u50cf\u6ee4\u6ce2\uff08\u5982\u9ad8\u65af\u6a21\u7cca\uff09\u3001\u8fb9\u7f18\u68c0\u6d4b\uff08\u5982Canny\u8fb9\u7f18\u68c0\u6d4b\uff09\u4ee5\u53ca\u4e00\u4e9b<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u7684\u63a8\u7406\u3002\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u9ad8\u5206\u8fa8\u7387\u56fe\u50cf\u6216\u89c6\u9891\u65f6\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528GPU\u52a0\u901f\u65f6\uff0c\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\uff1f<\/strong><br \/>\u4f7f\u7528GPU\u52a0\u901f\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u60a8\u7684\u8ba1\u7b97\u673a\u5177\u5907\u517c\u5bb9\u7684GPU\u548c\u6b63\u786e\u914d\u7f6e\u7684CUDA\u73af\u5883\u3002\u68c0\u67e5\u60a8\u7684\u663e\u5361\u662f\u5426\u652f\u6301CUDA\uff0c\u5e76\u786e\u4fdd\u5b89\u88c5\u4e86\u76f8\u5e94\u7248\u672c\u7684CUDA Toolkit\u548ccuDNN\u5e93\u3002\u6b64\u5916\uff0c\u4e0d\u540c\u7684\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u6027\u80fd\u8868\u73b0\uff0c\u56e0\u6b64\u5efa\u8bae\u5728\u4f7f\u7528GPU\u52a0\u901f\u524d\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\uff0c\u4ee5\u627e\u5230\u6700\u9002\u5408\u60a8\u5e94\u7528\u573a\u666f\u7684\u4f18\u5316\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528OpenCV\u8fdb\u884cGPU\u52a0\u901f\u7684\u65b9\u5f0f\u6709\uff1a\u5b89\u88c5\u5e26\u6709CUDA\u652f\u6301\u7684OpenCV\u7248\u672c\u3001\u5229\u7528NVIDI [&hellip;]","protected":false},"author":3,"featured_media":986637,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986615"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=986615"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986615\/revisions"}],"predecessor-version":[{"id":986640,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986615\/revisions\/986640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986637"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}