Blame view

stim/math/filters/median2.cuh 4.69 KB
58fdd08b   Jiabing Li   added median filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #ifndef STIM_CUDA_MEDIAN2_H
  #define STIM_CUDA_MEDIAN2_H
  
  #include <iostream>
  #include <cuda.h>
  #include <cmath>
  #include <algorithm>
  
  #ifdef USING_CUDA
  #include "cuda_runtime.h"
  #include "device_launch_parameters.h"
  #include <stim/cuda/cudatools.h>
  #endif
  
b87014ef   Jiabing Li   added median filter
15
  
58fdd08b   Jiabing Li   added median filter
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  //#include <thrust/sort.h>
  //#include <thrust/execution_policy.h>
  //#include <thrust/binary_search.h>
  //#include <thrust/device_ptr.h>
  
  template <typename T> 
  __device__ void cuswap ( T& a, T& b ){
  	T c(a);
  	a=b;
  	b=c;
  }
  
  namespace stim{
  	namespace cuda{
  
  		template<typename T>
  		__global__ void cuda_median2(T* in, T* out, T* kernel, size_t X, size_t Y, size_t K){
  
  			int xi = blockIdx.x * blockDim.x + threadIdx.x;
  			int yi = blockIdx.y * blockDim.y + threadIdx.y;			
  
  			size_t Xout = X - K + 1;
  			size_t Yout = Y - K + 1;
  			int i = yi * Xout + xi;
  
  			//return if (i,j) is outside the matrix
  			if(xi >= Xout || yi >= Yout)   return;		
  
  			//scan the image, copy data from input to 2D window
  			size_t kxi, kyi;
  			for(kyi = 0; kyi < K; kyi++){
  				for(kxi = 0; kxi < K; kxi++){
  					kernel[i * K * K + kyi * K + kxi] = in[(yi + kyi)* X + xi + kxi];				      
  				}
  			}					
  					
  			//calculate kernel radius 4
              int r = (K*K)/2;
  			
  			//sort the smallest half pixel values inside the window, calculate the middle one
  			size_t Ki = i * K * K;
  		
  			//sort the smallest half pixel values inside the window, calculate the middle one
              for (int  p = 0; p < r+1; p++){                             
  			    for(int kk = p + 1; kk < K*K; kk++){
                         if (kernel[Ki + kk] < kernel[Ki + p]){
  						   cuswap<T>(kernel[Ki + kk], kernel[Ki + p]);
  					   }
                    }
               }
              
  			//copy the middle pixel value inside the window to output
              out[i] = kernel[Ki + r]; 
  				     
          }
  		 
  		
  		template<typename T>
  		void gpu_median2(T* gpu_in, T* gpu_out, T* gpu_kernel, size_t X, size_t Y, size_t K){
                 
  			//get the maximum number of threads per block for the CUDA device
              int threads_total = stim::maxThreadsPerBlock();
  
  			//set threads in each block
  			dim3 threads(sqrt(threads_total), sqrt(threads_total));
  
  			//calculate the number of blocks
  			dim3 blocks(( (X - K + 1) / threads.x) + 1, ((Y - K + 1) / threads.y) + 1);
  
  			//call the kernel to perform median filter function
  		    cuda_median2 <<< blocks, threads >>>( gpu_in, gpu_out, gpu_kernel, X, Y, K);
  		
  		}
  
  		template<typename T>
  		void cpu_median2(T* cpu_in, T* cpu_out, size_t X, size_t Y, size_t K){
  
  		#ifndef USING_CUDA
  			
  			//output image width and height
  			 size_t X_out = X + K -1;
  			 size_t Y_out = Y + K -1;
  
  			 float* window = (float*)malloc(K * k *sizeof(float));
  
  			 for(int i = 0; i< Y; i++){
   		       for(int j =0; j< X; j++){
  
  				//Pick up window elements		
  				int k = 0; 			
  				for (int m=0; m< kernel_width; m++){
  					for(int n = 0; n < kernel_width; n++){
  						window[k] = in_image.at<float>(m+i, n+j);
  						k++;					
  					 }
  				}
  
  				//calculate kernel radius 4
  				r_ker = K * K/2;
  			    //Order elements (only half of them)
  				for(int p = 0; p<r_ker+1; p++){			
  				  //Find position of minimum element			       
  			       for (int l = p + 1; l < size_kernel; l++){
       
  					 if(window[l] < window[p]){
  					     float t  = window[p];
  					    window[p] = window[l];
  					    window[l] = t;          }		
  		     	}
  					}
  					
  			  //Get result - the middle element	
  				cpu_out[i * X_out + j] =  window[r_ker];	
  			   }
  			 }
  		#else
  			//calculate input and out image pixels & calculate kernel size
  			size_t N_in = X * Y;									//number of pixels in the input image
  			size_t N_out = (X - K + 1) * (Y - K + 1);								//number of pixels in the output image
              //size_t kernel_size = kernel_width*kernel_width;				//total number of pixels in the kernel
              
  			//allocate memory on the GPU for the array
  			T* gpu_in;																	//allocate device memory for the input image
  			HANDLE_ERROR( cudaMalloc( &gpu_in, N_in * sizeof(T) ) );
  			T* gpu_kernel; 															//allocate device memory for the kernel
  			HANDLE_ERROR( cudaMalloc( &gpu_kernel, K * K * N_out * sizeof(T) ) );
  			T* gpu_out;																	//allocate device memory for the output image
  			HANDLE_ERROR( cudaMalloc( &gpu_out, N_out * sizeof(T) ) );
  
  			//copy the array to the GPU
  			HANDLE_ERROR( cudaMemcpy( gpu_in, cpu_in, N_in * sizeof(T), cudaMemcpyHostToDevice) );
  			
  			//call the GPU version of this function
  			gpu_median2<T>(gpu_in, gpu_out, gpu_kernel, X, Y, K);
  
  			//copy the array back to the CPU
  			HANDLE_ERROR( cudaMemcpy( cpu_out, gpu_out, N_out * sizeof(T), cudaMemcpyDeviceToHost) );
  
  			//free allocated memory
  			cudaFree(gpu_in);
  			cudaFree(gpu_kernel);
  			cudaFree(gpu_out);
  
  		}
  		
  	}
  	#endif
  }
  
  
  #endif