Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <omp.h>
  4. #include <opencv/cv.h>
  5. #include <opencv/highgui.h>
  6.  
  7. using namespace cv;
  8. using namespace std;
  9.  
  10. __constant__ int maskaGPU[5][5];
  11.  
  12. __global__ void rozmycie(int kanal_zdj, int sumaWag, unsigned char* zdjWe, unsigned char* zdjWy, int wysokosc, int rozmiar) {
  13.  
  14.     int id = blockIdx.x * blockDim.x + threadIdx.x;
  15.     double piksel= 0;
  16.     int indeks,x,y;
  17.  
  18.     while(id < wysokosc*rozmiar) {
  19.             for(x = 0; x < 5; x++) {
  20.                 for(y = 0; y < 5; y++) {
  21.                     indeks = id + ((y - 2) * rozmiar) + ((x - 2) * kanal_zdj);
  22.                     piksel+= maskaGPU[x][y] * zdjWe[indeks];
  23.                 }
  24.             }
  25.             zdjWy[id] = (unsigned char)(piksel/sumaWag);
  26.             id += blockDim.x * gridDim.x;
  27.             piksel = 0;
  28.     }
  29. }
  30.  
  31. int sumaMaski(int maska[5][5], int x, int y, int suma){
  32.     for (x=0; x<5;x++){
  33.         for (y=0; y<5;y++){
  34.             suma+=maska[x][y];
  35.         }
  36.     }
  37.     return suma;
  38. }
  39.  
  40. int main(int argc, char** argv ){
  41.  
  42.     cudaEvent_t czas_start, czas_stop;
  43.     cudaEventCreate(&czas_start);
  44.     cudaEventCreate(&czas_stop);
  45.     unsigned char *zdj_gpu_we, *zdj_gpu_wy;
  46.  
  47.   int maska[5][5] ={
  48.     {1,1,2,1,1},
  49.     {1,2,4,2,1},
  50.     {2,4,8,4,2},
  51.     {1,2,4,2,1},
  52.     {1,1,2,1,1}
  53.   };
  54.     cudaMemcpyToSymbol(maskaGPU, &maska, sizeof(int) * 5 * 5);
  55.  
  56.   if ( argc != 3 ){
  57.       cout << "0 ms - brak wszystkich argumentow" << endl;
  58.       return 0;
  59.   }
  60.  
  61.   Mat zdj_we,zdj_wy;
  62.   zdj_we = imread(argv[1], CV_LOAD_IMAGE_COLOR);
  63.     if ( !zdj_we.data ){
  64.       cout <<  "0 ms - problem z ladowaniem wartosci" << endl ;
  65.       return 0;
  66.   }
  67.  
  68.   zdj_wy = zdj_we.clone();
  69.  
  70.     cudaMalloc(&zdj_gpu_we, zdj_we.rows*zdj_we.step*sizeof(unsigned char));
  71.     cudaMalloc(&zdj_gpu_wy, zdj_we.rows*zdj_we.step*sizeof(unsigned char));
  72.  
  73.     cudaMemcpy(zdj_gpu_we, zdj_we.ptr(), zdj_we.rows*zdj_we.step, cudaMemcpyHostToDevice);
  74.  
  75.     cudaEventRecord(czas_start, 0);
  76.  
  77.     rozmycie<<<32,1>>>(zdj_we.channels(), sumaMaski(maska,0,0,0), zdj_gpu_we, zdj_gpu_wy, zdj_we.rows, zdj_we.step);
  78.  
  79.     //cout<<"Kanał: "<<zdj_we.channels()<<endl;
  80.     cudaEventRecord(czas_stop, 0);
  81.     cudaEventSynchronize(czas_stop);
  82.     float czas = 0.0;
  83.     cudaEventElapsedTime(&czas, czas_start, czas_stop);
  84.  
  85.     cudaDeviceSynchronize();
  86.  
  87.     cudaMemcpy(zdj_wy.ptr(), zdj_gpu_wy, zdj_we.rows*zdj_we.step, cudaMemcpyDeviceToHost);
  88.  
  89.     cudaFree(zdj_gpu_wy);
  90.     cudaFree(zdj_gpu_we);
  91.  
  92.     imwrite(argv[2], zdj_wy);
  93.     printf("Czas: %.3f ms", czas);
  94.    return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement