gaussian.hh 24.5 KB
Newer Older
1
// Copyright (C) 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011,
2
// 2012, 2013 EPITA Research and Development Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// Olena 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, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
11
12
13
14
15
// 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
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
26

27
28
#ifndef MLN_LINEAR_GAUSSIAN_HH
# define MLN_LINEAR_GAUSSIAN_HH
29

30
/// \file
31
32
33
///
/// Gaussian filter.
///
34
/// \todo Add a clean reference Rachid Deriche
35
///	 Recursively implementing the gaussian and its derivatives (1993)
36

37
38
39
# include <vector>
# include <cmath>

40
# include <mln/core/concept/image.hh>
41
# include <mln/core/alias/point2d.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
42
43
# include <mln/core/alias/dpoint1d.hh>
# include <mln/core/alias/dpoint3d.hh>
44
# include <mln/extension/adjust_fill.hh>
45
46
# include <mln/geom/ncols.hh>
# include <mln/geom/nrows.hh>
47
48
49
50
51
52
# include <mln/geom/min_col.hh>
# include <mln/geom/max_col.hh>
# include <mln/geom/min_row.hh>
# include <mln/geom/max_row.hh>
# include <mln/geom/min_sli.hh>
# include <mln/geom/max_sli.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
53
# include <mln/geom/ninds.hh>
54
# include <mln/geom/nslis.hh>
55
# include <mln/data/paste.hh>
56
# include <mln/data/stretch.hh>
57
58
# include <mln/algebra/vec.hh>

59

60
61
62
63
64
65
namespace mln
{

  namespace linear
  {

66
67
68
69
70
71
    /*! \brief Gaussian filter of an image \p input

        \pre output.domain = input.domain

	\ingroup mlnlinear
    */
72
    template <typename I>
Matthieu Garrigues's avatar
Matthieu Garrigues committed
73
74
    mln_concrete(I)
    gaussian(const Image<I>& input, float sigma);
75

76

77
78
79
    /*! \overload
      \ingroup mlnlinear
     */
80
81
82
83
84
    template <typename I>
    mln_concrete(I)
    gaussian(const Image<I>& input, float sigma, int dir);


85
86
# ifndef MLN_INCLUDE_ONLY

87
88
89
    namespace impl
    {

90
91
92
93
94
95
96
      typedef float norm_fun(float, float,
			     float, float,
			     float, float,
			     float, float,
			     float, float,
			     int&);

97
      struct recursivefilter_coef_
98
99
      {

100
101
102
	///
	/// Constructor.
	///
103
104
105
106
	recursivefilter_coef_(float a0, float a1,
			      float b0, float b1,
			      float c0, float c1,
			      float w0, float w1,
107
			      float s, norm_fun norm);
108
109
110
	std::vector<float> n, d, nm, dm;
      };

111
      inline
112
      recursivefilter_coef_::recursivefilter_coef_(float a0, float a1,
113
114
115
116
						      float b0, float b1,
						      float c0, float c1,
						      float w0, float w1,
						      float s, norm_fun norm)
117
118
119
120
121
122
123
124
125
126
127
      {
	n.reserve(5);
	d.reserve(5);
	nm.reserve(5);
	dm.reserve(5);

	b0 /= s;
	b1 /= s;
	w0 /= s;
	w1 /= s;

128
129
130
131
	float sin0 = std::sin(w0);
	float sin1 = std::sin(w1);
	float cos0 = std::cos(w0);
	float cos1 = std::cos(w1);
132

133
134
	int sign = 1;
	float n_ = norm(a0, a1, b0, b1, c0, c1, cos0, sin0, cos1, sin1, sign);
135

136
137
138
139
	a0 /= n_;
	a1 /= n_;
	c0 /= n_;
	c1 /= n_;
140
141

	n[3] =
142
143
	  std::exp(-b1 - 2*b0) * (c1 * sin1 - cos1 * c0) +
	  std::exp(-b0 - 2*b1) * (a1 * sin0 - cos0 * a0);
144
	n[2] =
145
	  2 * std::exp(-b0 - b1) * ((a0 + c0) * cos1 * cos0 -
146
147
			       cos1 * a1 * sin0 -
			       cos0 * c1 * sin1) +
148
	  c0 * std::exp(-2*b0) + a0 * std::exp(-2*b1);
149
	n[1] =
150
151
	  std::exp(-b1) * (c1 * sin1 - (c0 + 2 * a0) * cos1) +
	  std::exp(-b0) * (a1 * sin0 - (2 * c0 + a0) * cos0);
152
153
154
	n[0] =
	  a0 + c0;

155
	d[4] =
156
	  std::exp(-2 * b0 - 2 * b1);
157
	d[3] =
158
159
	  -2 * cos0 * std::exp(-b0 - 2*b1) -
	  2 * cos1 * std::exp(-b1 - 2*b0);
160
	d[2] =
161
162
	  4 * cos1 * cos0 * std::exp(-b0 - b1) +
	  std::exp(-2*b1) + std::exp(-2*b0);
163
	d[1] =
164
	  -2 * std::exp(-b1) * cos1 - 2 * std::exp(-b0) * cos0;
165
166

	for (unsigned i = 1; i <= 3; ++i)
167
	{
168
	    dm[i] = d[i];
169
	    nm[i] = float(sign) * (n[i] - d[i] * n[0]);
170
	}
171
	dm[4] = d[4];
172
	nm[4] = float(sign) * (-d[4] * n[0]);
173
174
175
176
177
      }



