Advertisement
phystota

histogram_1

Oct 29th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. // Histogram Equalization
  2.  
  3. #include <wb.h>
  4.  
  5. #define HISTOGRAM_LENGTH 256
  6.  
  7. //@@ insert code here
  8.  
  9. #define wbCheck(stmt)                                                     \
  10.   do {                                                                    \
  11.     cudaError_t err = stmt;                                               \
  12.     if (err != cudaSuccess) {                                             \
  13.       wbLog(ERROR, "Failed to run stmt ", #stmt);                         \
  14.       wbLog(ERROR, "Got CUDA error ...  ", cudaGetErrorString(err));      \
  15.       return -1;                                                          \
  16.     }                                                                     \
  17.   } while (0)
  18.  
  19. __global__ void FloattoUChar(float *Pin, unsigned char *Pout, int imgsize){
  20.   unsigned int t = blockIdx.xblockDim.x + threadIdx.x;
  21.   if (t < imgsize){
  22.     Pout[t] =  (unsigned char) ((HISTOGRAM_LENGTH - 1) * Pin[t]);
  23.   }
  24. }
  25.  
  26. __global__ void ColortoGray(unsigned char *Pin, unsigned char *Pout, int width, int height, int channels){
  27.   unsigned int t = blockIdx.x*blockDim.x + threadIdx.x;
  28.   unsigned int imgsize = width*height*channels;
  29.   if (t < imgsize){
  30.     int rgbOffset = t * channels;
  31.     unsigned char r = Pin[rgbOffset    ];
  32.     unsigned char g = Pin[rgbOffset + 1];
  33.     unsigned char b = Pin[rgbOffset + 2];
  34.  
  35.     Pout[grayOffset] = (unsigned char) (0.21*r + 0.71*g + 0.07*b); ///// check this line again
  36.   }
  37. }
  38.  
  39. __global__ void GraytoHist(unsigned char *Pin, unsigned int *Pout, int imgsize){
  40.   __shared__ unsigned int hist[HISTOGRAM_LENGTH];
  41.  
  42.   unsigned int t = blockIdx.x*blockDim.x + threadIdx.x;
  43.   if (threadIdx.x < HISTOGRAM_LENGTH){ // set histogram initial values to zero
  44.     hist[t] = 0;
  45.   }
  46.   __syncthreads();
  47.  
  48.   if (t < imgsize){     // feeding the histogram, Pin[t] always from 0 to 255, so hist[Pin[t]] matches
  49.     atomicAdd(&hist[Pin[t]], 1);
  50.   }
  51.   __syncthreads();
  52.  
  53.   if (threadIdx.x < HISTOGRAM_LENGTH){    // adding results from different block to the output
  54.     atomicAdd(&Pout[threadIdx.x], hist[threadIdx.x]);
  55.   }
  56. }
  57.  
  58.  
  59. int main(int argc, char **argv) {
  60.   wbArg_t args;
  61.   int imageWidth;
  62.   int imageHeight;
  63.   int imageChannels;
  64.   wbImage_t inputImage;
  65.   wbImage_t outputImage;
  66.   float *hostInputImageData;
  67.   float *hostOutputImageData;
  68.   const char *inputImageFile;
  69.  
  70.   float *deviceInput;
  71.   unsigned char *deviceUChar;
  72.   unsigned char *deviceGray;
  73.   unsigned int *deviceHist;
  74.  
  75.  
  76.  
  77.   args = wbArg_read(argc, argv); /* parse the input arguments */
  78.  
  79.   inputImageFile = wbArg_getInputFile(args, 0);
  80.  
  81.   //Import data and create memory on host
  82.   inputImage = wbImport(inputImageFile);
  83.   imageWidth = wbImage_getWidth(inputImage);
  84.   imageHeight = wbImage_getHeight(inputImage);
  85.   imageChannels = wbImage_getChannels(inputImage);
  86.   outputImage = wbImage_new(imageWidth, imageHeight, imageChannels);
  87.  
  88.   int imgsize = imageWidth*imageHeight*imageChannels;
  89.  
  90.   hostInputImageData = wbImage_getData(inputImage);
  91.   hostOutputImageData = wbImage_getData(outputImage);
  92.  
  93.   wbCheck(cudaMalloc((void**)&deviceInput, imgsize * sizeof(float)));
  94.   wbCheck(cudaMalloc((void**)&deviceUChar, imgsize * sizeof(unsigned char)));
  95.   wbCheck(cudaMalloc((void**)&deviceGray,  imageWidth*imageHeight* sizeof(unsigned char)));  
  96.   wbCheck(cudaMalloc((void**)&deviceHist, imageWidth*imageHeight * sizeof(unsigned int)));
  97.  
  98.   wbCheck(cudaMemcpy(deviceInput, hostInputImageData, imgsize * sizeof(float), cudaMemcpyHostToDevice));
  99.   wbCheck(cudaMemset((void *) deviceHist, 0, HISTOGRAM_LENGTH * sizeof(unsigned int)));
  100.  
  101.   dim3 DimGrid(ceil(1.0*imgsize/(HISTOGRAM_LENGTH)), 1, 1);
  102.   dim3 DimBlock(HISTOGRAM_LENGTH, 1, 1);
  103.  
  104.   FloattoUChar <<<DimGrid, DimBlock>>>(deviceInput, deviceUChar, imgsize);
  105.   ColortoGray <<<DimGrid, DimBlock>>>(deviceUChar, deviceGray, imageWidth, imageHeight, imageChannels);
  106.   GraytoHist <<<DimGrid, DimBlock>>>(deviceGray, deviceHist, imageWidth*imageHeight);
  107.  
  108.  
  109.  
  110.  
  111.   wbSolution(args, outputImage);
  112.  
  113.   cudaFree(deviceInput);
  114.   cudaFree(deviceUChar);
  115.   cudaFree(deviceGray);
  116.  
  117.   return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement