-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathmedian_filter.h
59 lines (48 loc) · 1.86 KB
/
median_filter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
/**
This file is part of sgm. (https://github.com/dhernandez0/sgm).
Copyright (c) 2016 Daniel Hernandez Juarez.
sgm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with sgm. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef MEDIAN_FILTER_H_
#define MEDIAN_FILTER_H_
#include <stdint.h>
__global__ void MedianFilter3x3(const uint8_t* __restrict__ d_input, uint8_t* __restrict__ d_out, const uint32_t rows, const uint32_t cols);
template<int n, typename T>
__inline__ __device__ void MedianFilter(const T* __restrict__ d_input, T* __restrict__ d_out, const uint32_t rows, const uint32_t cols) {
const uint32_t idx = blockIdx.x*blockDim.x+threadIdx.x;
const uint32_t row = idx / cols;
const uint32_t col = idx % cols;
T window[n*n];
int half = n/2;
if(row >= half && col >= half && row < rows-half && col < cols-half) {
for(uint32_t i = 0; i < n; i++) {
for(uint32_t j = 0; j < n; j++) {
window[i*n+j] = d_input[(row-half+i)*cols+col-half+j];
}
}
for(uint32_t i = 0; i < (n*n/2)+1; i++) {
uint32_t min_idx = i;
for(uint32_t j = i+1; j < n*n; j++) {
if(window[j] < window[min_idx]) {
min_idx = j;
}
}
const T tmp = window[i];
window[i] = window[min_idx];
window[min_idx] = tmp;
}
d_out[idx] = window[n*n/2];
} else if(row < rows && col < cols) {
d_out[idx] = d_input[idx];
}
}
#endif /* MEDIAN_FILTER_H_ */