      template <class WorkType, class I>
178
      inline
179
180
181
      void
      recursivefilter_(I& ima,
		       const recursivefilter_coef_& c,
182
183
		       const mln_psite(I)& start,
		       const mln_psite(I)& finish,
184
		       int len,
185
		       const mln_deduce(I, psite, delta)& d)
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
      {
	std::vector<WorkType>	tmp1(len);
	std::vector<WorkType>	tmp2(len);

	// The fourth degree approximation implies to have a special
	// look on the four first points we consider that there is
	// no signal before 0 (to be discussed)

	// --
	// Causal part

	tmp1[0] =
	  c.n[0] * ima(start);

	tmp1[1] =
	  c.n[0] * ima(start + d)
	  + c.n[1] * ima(start)
	  - c.d[1] * tmp1[0];

	tmp1[2] =
	  c.n[0] * ima(start + d + d)
	  + c.n[1] * ima(start + d)
	  + c.n[2] * ima(start)
	  - c.d[1] * tmp1[1]
	  - c.d[2] * tmp1[0];

	tmp1[3] =
	  c.n[0] * ima(start + d + d + d)
	  + c.n[1] * ima(start + d + d)
	  + c.n[2] * ima(start + d)
	  + c.n[3] * ima(start)
217
	  - c.d[1] * tmp1[2] - c.d[2] * tmp1[1]
218
219
	  - c.d[3] * tmp1[0];

220
	mln_psite(I) current(start + d + d + d + d);
221
	for (mln_deduce(I, site, coord) i = 4; i < len; ++i)
222
223
224
225
226
227
228
229
230
231
        {
          tmp1[i] =
            c.n[0] * ima(current)
            + c.n[1] * ima(current - d)
            + c.n[2] * ima(current - d - d)
            + c.n[3] * ima(current - d - d - d)
            - c.d[1] * tmp1[i - 1] - c.d[2] * tmp1[i - 2]
            - c.d[3] * tmp1[i - 3] - c.d[4] * tmp1[i - 4];
          current = current + d;
        }
232

233
	// Non causal part
234

235
	tmp2[len - 1] = WorkType(); // FIXME : = 0, literal::zero ...?
236

237
238
	tmp2[len - 2] =
	  c.nm[1] * ima(finish);
239

240
241
242
	tmp2[len - 3] =
	  c.nm[1] * ima(finish - d)
	  + c.nm[2] * ima(finish)
243
	  - c.dm[1] * tmp2[len - 2];
244

245
246
247
248
	tmp2[len - 4] =
	  c.nm[1] * ima(finish - d - d)
	  + c.nm[2] * ima(finish - d)
	  + c.nm[3] * ima(finish)
249
250
	  - c.dm[1] * tmp2[len - 3]
	  - c.dm[2] * tmp2[len - 2];
251

252
	current = finish - d - d - d ;
253

254
	for (int i = len - 5; i >= 0; --i)
255
256
257
258
259
260
261
262
263
264
	{
          tmp2[i] =
            c.nm[1] * ima(current)
            + c.nm[2] * ima(current + d)
            + c.nm[3] * ima(current + d + d)
            + c.nm[4] * ima(current + d + d + d)
            - c.dm[1] * tmp2[i + 1] - c.dm[2] * tmp2[i + 2]
            - c.dm[3] * tmp2[i + 3] - c.dm[4] * tmp2[i + 4];
          current = current - d;
        }
265

266
267
268
	// Combine results from causal and non-causal parts.
	current = start;
	for (int i = 0; i < len; ++i)
269
270
271
272
	{
          ima(current) = tmp1[i] + tmp2[i];
          current = current + d;
	}
273
274
275
      }


276
277
278
279
280
281
282
283
      inline
      float gaussian_norm_coef_(float a0, float a1,
				float b0, float b1,
				float c0, float c1,
				float cos0, float sin0,
				float cos1, float sin1,
				int& sign)
      {
284
285
	float expb0 = std::exp(b0);
	float exp2b0 = std::exp(2.f * b0);
286
287
288
289

	float scale0 = 1 + exp2b0 - 2 * cos0 * expb0;
	float scaleA = 2 * a1 * sin0 * expb0 - a0 * (1 - exp2b0);

290
291
	float expb1 = std::exp(b1);
	float exp2b1 = std::exp(2.f * b1);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

	float scale1 = 1 + exp2b1 - 2 * cos1 * expb1;
	float scaleC = 2 * c1 * sin1 * expb1 - c0 * (1 - exp2b1);

	float sumA = scaleA / scale0;
	float sumC = scaleC / scale1;

	sign = 1;

	return (sumA + sumC);
      }

