7
•处理单个图像
•处理一系列图像
•对一系列图像使用Dask进行并行延时处理
首先,我们需要创建GPU空间(GPU_frame)来保存图像(就像相框保存图片一样),然后才能将图像上传到GPU。
第1步:上传
|
<span class="hljs-keyword">import</span> cv2 <span class="hljs-keyword">as</span> cv gpu_frame = cv.cuda_GpuMat() |
下一步,用CPU将图像加载到内存中(截图),并将其.upload到gpu上(帧图像);
|
<span class="hljs-attribute">screenshot</span> = cv.imread(<span class="hljs-string">'media/drop.png'</span>) gpu_frame.upload(screenshot) |
我们可以开始了。
第2步:开始玩图像
OpenCV CUDA函数返回cv2.cuda_GpuMat(GPU矩阵),因此每个结果都可以在用户不必重新upload的情况下操作。
让我们把图像从RGB转换成BGR(OpenCV格式),然后调整大小:
|
<span class="hljs-attr">screenshot</span> = cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR) <span class="hljs-attr">screenshot</span> = cv.cuda.resize(screenshot, (<span class="hljs-number">400</span>, <span class="hljs-number">400</span>)) |
注意:你调用的函数的第一个参数应该是GPU矩阵(GPU帧),而不是你刚刚上传的图像。这将返回一个新的GPU矩阵。
原始的GPU矩阵(gpu_frame)将继续保存原始图像,直到新图像被upload。
第3步:下载
现在你可能会想:“图像呢?”
它被存放在GPU上了,我们需要把它下载回CPU:
|
<span class="hljs-selector-tag">screenshot</span><span class="hljs-selector-class">.download</span>() |
注意:download()将从cv转换为图像,从cuda_GpuMat到numpy.ndarray。
要处理新图片,只需用upload()将新图片加载到现有的GPU矩阵中。图像在传递给GPU之前同样须加载到CPU上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
<span class="hljs-attr">import</span> <span class="hljs-string">cv2 as cv</span> <span class="hljs-attr">img_files</span> = <span class="hljs-string">['bear.png', 'drip.png', 'tldr.png', 'frog.png']</span> <span class="hljs-attr">gpu_frame</span> = <span class="hljs-string">cv.cuda_GpuMat()</span> <span class="hljs-attr">for</span> <span class="hljs-string">i in range(len(img_files)):</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.imread(f"media/{img_files[i]}")</span> <span class="hljs-attr">gpu_frame.upload(screenshot)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.threshold(screenshot, 105, 255, cv.THRESH_BINARY_INV)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.resize(screenshot[1], (200, 200))</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">screenshot.download()</span> |
这一次我们在预处理中添加了一个反向的binary.threshold()。
使用Dask延时后,我们可以将上面的循环推入Dask延时函数,并行预处理多个系列的图像。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
<span class="hljs-attr">import</span> <span class="hljs-string">cv2 as cv</span> <span class="hljs-attr">import</span> <span class="hljs-string">dask.delayed</span> <span class="hljs-attr">@dask.delayed</span> <span class="hljs-attr">def</span> <span class="hljs-string">preprocess(files):</span> <span class="hljs-attr">i_files</span> = <span class="hljs-string">files.copy()</span> <span class="hljs-attr">gpu_frame</span> = <span class="hljs-string">cv.cuda_GpuMat()</span> <span class="hljs-attr">for</span> <span class="hljs-string">i in range(len(i_files)):</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.imread(f'media/{i_files[i]}')</span> <span class="hljs-attr">gpu_frame.upload(screenshot)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.cvtColor(screenshot, cv.COLOR_BGR2GRAY)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.threshold(screenshot, 125, 255, cv.THRESH_BINARY)</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">cv.cuda.resize(screenshot[1], (200, 200))</span> <span class="hljs-attr">screenshot</span> = <span class="hljs-string">screenshot.download()</span> <span class="hljs-meta">i_files[i]</span> = <span class="hljs-string">screenshot</span> <span class="hljs-attr">return</span> <span class="hljs-string">i_files</span> |
我还添加了第二个cvtColor()来灰度化图像,并将反转的二进制阈值切换为二进制阈值。
我们现在可以使用compute();
|
<span class="hljs-keyword">from</span> dask <span class="hljs-keyword">import</span> compute img_files = [<span class="hljs-string">'bear.png'</span>, <span class="hljs-string">'drip.png'</span>, <span class="hljs-string">'tldr.png'</span>, <span class="hljs-string">'frog.png'</span>] img_files_2 = [<span class="hljs-string">'apple.png'</span>, <span class="hljs-string">'eye.png'</span>, <span class="hljs-string">'window.png'</span>, <span class="hljs-string">'blinds.png'</span>] set_a = dask.delayed(preprocess)(img_files) set_b = dask.delayed(preprocess)(img_files_2) out_a, out_b = compute(*[set_a, set_b]) |
结果如下:
0
谢谢你的阅读。Github链接:https://github.com/Dropout-Analytics/opencv_cuda。
•https://medium.com/dropout-analytics/opencv-cuda-for-videos-f3dcf346e398•https://medium.com/dropout-analytics/pycuda-on-jetson-nano-7990decab299•https://medium.com/dropout-analytics/beginners-guide-to-knn-with-cuml-ddca099f9e9d
1.Koriukina, Valeriia. “Getting Started with OpenCV CUDA Module.” Learn OpenCV, Learnopencv.com, 15 Sept. 2020, learnopencv.com/getting-started-opencv-cuda-modul.2.McWhorter, Paul. “AI on the Jetson Nano LESSON 10: Installing OpenCV for Python 3.” Paul McWhorter — YouTube, Youtube.com/User/Mcwhorpj, 2 Nov. 2019, youtu.be/3QYayL5y2hk.3.Pulli, Kari; Baksheev, Anatoly; Kornyakov, Kirill; Eruhimov, Victor. “Realtime Computer Vision with OpenCV.” Realtime Computer Vision with OpenCV — ACM Queue, Association for Computing Machinery, 22 Apr. 2012, queue.acm.org/detail.cfm?id=2206309.
OpenCV CUDA简介https://m.toutiaocdn.com/i6907542473883648515/?app=news_article×tamp=1608552643&use_new_style=1&req_id=2020122120104201013109913355104877&group_id=6907542473883648515&tt_from=android_share&utm_medium=toutiao_android&utm_campaign=client_share