从Python(Pycuda gpuarray)到OpENCV(CV :: CUDA :: GPUMAT)的交换GPU数据,反之亦然(重复)
由于没有人回答
是否可以在Pycuda和OpenCV CUDA模块之间交换数据? Pycuda拥有自己的pycuda gpuarray类,OpenCV有自己的gpu_mat。
该计划是在图像(例如现在仅将其倒置)对图像执行某种动作,将其保留在GPU上,然后使用OpenCV执行Charny。
import numpy
import time
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
from pycuda.compiler import SourceModule
import pycuda.gpuarray as gpuarray
import cv2
mod = SourceModule("""
__global__ void multiply_them(uchar3 *dest, uchar3* img, int row, int col)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
int j = blockIdx.y * blockDim.y + threadIdx.y;
if (j >= row || i >= col) { return; }
dest[j * col + i].x = 255 - img[j * col + i].x;
dest[j * col + i].y = 255 - img[j * col + i].y;
dest[j * col + i].z = 255 - img[j * col + i].z;
}
""")
img = cv2.imread("./colorful_image.jpg", 0)
dest = numpy.zeros_like(img)
col = np.int32(img.shape[1])
row = np.int32(img.shape[0])
start = time.perf_counter()
multiply_them = mod.get_function("multiply_them")
img_gpu = gpuarray.to_gpu(img.astype(numpy.uint8))
dest_gpu = gpuarray.to_gpu(dest.astype(numpy.uint8))
block_size = (32,32,1)
grid_size = (int(col/block_size[0] + 1), int(row/block_size[1] + 1),1)
multiply_them(dest_gpu, img_gpu, row, col, block=block_size, grid=grid_size)
# dest_gpu = dest_gpu.get() # If we download to CPU it work fine but we dont want that.
# dest_gpu = cv2.cuda_GpuMat(dest_gpu)
cannyFilter = cv2.cuda.createCannyEdgeDetector(50, 120)
gpu_img_canny = cannyFilter.detect(dest_gpu)
b = gpu_img_canny.download()
cv2.imwrite("./slika_canny.jpg", b)
stop = time.perf_counter()
print("Time: ", stop-start)
Since nobody answered this question I'm trying again.
Is it possible to exchange data between Pycuda and OpenCV Cuda module?
Pycuda has its own class Pycuda GPUArray and OpenCV has its own Gpu_Mat.
The plan is to perform some kind of action on the image (for example now only to invert it) on Pycuda, keep it on GPU, and then perform Canny with OpenCV.
import numpy
import time
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
from pycuda.compiler import SourceModule
import pycuda.gpuarray as gpuarray
import cv2
mod = SourceModule("""
__global__ void multiply_them(uchar3 *dest, uchar3* img, int row, int col)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
int j = blockIdx.y * blockDim.y + threadIdx.y;
if (j >= row || i >= col) { return; }
dest[j * col + i].x = 255 - img[j * col + i].x;
dest[j * col + i].y = 255 - img[j * col + i].y;
dest[j * col + i].z = 255 - img[j * col + i].z;
}
""")
img = cv2.imread("./colorful_image.jpg", 0)
dest = numpy.zeros_like(img)
col = np.int32(img.shape[1])
row = np.int32(img.shape[0])
start = time.perf_counter()
multiply_them = mod.get_function("multiply_them")
img_gpu = gpuarray.to_gpu(img.astype(numpy.uint8))
dest_gpu = gpuarray.to_gpu(dest.astype(numpy.uint8))
block_size = (32,32,1)
grid_size = (int(col/block_size[0] + 1), int(row/block_size[1] + 1),1)
multiply_them(dest_gpu, img_gpu, row, col, block=block_size, grid=grid_size)
# dest_gpu = dest_gpu.get() # If we download to CPU it work fine but we dont want that.
# dest_gpu = cv2.cuda_GpuMat(dest_gpu)
cannyFilter = cv2.cuda.createCannyEdgeDetector(50, 120)
gpu_img_canny = cannyFilter.detect(dest_gpu)
b = gpu_img_canny.download()
cv2.imwrite("./slika_canny.jpg", b)
stop = time.perf_counter()
print("Time: ", stop-start)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论