      inline
      float gaussian_1st_deriv_coef_norm_(float a0, float a1,
					  float b0, float b1,
					  float c0, float c1,
					  float cos0, float sin0,
					  float cos1, float sin1,
					  int& sign)
      {
312
313
	float expb0 = std::exp(b0);
	float exp2b0 = std::exp(2.f * b0);
314
315
316
317
318
319

	float scale0 = 1 + exp2b0 - 2 * cos0 * expb0;
	scale0 *= scale0;
	float scaleA = - 2 * a1 * sin0 * expb0 * (1 - exp2b0) +
			 2 * a0 * expb0 * (2 * expb0 - cos0 * (1 + exp2b0));

320
321
	float expb1 = std::exp(b1);
	float exp2b1 = std::exp(2.f * b1);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

	float scale1 = 1 + exp2b1 - 2 * cos1 * expb1;
	scale1 *= scale1;
	float scaleC = - 2 * c1 * sin1 * expb1 * (1 - exp2b1) +
			 2 * c0 * expb1 * (2 * expb1 - cos1 * (1 + exp2b1));

	float sumA = scaleA / scale0;
	float sumC = scaleC / scale1;

	sign = -1;

	return (sumA + sumC);
      }

      inline
      float gaussian_2nd_deriv_coef_norm_(float a0, float a1,
					  float b0, float b1,
					  float c0, float c1,
					  float cos0, float sin0,
					  float cos1, float sin1,
					  int& sign)
      {
344
345
	float expb0 = std::exp(b0);
	float exp2b0 = std::exp(2.f * b0);
346
347

	float scale0 = 1 + exp2b0 - 2 * cos0 * expb0;
348
	scale0 *= scale0 * scale0;
349

350
351
352
353
	float scaleA = a1 * sin0 * expb0 *
          (1 + expb0 * (2 * cos0 * (1 + exp2b0) + exp2b0 - 6)) +
          a0 * expb0 * (2 * expb0 * (2 - cos0 * cos0) *
                        (1 - exp2b0) - cos0 * (1 - exp2b0 * exp2b0));
354

355
356
	float expb1 = std::exp(b1);
	float exp2b1 = std::exp(2.f * b1);
357
358

	float scale1 = 1 + exp2b1 - 2 * cos1 * expb1;
359
	scale1 *= scale1 * scale1;
360

361
362
363
364
	float scaleC = c1 * sin1 * expb1 *
          (1 + expb1 * (2 * cos1 * (1 + exp2b1) + exp2b1 - 6)) +
          c0 * expb1 * (2 * expb1 * (2 - cos1 * cos1) *
                        (1 - exp2b1) - cos1 * (1 - exp2b1 * exp2b1));
365
366
367
368
369
370
371
372

	float sumA = scaleA / scale0;
	float sumC = scaleC / scale1;
	sign = 1;

	return (sumA + sumC);
      }

373

374
      template <class I, class F>
375
      inline
376
      void
377
378
      generic_filter_(trait::image::dimension::one_d,
                      Image<I>& img_, const F& coef, int dir)
379
380
      {
	I& img = exact(img_);
381
	(void) dir;
382
        mln_precondition(dir < I::site::dim);
383

384
385

        recursivefilter_<mln_value(I)>(img, coef,
386
387
388
                                       point1d(static_cast<def::coord>(-img.border())),
                                       point1d(static_cast<def::coord>(geom::ninds(img) - 1 +
								       img.border())),
389
390
                                       geom::ninds(img) + 2 * img.border(),
                                       dpoint1d(1));
391
392
      }

393
394
395
396
397
398
399
400
401
402
403
404
405
406
      template <class I, class F>
      inline
      void
      generic_filter_(trait::image::dimension::two_d,
                      Image<I>& img_, const F& coef, int dir)
      {
	I& img = exact(img_);

        mln_precondition(dir < I::site::dim);


        if (dir == 0)
        {
          // Apply on rows.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
407
          for (def::coord j = geom::min_col(img); j <= geom::max_col(img); ++j)
408
            recursivefilter_<mln_value(I)>(img, coef,
409
                                           point2d(static_cast<def::coord>(geom::min_row(img) - img.border()),
410
						   static_cast<def::coord>(j)),
411
                                           point2d(static_cast<def::coord>(geom::max_row(img) +
412
413
									   img.border()),
						   static_cast<def::coord>(j)),
414
415
416
417
418
419
420
                                           geom::nrows(img) + 2 * img.border(),
                                           dpoint2d(1, 0));
        }

        if (dir == 1)
        {
          // Apply on columns.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
421
          for (def::coord i = geom::min_row(img); i <= geom::max_row(img); ++i)
422
            recursivefilter_<mln_value(I)>(img, coef,
423
                                           point2d(static_cast<def::coord>(i),
424
						   static_cast<def::coord>(geom::min_col(img) - img.border())),
425
                                           point2d(static_cast<def::coord>(i),
426
						   static_cast<def::coord>(geom::max_col(img) +
427
									   img.border())),
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
                                           geom::ncols(img) + 2 * img.border(),
                                           dpoint2d(0, 1));
        }
      }

