colored image to greyscale image using CUDA parallel processing

前端 未结 12 1228
失恋的感觉
失恋的感觉 2021-02-04 19:10

I am trying to solve a problem in which i am supposed to change a colour image to a greyscale image. For this purpose i am using CUDA parallel approach.

The kerne code i

12条回答
  •  [愿得一人]
    2021-02-04 19:53

    __global__
    void rgba_to_greyscale(const uchar4* const rgbaImage,
                           unsigned char* const greyImage,
                           int numRows, int numCols)
    {
        int rgba_x = blockIdx.x * blockDim.x + threadIdx.x;
        int rgba_y = blockIdx.y * blockDim.y + threadIdx.y;
        int pixel_pos = rgba_x+rgba_y*numCols;
    
        uchar4 rgba = rgbaImage[pixel_pos];
        unsigned char gray = (unsigned char)(0.299f * rgba.x + 0.587f * rgba.y + 0.114f * rgba.z);
        greyImage[pixel_pos] = gray;
    }
    
    void your_rgba_to_greyscale(const uchar4 * const h_rgbaImage, uchar4 * const d_rgbaImage,
                                unsigned char* const d_greyImage, size_t numRows, size_t numCols)
    {
        //You must fill in the correct sizes for the blockSize and gridSize
        //currently only one block with one thread is being launched
        const dim3 blockSize(24, 24, 1);  //TODO
        const dim3 gridSize( numCols/24+1, numRows/24+1, 1);  //TODO
        rgba_to_greyscale<<>>(d_rgbaImage, d_greyImage, numRows, numCols);
    
        cudaDeviceSynchronize(); checkCudaErrors(cudaGetLastError());
    }
    

提交回复
热议问题