Access pixels in gpu::mat

南楼画角 提交于 2021-02-08 10:50:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!