-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgaussian_filter.F90
190 lines (147 loc) · 6.04 KB
/
gaussian_filter.F90
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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module gaussian_filter
use, intrinsic :: iso_fortran_env, only: error_unit
implicit none
private
public gaussian_kernel
public convolve
public assert
public tile_and_reflect
contains
! This is a copy of code found in gaussian_filter.py. These two implementations
! must remain equivalent for tests to pass.
subroutine gaussian_kernel(sigma, kernel, truncate)
real, intent(in) :: sigma
real, intent(out), dimension(:,:), allocatable :: kernel
real, intent(in), optional :: truncate
real, dimension(:,:), allocatable :: x, y
integer :: radius, trunc, i, j
real :: s
if (present(truncate)) then
trunc = truncate
else
trunc = 4.0
endif
radius = int(trunc * sigma + 0.5)
s = sigma**2
! Set up meshgrid.
allocate(x(-radius:radius, -radius:radius))
allocate(y(-radius:radius, -radius:radius))
do j = -radius, radius
do i = -radius, radius
x(i, j) = i
y(i, j) = j
enddo
enddo
! Make kernel.
allocate(kernel(-radius:radius, -radius:radius))
kernel = 2.0*exp(-0.5 * (x**2 + y**2) / s)
kernel = kernel / sum(kernel)
deallocate(x)
deallocate(y)
end subroutine gaussian_kernel
! Set up 3x3 tiles around the input.
subroutine tile_and_reflect(input, output)
real, intent(in), dimension(:,:) :: input
real, intent(out), dimension(:,:), allocatable :: output
integer :: rows, cols
rows = ubound(input, 1)
cols = ubound(input, 2)
! Rely on automatic deallocation to clean this up.
allocate(output(3*rows, 3*cols))
! There are 3x3 tiles, we start at the top left and set the tiles up row by
! row.
! Top left is flipped left-to-right and up-to-down.
output(:rows, :cols) = input(rows:1:-1, cols:1:-1)
! Top centre is flipped up-to-down
output(:rows, cols+1:2*cols) = input(rows:1:-1, :)
! Top right is flipped left-to-right and up-to-down.
output(:rows, 2*cols+1:3*cols) = input(rows:1:-1, cols:1:-1)
! Middle left flipped left-to-right
output(rows+1:2*rows, :cols) = input(:, cols:1:-1)
! Middle centre unchanged
output(rows+1:2*rows, cols+1:2*cols) = input(:, :)
! Middle right flipped left-to-right
output(rows+1:2*rows, 2*cols+1:3*cols) = input(:, cols:1:-1)
! Bottom left flipped left-to-right and up-to-down
output(2*rows+1:3*rows, :cols) = input(rows:1:-1, cols:1:-1)
! Bottom cente flipped up-to-down
output(2*rows+1:3*rows, cols+1:2*cols) = input(rows:1:-1, :)
! Bottom right flipped left-to-right and up-to-down
output(2*rows+1:3*rows, 2*cols+1:3*cols) = input(rows:1:-1, cols:1:-1)
end subroutine tile_and_reflect
! Convolution.
subroutine convolve(input, weights, output, mask)
real, intent(in), dimension(:,:) :: input, weights
! The mask is 0 on masked points and 1 on non-masked. All masked points are
! left unchanged.
real, intent(in), dimension(:,:), optional :: mask
real, intent(inout), dimension(:,:) :: output
! These are allocated within tile_and_reflect, we rely on automatic
! deallocation at the end of the subroutine.
real, dimension(:, :), allocatable, target :: tiled_input
real, dimension(:, :), allocatable, target :: tiled_mask
real, dimension(:, :), pointer :: overlapping, overlapping_mask
integer :: rows, cols, hw_row, hw_col, i, j, tj, ti
real :: clobber_total, correction
! First step is to tile the input.
rows = ubound(input, 1)
cols = ubound(input, 2)
! Stands for half weights row.
hw_row = ubound(weights, 1) / 2
hw_col = ubound(weights, 2) / 2
! Only one reflection is done on each side so the weights array cannot be
! bigger than width/height of input +1.
call assert(ubound(weights, 1) < rows + 1, &
'Input size too small for weights matrix')
call assert(ubound(weights, 2) < cols + 1, &
'Input size too small for weights matrix')
if (present(mask)) then
call assert(all(shape(mask) - shape(input) == 0), &
'Mask and input shapes do not match')
call tile_and_reflect(mask, tiled_mask)
endif
! This ensures that in the masked case, all masked points remain unchanged.
output(:,:) = input(:,:)
call tile_and_reflect(input, tiled_input)
! Very similar Python code can be found in gaussian_filter.py.
do j = 1, cols
do i = 1, rows
! Use i, j to offset into equivalent part of the tiled arrays.
ti = i + rows
tj = j + cols
! Skip masked points.
if (present(mask)) then
if (tiled_mask(ti, tj) == 0) then
cycle
endif
endif
overlapping => tiled_input(ti - hw_row:ti + hw_row, &
tj - hw_col:tj + hw_col)
if (present(mask)) then
! The approach taken here is to find out which parts of the
! weights matrix are masked, add up the value of all these and
! destribute them evenly over the rest of the matrix. The
! intention is to conserve the field.
overlapping_mask => tiled_mask(ti - hw_row:ti + hw_row, &
tj - hw_col:tj + hw_col)
! Total value and number of weights clobbered by the mask.
clobber_total = sum((1 - overlapping_mask) * weights)
correction = clobber_total / sum(overlapping_mask)
! Add correction and calculate.
output(i, j) = sum((weights(:, :) + correction) * overlapping &
* overlapping_mask)
else
output(i, j) = sum(weights(:,:) * overlapping)
endif
enddo
enddo
end subroutine convolve
subroutine assert(statement, msg)
logical, intent(in) :: statement
character(len=*), intent(in) :: msg
if (.not. statement) then
write(error_unit, *) msg
stop 'Assert triggered, see stderr.'
endif
end subroutine assert
end module gaussian_filter