      template <class I, class F>
      inline
      void
      generic_filter_(trait::image::dimension::three_d,
                      Image<I>& img_, const F& coef, int dir)
      {
	I& img = exact(img_);
        mln_precondition(dir < I::site::dim);

        if (dir == 0)
        {
          // Apply on slices.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
445
446
          for (def::coord j = geom::min_row(img); j <= geom::max_row(img); ++j)
            for (def::coord k = geom::min_col(img); k <= geom::max_col(img); ++k)
447
              recursivefilter_<mln_value(I)>(img, coef,
448
449
450
                                             point3d(static_cast<def::coord>(-img.border()),
						     static_cast<def::coord>(j),
						     static_cast<def::coord>(k)),
451
                                             point3d(static_cast<def::coord>(geom::nslis(img) - 1 +
452
453
454
									     img.border()),
						     static_cast<def::coord>(j),
						     static_cast<def::coord>(k)),
455
                                             geom::nslis(img) + 2 *
456
457
458
459
460
461
462
463
                                             img.border(),
                                             dpoint3d(1, 0, 0));
        }


        if (dir == 1)
        {
          // Apply on rows.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
464
465
          for (def::coord i = geom::min_sli(img); i <= geom::max_sli(img); ++i)
            for (def::coord k = geom::min_col(img); k <= geom::max_col(img); ++k)
466
              recursivefilter_<mln_value(I)>(img, coef,
467
468
469
470
471
                                             point3d(static_cast<def::coord>(i),
						     static_cast<def::coord>(-img.border()),
						     static_cast<def::coord>(k)),
                                             point3d(static_cast<def::coord>(i),
						     static_cast<def::coord>(geom::nrows(img) - 1 +
Guillaume Lazzara's avatar
Guillaume Lazzara committed
472
									     img.border()),
473
						     static_cast<def::coord>(k)),
474
475
476
477
478
479
480
481
                                             geom::nrows(img) + 2 *
                                             img.border(),
                                             dpoint3d(0, 1, 0));
        }

        if (dir == 2)
        {
          // Apply on columns.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
482
483
          for (def::coord i = geom::min_sli(img); i <= geom::max_sli(img); ++i)
            for (def::coord j = geom::min_row(img); j <= geom::max_row(img); ++j)
484
              recursivefilter_<mln_value(I)>(img, coef,
485
486
487
488
489
490
491
                                             point3d(static_cast<def::coord>(i),
						     static_cast<def::coord>(j),
						     static_cast<def::coord>(-img.border())),
					     point3d(static_cast<def::coord>(i),
						     static_cast<def::coord>(j),
						     static_cast<def::coord>(geom::ncols(img) -
									     1 + img.border())),
492
493
494
495
496
497
498
499
500
501
502
                                             geom::ncols(img) + 2 *
                                             img.border(),
                                             dpoint3d(0, 0, 1));
        }
      }



