OpenCV使用CUDA处理图像的教程与实战
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达
import cv2 as cv
gpu_frame = cv.cuda_GpuMat()
screenshot = cv.imread('media/drip.png')
gpu_frame.upload(screenshot)
gpu_frame.download()
在单张图像上使用
在多张图像上使用
对多张图像使用Dask进行并行延时处理
第1步:上传
import cv2 as cv
gpu_frame = cv.cuda_GpuMat()
screenshot = cv.imread('media/drop.png')
gpu_frame.upload(screenshot)
第2步:处理图像
screenshot = cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR)
screenshot = cv.cuda.resize(screenshot, (400, 400))
第3步:下载
screenshot.download()
import cv2 as cv
img_files = ['bear.png', 'drip.png', 'tldr.png', 'frog.png']
# 创建帧来保存图片(cv2.cuda_GpuMat)
gpu_frame = cv.cuda_GpuMat()
for i in range(len(img_files)):
# 加载图像(CPU)
screenshot = cv.imread(f"media/{img_files[i]}")
# 上传到GPU
gpu_frame.upload(screenshot)
# 转换颜色到opencv (numpy) ndarray→cv2.cuda_GpuMat
screenshot = cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR)
# 反向阈值@ 100
screenshot = cv.cuda.threshold(screenshot, 105, 255, cv.THRESH_BINARY_INV)
# 调整图像
screenshot = cv.cuda.resize(screenshot[1], (200, 200))
# 从GPU下载图像(cv2) cuda_GpuMat→numpy.ndarray
screenshot = screenshot.download()
import cv2 as cv
import dask.delayed
@dask.delayed
def preprocess(files):
# 复制图像文件
i_files = files.copy()
# 创建GPU帧来保存图像
gpu_frame = cv.cuda_GpuMat()
for i in range(len(i_files)):
# 加载图像(CPU)
screenshot = cv.imread(f'media/{i_files[i]}')
# 上传到GPU
gpu_frame.upload(screenshot)
# 转换颜色到opencv (numpy) ndarray→cv2.cuda_GpuMat
screenshot = cv.cuda.cvtColor(gpu_frame, cv.COLOR_RGB2BGR)
screenshot = cv.cuda.cvtColor(screenshot, cv.COLOR_BGR2GRAY)
# 反向阈值@ 100
screenshot = cv.cuda.threshold(screenshot, 125, 255, cv.THRESH_BINARY)
# 调整图像
screenshot = cv.cuda.resize(screenshot[1], (200, 200))
# 从GPU下载图像 (cv2.cuda_GpuMat -> numpy.ndarray)
screenshot = screenshot.download()
# 用新图像
i_files[i] = screenshot
# 输出预处理图像
return i_files
from dask import compute
img_files = ['bear.png', 'drip.png', 'tldr.png', 'frog.png']
img_files_2 = ['apple.png', 'eye.png', 'window.png', 'blinds.png']
# 设置延迟
set_a = dask.delayed(preprocess)(img_files)
set_b = dask.delayed(preprocess)(img_files_2)
# 开始计算
out_a, out_b = compute(*[set_a, set_b])
进阶阅读:
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
参考引用
Koriukina, Valeriia. “Getting Started with OpenCV CUDA Module.” Learn OpenCV, Learnopencv.com, 15 Sept. 2020, learnopencv.com/getting-started-opencv-cuda-modul. 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. 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.
交流群
欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~
评论