Manually download cv::cuda::GpuMat to cv::Mat using cudaMemcpyAsync

Hi,

As the title suggests, I’m trying to download a cv::cuda::GpuMat gpuMat to cv::Mat cpuMat by calling cudaMemcpyAsync.

cv::Mat cpuMat(gpuMat.size(), CV_8UC3);
checkCudaErrorCode(cudaMemcpyAsync(
                            cpuMat,
                            gpuMat.cudaPtr(),
                            gpuMat.cols * gpuMat.rows * gpuMat.channels() * CV_ELEM_SIZE1(gpuMat.type()),
                            cudaMemcpyDeviceToHost,
                             stream
 ), __func__);
checkCudaErrorCode(cudaStreamSynchronize(stream), __func__);

But then when try write cpuMat to file using cv::imwrite, it turns out gibberish.

I check if gpuMat’s data is corrupted with the conventional and there doesn’t seem to be a problem.

gpuMat.download(cpuMat)

Do you have any idea?
Thanks in advance.

GpuMat’s are allocated with cudaMallocPitch for efficient memory coalescing.

bool DefaultAllocator::allocate(GpuMat* mat, int rows, int cols, size_t elemSize)

To copy to the host you need to use cudaMemcpy2D passing in the step of the GpuMat for the pitch.

void cv::cuda::GpuMat::download(OutputArray _dst) const