从Python(Pycuda gpuarray)到OpENCV(CV :: CUDA :: GPUMAT)的交换GPU数据,反之亦然(重复)

发布于 2025-01-27 16:17:02 字数 1629 浏览 2 评论 0原文

由于没有人回答

是否可以在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文