      template <class I, class F, class O>
      inline
      void
503
      generic_filter_common_(trait::value::nature::floating,
504
505
506
507
508
509
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out)
      {
	mln_ch_value(O, float) work_img(exact(in).domain());
510
	data::paste(in, work_img);
511
512
513
514
515
516
517
518
519
	extension::adjust_fill(work_img, 4, 0);

	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
	if (sigma > 0.006)
          for (int i = 0; i < I::site::dim; ++i)
            generic_filter_(mln_trait_image_dimension(I)(),
                            work_img, coef, i);

520
        // We don't need to convert work_img
521
	data::paste(work_img, out);
522
      }
523
524

      template <class I, class F, class O>
525
      inline
526
      void
527
      generic_filter_common_(trait::value::nature::floating,
528
529
530
531
532
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out,
                             int dir)
533
      {
534
	mln_ch_value(O, float) work_img(exact(in).domain());
535
	data::paste(in, work_img);
536
537
538
539
540
	extension::adjust_fill(work_img, 4, 0);

	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
	if (sigma > 0.006)
541
542
543
	  generic_filter_(mln_trait_image_dimension(I)(),
                          work_img, coef, dir);

544
        // We don't need to convert work_img
545
	data::paste(work_img, out);
546
547
548
      }


549
550
551
552
553
554
555
556
557
558
      template <class I, class F, class O>
      inline
      void
      generic_filter_common_(trait::value::nature::scalar,
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out)
      {
	mln_ch_value(O, float) work_img(exact(in).domain());
559
	data::paste(in, work_img);
560
561
562
563
564
565
566
567
568
569
	extension::adjust_fill(work_img, 4, 0);

	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
	if (sigma > 0.006)
          for (int i = 0; i < I::site::dim; ++i)
            generic_filter_(mln_trait_image_dimension(I)(),
                            work_img, coef, i);

        // Convert work_img into result type
570
	data::paste(data::stretch(mln_value(I)(), work_img), out);
571
572
573
574
575
576
577
578
579
580
581
582
583
      }

