-
Notifications
You must be signed in to change notification settings - Fork 2
/
Convolution.cs
344 lines (333 loc) · 16.6 KB
/
Convolution.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Vision.NET 2.1 Computer Vision Library
* Copyright (C) 2009 Matthew Johnson
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace VisionNET
{
/// <summary>
/// This class contains routines for convolving images with image filter kernels.
/// </summary>
public static class Convolution
{
/// <summary>
/// Convolves the provided image with a two dimensional Gaussian of the provided sigma and returns the result.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="sigma">The sigma to use in the Gaussian</param>
/// <returns>A blurred image</returns>
public static I ConvolveGaussian<I>(IArrayHandler<float> image, float sigma) where I : IArrayHandler<float>, new()
{
return ConvolveGaussian<I>(image, sigma, 1);
}
/// <summary>
/// Convolves the provided image with a two dimensional Gaussian of the provided sigma and returns the result,
/// subsampled as directed.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="sigma">The sigma to use in the Gaussian</param>
/// <param name="subsample">The subsampling frequency.</param>
/// <returns>A blurred image</returns>
public static I ConvolveGaussian<I>(IArrayHandler<float> image, float sigma, int subsample) where I : IArrayHandler<float>, new()
{
I result = ConvolveHalf<I>(image, Gaussian.ComputeHalfKernel(sigma), subsample);
return result;
}
/// <summary>
/// Convolves the image using the provided kernel for both horizontal and vertical convolution.
/// The kernel is assumed to be radially invariant, seperable and
/// take the form {center value, value 1 pixel from center, value 2 pixels from center, etc.}.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernel">The kernel to use in both directions.</param>
/// <param name="subsample">The amount to subsample the image.</param>
/// <returns>A fitlered image</returns>
public static I ConvolveHalf<I>(IArrayHandler<float> image, float[] kernel, int subsample) where I : IArrayHandler<float>, new()
{
return ConvolveHalf<I>(image, kernel, kernel, subsample);
}
/// <summary>
/// Convolves an image with the provided kernel. The kernel is assumed to be radially invariant, seperable and
/// take the form {center value, value 1 pixel from center, value 2 pixels from center, etc.}.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernel">The kernel to use for convolution</param>
/// <returns>A fitlered image</returns>
public static I ConvolveHalf<I>(IArrayHandler<float> image, float[] kernel) where I : IArrayHandler<float>, new()
{
return ConvolveHalf<I>(image, kernel, 1);
}
/// <summary>
/// Convolves an image with the provided kernels. Both kernels are assumed to be radially invariant, seperable and
/// take the form {center value, value 1 pixel from center, value 2 pixels from center, etc.}. The result is
/// sub-sampled using the provided frequency.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernelx">The kernel to use for convolution in the horizontal direction</param>
/// <param name="kernely">The kernel to use for convolution in the vertical direction</param>
/// <param name="subsample">The subsampling frequency</param>
/// <returns>a fitlered image</returns>
public static unsafe I ConvolveHalf<I>(IArrayHandler<float> image, float[] kernelx, float[] kernely, int subsample)
where I : IArrayHandler<float>, new()
{
int rows = image.Rows;
int columns = image.Columns;
int channels = image.Channels;
int sizex = kernelx.Length;
int sizey = kernely.Length;
float[, ,] dest = new float[rows, columns, channels];
fixed (float* src = image.RawArray, dst = dest, knl = kernely)
{
float* srcPtr = src;
float* dstPtr = dst;
int stride = columns * channels;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
for (int i = 0; i < channels; i++, srcPtr++, dstPtr++)
{
float* knlPtr = knl;
float sum = *knlPtr * *srcPtr;
knlPtr++;
for (int k = 1; k < sizey; k++, knlPtr++)
{
int diff = k * stride;
int nr = r - k;
int pr = r + k;
if (nr < 0)
sum += 2 * (*knlPtr * *(srcPtr + diff));
else if (pr >= rows)
sum += 2 * (*knlPtr * *(srcPtr - diff));
else sum += *knlPtr * *(srcPtr - diff) + *knlPtr * *(srcPtr + diff);
}
*dstPtr = sum;
}
}
}
}
int nrows = rows / subsample;
int ncolumns = columns / subsample;
float[,,] source = dest;
dest = new float[nrows, ncolumns, channels];
fixed (float* src = source, dst = dest, knl = kernelx)
{
int stride = columns * channels;
float* srcPtr = src + stride*(subsample/2) + channels*(subsample/2);
float* dstPtr = dst;
for (int r = 0; r < nrows; r++, srcPtr += subsample * stride)
{
float* srcScan = srcPtr;
for (int c = 0; c < ncolumns; c++, srcScan += channels * (subsample - 1))
{
for (int i = 0; i < channels; i++, srcScan++, dstPtr++)
{
float* knlPtr = knl;
float sum = *knlPtr * *srcScan;
knlPtr++;
for (int k = 1; k < sizex; k++, knlPtr++)
{
int nc = c - k;
int pc = c + k;
int diff = k * channels;
if (nc < 0)
sum += 2 * (*knlPtr * *(srcScan + diff));
else if (pc >= columns)
sum += 2 * (*knlPtr * *(srcScan - diff));
else sum += *knlPtr * *(srcScan + diff) + *knlPtr * *(srcScan - diff);
}
*dstPtr = sum;
}
}
}
}
I result = new I();
result.SetData(dest);
return result;
}
/// <summary>
/// Convolves an image with the provided kernels. These Kernels are full kernels, in that they go from
/// a minimum value to a maximum value. There are no restrictions on what these kernels can be, though
/// the user is cautioned to make sure that they are passing kernels which make sense, as this code
/// does not check for any of the necessary kernel conditions.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernel">The kernel to use for convolution in the horizontal direction</param>
/// <returns>a fitlered image</returns>
public static I ConvolveFull<I>(IArrayHandler<float> image, float[] kernel)
where I : IArrayHandler<float>, new()
{
return ConvolveFull<I>(image, kernel, 1);
}
/// <summary>
/// Convolves an image with the provided kernels. These Kernels are full kernels, in that they go from
/// a minimum value to a maximum value. There are no restrictions on what these kernels can be, though
/// the user is cautioned to make sure that they are passing kernels which make sense, as this code
/// does not check for any of the necessary kernel conditions.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernel">The kernel to use for convolution in the horizontal direction</param>
/// <param name="subsample">The subsampling frequency</param>
/// <returns>a fitlered image</returns>
public static I ConvolveFull<I>(IArrayHandler<float> image, float[] kernel, int subsample)
where I : IArrayHandler<float>, new()
{
return ConvolveFull<I>(image, kernel, kernel, subsample);
}
/// <summary>
/// Convolves an image with the provided kernels. These Kernels are full kernels, in that they go from
/// a minimum value to a maximum value. There are no restrictions on what these kernels can be, though
/// the user is cautioned to make sure that they are passing kernels which make sense, as this code
/// does not check for any of the necessary kernel conditions.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">The image to convolve.</param>
/// <param name="kernelx">The kernel to use for convolution in the horizontal direction</param>
/// <param name="kernely">The kernel to use for convolution in the vertical direction</param>
/// <param name="subsample">The subsampling frequency</param>
/// <returns>a fitlered image</returns>
public static unsafe I ConvolveFull<I>(IArrayHandler<float> image, float[] kernelx, float[] kernely, int subsample)
where I : IArrayHandler<float>, new()
{
int rows = image.Rows;
int columns = image.Columns;
int channels = image.Channels;
int sizex = kernelx.Length;
int halfx = sizex / 2;
int sizey = kernely.Length;
int halfy = sizey / 2;
float[, ,] dest = new float[rows, columns, channels];
fixed (float* src = image.RawArray, dst = dest, knl = kernely)
{
float* srcPtr = src;
float* dstPtr = dst;
int stride = columns * channels;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
for (int i = 0; i < channels; i++, srcPtr++)
{
float* knlPtr = knl;
int diff = -halfy;
if (r + diff < 0)
diff = -r;
float* srcScan = srcPtr + diff*stride;
float sum = 0;
for (int k = 0, rr = r-halfy; k < sizey; k++, knlPtr++, rr++)
{
sum += *knlPtr * *srcScan;
if (rr >= 0 && rr < rows - 1)
srcScan += stride;
}
*dstPtr++ = sum;
}
}
}
}
int nrows = rows / subsample;
int ncolumns = columns / subsample;
float[, ,] source = dest;
dest = new float[nrows, ncolumns, channels];
fixed (float* src = source, dst = dest, knl = kernelx)
{
float* srcPtr = src;
float* dstPtr = dst;
int stride = columns * channels;
for (int r = 0, tr = 0; r < nrows; r++, tr += subsample, srcPtr += subsample * stride)
{
float* srcScan = srcPtr;
for (int c = 0, tc = 0; c < ncolumns; c++, tc += subsample, srcScan += channels * (subsample - 1))
{
for (int i = 0; i < channels; i++, srcScan++)
{
float* knlPtr = knl;
int diff = -halfx;
if (tc + diff < 0)
diff = -tc;
float* srcScan1 = srcScan + diff * channels;
float sum = 0;
for (int k = 0, cc=tc-halfx; k < sizex; k++, knlPtr++, cc++)
{
sum += *knlPtr * *srcScan1;
if (cc >= 0 && cc < columns - 1)
srcScan1 += channels;
}
*dstPtr++ = sum;
}
}
}
}
I result = new I();
result.SetData(dest);
return result;
}
/// <summary>
/// Convolves an image with the provided two-dimensional kernel. This is done in the spatial
/// domain, and as such is not as efficient as using an Fast Fourier Transform.
/// </summary>
/// <typeparam name="I">Any image whose pixel values are stored as floats</typeparam>
/// <param name="image">Image to convolve</param>
/// <param name="kernel">The two-dimensional kernel.</param>
/// <returns>The filtered image</returns>
public static unsafe I Convolve<I>(IArrayHandler<float> image, float[,] kernel)
where I : IArrayHandler<float>, new()
{
int rows = image.Rows;
int columns = image.Columns;
int channels = image.Channels;
int krows = kernel.GetLength(1);
int kcols = kernel.GetLength(0);
int kernelCenterX = kcols/2;
int kernelCenterY = krows/2;
int stride = columns*channels;
float[, ,] data = new float[rows, columns, channels];
fixed (float* src = image.RawArray, dst = data, knl = kernel)
{
float* srcPtr = src + kernelCenterY * stride + kernelCenterX * channels;
float* srcScanStart = src;
float* dstPtr = dst + kernelCenterY * stride + kernelCenterX * channels;
for (int r = kernelCenterY; r < rows - kernelCenterY; r++)
{
for (int c = 0; c < columns; c++, srcPtr += channels, srcScanStart+= channels)
{
float[] sums = new float[channels];
float* srcScan = srcScanStart;
float* knlPtr = knl;
for (int u = 0; u < krows; u++, srcScan += stride - kcols * channels)
for (int v = 0; v < kcols; v++, knlPtr++)
{
float mult = *knlPtr;
for (int i = 0; i < channels; i++, srcScan++)
sums[i] += mult * *srcScan;
}
for (int i = 0; i < channels; i++)
*dstPtr++ = sums[i];
}
}
}
I result = new I();
result.SetData(data);
return result;
}
}
}