integral_single_image.hh 12.2 KB
Newer Older
1
2
// Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
// (LRDE)
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
//
// This file is part of Olena.
//
// 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,
// 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 Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// 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.

#ifndef SCRIBO_SUBSAMPLING_INTEGRAL_HH
# define SCRIBO_SUBSAMPLING_INTEGRAL_HH

/// \file
///
/// Both subsampling and integral image computation.

#include <mln/core/concept/image.hh>
#include <mln/metal/equal.hh>
#include <mln/extension/fill.hh>
#include <mln/debug/println.hh>
#include <mln/debug/println_with_border.hh>



namespace scribo
{

  namespace subsampling
  {
    using namespace mln;


    /*! \brief Subsample an image and compute tow integral images.

      \param[in] input An image of Scalar.
      \param[in] scale The scale factor.
      \param[in] integral_sum Integral image of mean values.
      \param[in] integral_sum_2 Integral image of squared mean values.
      \param[in] output_domain The domain of the subscaled image.
      \param[in] border_thickness Border of the integral and subscaled images.

      \p integral_sum, \p integral_sum_2 and output image have the same domain.
      The output domain is set with \p output_domain.

     */
    template <typename I, typename J>
    inline
    mln_concrete(I)
    integral(const Image<I>& input, unsigned scale,
	     Image<J>& integral_sum, Image<J>& integral_sum_2,
	     const mln_domain(I)& output_domain, unsigned border_thickness);

    /*! \overload

      The output domain is defined as follows :

      for_each(dimension)
        input.domain()[dimension] = (N + scale - 1) / scale

      where N is the number of elements in one run in a given dimension.

      The border thickness is set to mln::border::thickness.
     */
    template <typename I, typename J>
    inline
    mln_concrete(I)
    integral(const Image<I>& input, unsigned scale,
	     Image<J>& integral_sum, Image<J>& integral_sum_2);



# ifndef MLN_INCLUDE_ONLY


    // Implementation.

    namespace impl
    {

      template <typename I, typename J>
      inline
      mln_concrete(I)
      integral_3(const Image<I>& input_,
		 Image<J>& integral_sum_sum_2_,
		 const mln_domain(I)& output_domain,
		 unsigned border_thickness)
      {
	trace::entering("subsampling::impl::integral_3");

	const unsigned scale = 3;

	const I& input = exact(input_);
	J& integral_sum_sum_2 = exact(integral_sum_sum_2_);

	mln_precondition(input.is_valid());
	mln_precondition(input.domain().pmin() == literal::origin);
	mln_precondition(scale > 1);

	typedef mln_value(I) V;
	typedef mln_sum(V) S;
	typedef mln_value(J) V2;
	typedef mln_site(I) P;

	mln_concrete(I) sub(output_domain, border_thickness);
	V* p_sub = sub.buffer();

	integral_sum_sum_2.init_(output_domain, border_thickness);
	V2* p_integ = integral_sum_sum_2.buffer();

127
	const int up = sub.delta_index(dpoint2d(-1, 0));
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

	const unsigned nrows = 3 * output_domain.nrows();
	const unsigned ncols = 3 * output_domain.ncols();

	unsigned row = 0;

	unsigned b_offset = sub.delta_index(dpoint2d(border_thickness,
						     border_thickness));
	p_sub += b_offset;
	p_integ += b_offset;
	{
	  S h_sum = 0, h_sum_2 = 0;
	  const V* ptr1 = & input.at_(row, 0);
	  const V* ptr2 = & input.at_(row + 1, 0);
	  const V* ptr3 = & input.at_(row + 2, 0);
	  for (unsigned col = 0; col < ncols; col += scale)
	  {
145
146
147
	    V v11 = *ptr1, v12 = *(ptr1 + 1), v13 = *(ptr1 + 2),
	      v21 = *ptr2, v22 = *(ptr2 + 1), v23 = *(ptr2 + 2),
	      v31 = *ptr3, v32 = *(ptr3 + 1), v33 = *(ptr3 + 2);
148
149
150
	    ptr1 += 3;
	    ptr2 += 3;
	    ptr3 += 3;
151
152
153
154
155
156
	    S local_sum   = v11     + v12     + v13
	                  + v21     + v22     + v23
	                  + v31     + v32     + v33,
	      local_sum_2 = v11*v11 + v12*v12 + v13*v13
	                  + v21*v21 + v22*v22 + v23*v23
	                  + v31*v31 + v32*v32 + v33*v33;
157

Guillaume Lazzara's avatar
Guillaume Lazzara committed
158
	    mln::convert::from_to(local_sum / 9, *p_sub++);
159
160
	    h_sum   += local_sum;
	    h_sum_2 += local_sum_2;
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

	    // exception
	    p_integ->first() = h_sum;
	    p_integ->second() = h_sum_2;

	    p_integ += 1;
	  }
	}

 	unsigned b_next = 2 * border_thickness;

	p_sub += b_next;
	p_integ += b_next;

	for (row += scale; row < nrows; row += scale)
	{
	  S h_sum = 0, h_sum_2 = 0;
178
    	  const V* ptr1 = & input.at_(row, 0);
179
180
181
182
	  const V* ptr2 = & input.at_(row + 1, 0);
	  const V* ptr3 = & input.at_(row + 2, 0);
	  for (unsigned col = 0; col < ncols; col += scale)
	  {
183
184
185
	    V v11 = *ptr1, v12 = *(ptr1 + 1), v13 = *(ptr1 + 2),
	      v21 = *ptr2, v22 = *(ptr2 + 1), v23 = *(ptr2 + 2),
	      v31 = *ptr3, v32 = *(ptr3 + 1), v33 = *(ptr3 + 2);
186
187
188
	    ptr1 += 3;
	    ptr2 += 3;
	    ptr3 += 3;
189
190
191
192
193
194
	    S local_sum   = v11     + v12     + v13
	                  + v21     + v22     + v23
	                  + v31     + v32     + v33,
	      local_sum_2 = v11*v11 + v12*v12 + v13*v13
	                  + v21*v21 + v22*v22 + v23*v23
	                  + v31*v31 + v32*v32 + v33*v33;
195

Guillaume Lazzara's avatar
Guillaume Lazzara committed
196
	    mln::convert::from_to(local_sum / 9, *p_sub++);
197
198
	    h_sum   += local_sum;
	    h_sum_2 += local_sum_2;
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

	    p_integ->first() = h_sum + (p_integ + up)->first();
	    p_integ->second() = h_sum_2 + (p_integ + up)->second();

	    p_integ += 1;
	  }

	  p_sub += b_next;
	  p_integ += b_next;
	}

	trace::exiting("subsampling::impl::integral_3");
	return sub;
      }