      template <class I, class F, class O>
      inline
      void
      generic_filter_common_(trait::value::nature::scalar,
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out,
                             int dir)
      {
	mln_ch_value(O, float) work_img(exact(in).domain());
584
	data::paste(in, work_img);
585
586
587
588
589
590
591
592
593
	extension::adjust_fill(work_img, 4, 0);

	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
	if (sigma > 0.006)
	  generic_filter_(mln_trait_image_dimension(I)(),
                          work_img, coef, dir);

        // Convert work_img into result type
594
	data::paste(data::stretch(mln_value(I)(), work_img), out);
595
596
597
      }


598

599
600
601
      template <class I, class F, class O>
      inline
      void
602
      generic_filter_common_(trait::value::nature::vectorial,
603
604
605
606
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out)
607
608
609
610
      {
        // typedef algebra::vec<3, float> vec3f;
        // mln_ch_value(O, vec3f) work_img(exact(in).domain());
        // FIXME : paste does not work (rgb8 -> vec3f).
611
        data::paste(in, out);
612

613
614
	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
615
	if (sigma > 0.006)
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
          for (int i = 0; i < I::site::dim; ++i)
            generic_filter_(mln_trait_image_dimension(I)(),
                            out, coef, i);
      }

      template <class I, class F, class O>
      inline
      void
      generic_filter_common_(trait::value::nature::vectorial,
                             const Image<I>& in,
                             const F& coef,
                             float sigma,
                             Image<O>& out,
                             int dir)
      {
        // typedef algebra::vec<3, float> vec3f;
        // mln_ch_value(O, vec3f) work_img(exact(in).domain());
        // FIXME : paste does not work (rgb8 -> vec3f).
634
        data::paste(in, out);
635
636
637
638
639
640

	// On tiny sigma, Derich algorithm doesn't work.
	// It is the same thing that to convolve with a Dirac.
	if (sigma > 0.006)
	  generic_filter_(mln_trait_image_dimension(I)(),
                          out, coef, dir);
641
      }
642

643
    } // end of namespace mln::linear::impl
644

645
646
    // Facade.

647
648
649
650
651
652
    /*! Apply an approximated gaussian filter of \p sigma on \p input.
     * on a specific direction \p dir
     * if \p dir = 0, the filter is applied on the first image dimension.
     * if \p dir = 1, the filter is applied on the second image dimension.
     * And so on...
     *
653
     * \pre input.is_valid
654
655
     * \pre dir < dimension(input)
     */
656
    template <typename I>
657
658
659
660
    inline
    mln_concrete(I)
    gaussian(const Image<I>& input, float sigma, int dir)
    {
661
      mln_precondition(exact(input).is_valid());
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
      mln_precondition(dir < I::site::dim);

      mln_concrete(I) output;
      initialize(output, input);
      impl::recursivefilter_coef_ coef(1.68f, 3.735f,
                                       1.783f, 1.723f,
                                       -0.6803f, -0.2598f,
                                       0.6318f, 1.997f,
                                       sigma, impl::gaussian_norm_coef_);

      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output, dir);
      return output;
    }


    /*! Apply an approximated first derivative gaussian filter of \p sigma on
     * \p input.
     * on a specific direction \p dir
     * if \p dir = 0, the filter is applied on the first image dimension.
     * if \p dir = 1, the filter is applied on the second image dimension.
     * And so on...
     *
685
     * \pre input.is_valid
686
687
     * \pre dir < dimension(input)
     */
688
    template <typename I>
689
690
691
692
    inline
    mln_concrete(I)
    gaussian_1st_derivative(const Image<I>& input, float sigma, int dir)
    {
693
      mln_precondition(exact(input).is_valid());
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
      mln_precondition(dir < I::site::dim);

      mln_concrete(I) output;
      initialize(output, input);

      impl::recursivefilter_coef_
        coef(-0.6472f, -4.531f,
             1.527f, 1.516f,
             0.6494f, 0.9557f,
             0.6719f, 2.072f,
             sigma, impl::gaussian_1st_deriv_coef_norm_);

      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output, dir);
      return output;
    }

    /*! Apply an approximated second derivative gaussian filter of \p sigma on
     * \p input.
     * on a specific direction \p dir
     * if \p dir = 0, the filter is applied on the first image dimension.
     * if \p dir = 1, the filter is applied on the second image dimension.
     * And so on...
     *
718
     * \pre input.is_valid
719
720
     * \pre dir < dimension(input)
     */
