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)