      template <typename I, typename J>
      inline
      mln_concrete(I)
      integral_2(const Image<I>& input_,
		 Image<J>& integral_sum_sum_2_,
		 const mln_domain(I)& output_domain,
		 unsigned border_thickness)
      {
	trace::entering("subsampling::impl::integral_2");

	const unsigned scale = 2;

	const I& input = exact(input_);
	J& integral_sum_sum_2 = exact(integral_sum_sum_2_);

	typedef mln_value(I) V;
	typedef mln_sum(V) S;
	typedef mln_site(I) P;
	typedef mln_value(J) V2;

	mlc_bool(P::dim == 2)::check();
	mln_precondition(input.is_valid());
	mln_precondition(input.domain().pmin() == literal::origin);
	mln_precondition(scale > 1);

	mln_concrete(I) sub(output_domain, border_thickness);
	V* p_sub = sub.buffer();

	integral_sum_sum_2.init_(output_domain, border_thickness);
	V2* p_integ = integral_sum_sum_2.buffer();

246
	const int up = sub.delta_index(dpoint2d(-1, 0));
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

	const unsigned nrows = 2 * output_domain.nrows();
	const unsigned ncols = 2 * output_domain.ncols();

  	extension::fill(sub, 0);

	unsigned b_offset = sub.delta_index(dpoint2d(border_thickness,
						     border_thickness));
	p_sub += b_offset;
	p_integ += b_offset;

	unsigned row = 0;
	{
	  S h_sum = 0, h_sum_2 = 0;
	  const V* ptr1 = & input.at_(row, 0);
	  const V* ptr2 = & input.at_(row + 1, 0);
	  for (unsigned col = 0; col < ncols; col += scale)
	  {
265
/*
266
267
268
269
270
271
272
273
274
275
276
	    S sum;
	    sum  = *ptr1 + *(ptr1 + 1);
	    sum += *ptr2 + *(ptr2 + 1);
	    ptr1 += 2;
	    ptr2 += 2;

	    S val = sum / area;
	    *p_sub++ = val;

	    h_sum   += val;
	    h_sum_2 += val * val;
277
278
279
280
281
282
283
284
285
286
*/

	    // NEW:

	    V v11 = *ptr1, v12 = *(ptr1 + 1),
	      v21 = *ptr2, v22 = *(ptr2 + 1);
	    ptr1 += 2;
	    ptr2 += 2;
	    S local_sum   = v11     + v12     + v21     + v22,
	      local_sum_2 = v11*v11 + v12*v12 + v21*v21 + v22*v22;
Guillaume Lazzara's avatar
Guillaume Lazzara committed
287
	    mln::convert::from_to(local_sum / 4, *p_sub++);
288
289
290
291
292
	    h_sum   += local_sum;
	    h_sum_2 += local_sum_2;

	    // end of NEW.

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

	    // exception
	    p_integ->first() = h_sum;
	    p_integ->second() = h_sum_2;

	    p_integ += 1;
	  }
	}

 	unsigned b_next = 2 * border_thickness;

	p_sub += b_next;
	p_integ += b_next;

	for (row += scale; row < nrows; row += scale)
	{
	  S h_sum = 0, h_sum_2 = 0;
	  const V* ptr1 = & input.at_(row, 0);
	  const V* ptr2 = & input.at_(row + 1, 0);
	  for (unsigned col = 0; col < ncols; col += scale)
	  {
314
315
316
317
318
319
320
321
	    // NEW:

	    V v11 = *ptr1, v12 = *(ptr1 + 1),
	      v21 = *ptr2, v22 = *(ptr2 + 1);
	    ptr1 += 2;
	    ptr2 += 2;
	    S local_sum   = v11     + v12     + v21     + v22,
	      local_sum_2 = v11*v11 + v12*v12 + v21*v21 + v22*v22;
Guillaume Lazzara's avatar
Guillaume Lazzara committed
322
	    mln::convert::from_to(local_sum / 4, *p_sub++);
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	    h_sum   += local_sum;
	    h_sum_2 += local_sum_2;

	    // end of NEW.


	    /*

	    // To get the strict equivalent to the integral image
            // computed at working scale (scale (2)), we need the code
            // below.  In addition, the integral_browsing shall call
            // the threshold formula with (..size..) and NOT with
	    // (..size * s_2..).

337
338
339
340
341
342
343
344
345
346
347
	    S sum;
	    sum  = *ptr1 + *(ptr1 + 1);
	    sum += *ptr2 + *(ptr2 + 1);
	    ptr1 += 2;
	    ptr2 += 2;

	    S val = sum / area;
	    *p_sub++ = val;
	    h_sum   += val;
	    h_sum_2 += val * val;

348
349
350
351
352
353
354
355
356
357
358
359
360
	    */


            // Never write something like this:
            //   *p_sub++ = sum / area;
	    //   h_sum   += sum;
            //   h_sum_2 += sum * sum;
	    // because the product 'sum * sum' is not
            // equivalent to the sum of the value^2.  E.g.
            // we have (v1 + v2 + v3 + v4)^2 + etc. instead
            // of having the correct sum_2 being v1^2 + v2^2 etc.


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	    p_integ->first() = h_sum + (p_integ + up)->first();
	    p_integ->second() = h_sum_2 + (p_integ + up)->second();

	    p_integ += 1;
	  }

	  p_sub += b_next;
	  p_integ += b_next;
	}

	trace::exiting("subsampling::impl::integral_2");
	return sub;
      }