721
    template <typename I>
722
723
724
725
    inline
    mln_concrete(I)
    gaussian_2nd_derivative(const Image<I>& input, float sigma, int dir)
    {
726
      mln_precondition(exact(input).is_valid());
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
      mln_precondition(dir < I::site::dim);

      mln_concrete(I) output;
      initialize(output, input);

      impl::recursivefilter_coef_
        coef(-1.331f, 3.661f,
             1.24f, 1.314f,
             0.3225f, -1.738f,
             0.748f, 2.166f,
             sigma, impl::gaussian_2nd_deriv_coef_norm_);

      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output, dir);
      return output;
    }





    /*! Apply an approximated gaussian filter of \p sigma on \p input.
     * This filter is applied in all the input image direction.
     *
751
     * \pre input.is_valid
752
     */
753
    template <typename I>
754
    inline
Matthieu Garrigues's avatar
Matthieu Garrigues committed
755
756
    mln_concrete(I)
    gaussian(const Image<I>& input, float sigma)
757
    {
758
      mln_precondition(exact(input).is_valid());
Matthieu Garrigues's avatar
Matthieu Garrigues committed
759
760

      mln_concrete(I) output;
Jimmy Ma's avatar
Jimmy Ma committed
761
      initialize(output, input);
762

763
      impl::recursivefilter_coef_
764
765
766
767
	coef(1.68f, 3.735f,
	     1.783f, 1.723f,
	     -0.6803f, -0.2598f,
	     0.6318f, 1.997f,
768
769
770
771
772
773
774
	     sigma, impl::gaussian_norm_coef_);
      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output);

      return output;
    }

775
776
777
778
779

    /*! Apply an approximated first derivative gaussian filter of \p sigma on
     * \p input
     * This filter is applied in all the input image direction.
     *
780
     * \pre input.is_valid
781
     */
782
    template <typename I>
783
784
785
786
    inline
    mln_concrete(I)
    gaussian_1st_derivative(const Image<I>& input, float sigma)
    {
787
      mln_precondition(exact(input).is_valid());
788
789

      mln_concrete(I) output;
Jimmy Ma's avatar
Jimmy Ma committed
790
      initialize(output, input);
791
792
793
794
795
796
797
798
799
800
801
802

      impl::recursivefilter_coef_
        coef(-0.6472f, -4.531f,
             1.527f, 1.516f,
             0.6494f, 0.9557f,
             0.6719f, 2.072f,
             sigma, impl::gaussian_1st_deriv_coef_norm_);
      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output);
      return output;
    }

803
804
805
806
807

    /*! Apply an approximated second derivative gaussian filter of \p sigma on
     * \p input
     * This filter is applied in all the input image direction.
     *
808
     * \pre input.is_valid
809
     */
810
    template <typename I>
811
812
813
814
    inline
    mln_concrete(I)
    gaussian_2nd_derivative(const Image<I>& input, float sigma)
    {
815
      mln_precondition(exact(input).is_valid());
816
817

      mln_concrete(I) output;
Jimmy Ma's avatar
Jimmy Ma committed
818
      initialize(output, input);
Matthieu Garrigues's avatar
Matthieu Garrigues committed
819

820
821
822
823
824
825
826
827
      impl::recursivefilter_coef_
        coef(-1.331f, 3.661f,
             1.24f, 1.314f,
             0.3225f, -1.738f,
             0.748f, 2.166f,
             sigma, impl::gaussian_2nd_deriv_coef_norm_);
      impl::generic_filter_common_(mln_trait_value_nature(mln_value(I))(),
				   input, coef, sigma, output);
Matthieu Garrigues's avatar
Matthieu Garrigues committed
828
      return output;
829
830
831
832
833
834
835
836
837
    }

# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace mln::linear

} // end of namespace mln


838
#endif // ! MLN_LINEAR_GAUSSIAN_HH