问题
I'd like to know how to access pixel information when using OpenCV GPU. I'm currently downloading gpu::mat information to a mat variable but it is too slow. Does anyone know how to do it?
回答1:
You could access to the data inside a kernel.
For (row,col) the row and column number, the value of a pixel with channel number ch < 3 will be:
uint8_t val = gpumat.data[ (row*gpumat.step) + col*gpumat.channels() + ch];
So let say you have an BGR input image stored on a GpuMat called src and you want to assign each pixel value to a destination image called dst (also GpuMat). You could call the kernel
kernel_assign_pixel<<<gridDim, blockDim>>>
(src.data, dst.data, src.rows, src.cols, src.step, dst.step, src.channels());
Defined in the following way
__global__ void kernel_assign_pixel
(uint8_t* src, uint8_t* dst, int MaxRows, int MaxCols, int iStep, int oStep, int MaxC)
{
unsigned int row = blockIdx.x * blockDim.x + threadIdx.x; //Row number
unsigned int col = blockIdx.y * blockDim.y + threadIdx.y; //Column number
unsigned int ch = blockIdx.z * blockDim.z + threadIdx.z; //Channel number
if (row<MaxRows && col<MaxCols && ch < MaxC)
{
int tidIn = row * iStep + col * MaxC + ch;
int tidOut = row * oStep + col * MaxC + ch;
dst[tidOut]=src[tidIn];
}
}
来源:https://stackoverflow.com/questions/23372262/access-pixels-in-gpumat