      template <typename I, typename J>
      inline
      mln_concrete(I)
      integral(const Image<I>& input, unsigned scale,
	       Image<J>& integral_sum_sum_2,
	       const mln_domain(I)& output_domain, unsigned border_thickness)
      {
	//       mln_precondition(input.nrows() % scale == 0);
	//       mln_precondition(input.ncols() % scale == 0);
	if (scale == 3)
	  return integral_3(input, integral_sum_sum_2,
			    output_domain, border_thickness);
	else if (scale == 2)
	  return integral_2(input, integral_sum_sum_2,
			    output_domain, border_thickness);
	else
	  std::cerr << "NYI!" << std::endl;

	typedef mln_concrete(I) output_t;
	return output_t();
      }


    } // end of namespace mln::subsampling::impl




    // Facades.

    template <typename I, typename J>
    inline
    mln_concrete(I)
    integral(const Image<I>& input_, unsigned scale,
	     Image<J>& integral_sum_sum_2,
	     const mln_domain(I)& output_domain, unsigned border_thickness)
    {
      trace::entering("subsampling::integral");

      const I& input = exact(input_);

      mln_precondition(input.is_valid());
      mln_precondition(input.domain().pmin() == literal::origin);
      mln_precondition(scale > 1);

      mln_concrete(I)
	output = impl::integral(input, scale, integral_sum_sum_2,
				output_domain, border_thickness);

      trace::exiting("subsampling::integral");
      return output;
    }

    template <typename I, typename J>
    inline
    mln_concrete(I)
    integral(const Image<I>& input_, unsigned scale,
	     Image<J>& integral_sum_sum_2)
    {
      trace::entering("subsampling::integral");

      const I& input = exact(input_);

      mln_precondition(input.is_valid());
      mln_precondition(input.domain().pmin() == literal::origin);
      mln_precondition(scale > 1);

      box<mln_site(I)> b = make::box2d((input.nrows() + scale - 1) / scale,
				       (input.ncols() + scale - 1) / scale);
      mln_concrete(I) output;
      output = integral(input_, scale, integral_sum_sum_2,
			b, mln::border::thickness);

      trace::exiting("subsampling::integral");
      return output;
    }

# endif // ! MLN_INCLUDE_ONLY

  } // end of namespace scribo::subsampling

} // end of namespace scribo


#endif // ! SCRIBO_SUBSAMPLING_INTEGRAL_HH