colored image to greyscale image using CUDA parallel processing

前端 未结 12 1229
失恋的感觉
失恋的感觉 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 20:11

    same code with with ability to handle non-standard input size images

    int idx=blockDim.x*blockIdx.x+threadIdx.x;
    int idy=blockDim.y*blockIdx.y+threadIdx.y;
    
    uchar4 rgbcell=rgbaImage[idx*numCols+idy];
    
       greyImage[idx*numCols+idy]=0.299*rgbcell.x+0.587*rgbcell.y+0.114*rgbcell.z;
    
    
      }
    
      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
    
    int totalpixels=numRows*numCols;
    int factors[]={2,4,8,16,24,32};
    vector numbers(factors,factors+sizeof(factors)/sizeof(int));
    int factor=1;
    
       while(!numbers.empty())
      {
     if(totalpixels%numbers.back()==0)
     {
         factor=numbers.back();
         break;
     }
       else
       {
      numbers.pop_back();
       }
     }
    
    
    
     const dim3 blockSize(factor, factor, 1);  //TODO
     const dim3 gridSize(numRows/factor+1, numCols/factor+1,1);  //TODO
     rgba_to_greyscale<<>>(d_rgbaImage, d_greyImage,    numRows, numCols);
    

提交回复
热议问题