File: MyImageStacker_SigmaReject.m

package info (click to toggle)
lynkeos.app 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,740 kB
  • sloc: objc: 36,412; ansic: 684; cpp: 148; sh: 68; makefile: 21
file content (406 lines) | stat: -rw-r--r-- 13,131 bytes parent folder | download
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
//
//  Lynkeos
//  $Id: MyImageStacker_SigmaReject.m 585 2018-09-08 21:30:37Z j-etienne $
//
//  Created by Jean-Etienne LAMIAUD on 03/01/11.
//  Copyright 2011-2014 Jean-Etienne LAMIAUD. All rights reserved.
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
#include <stdlib.h>
#include <objc/runtime.h>

#include "MyImageStacker_SigmaReject.h"

// Private (and temporary) parameter used to recombine the stacks
static NSString * const mySigmaRejectImageStackerResult
                           = @"SigmaRejectStackerResult";

/*!
 * @abstract Result for the sigma reject stacking strategy
 */
@interface SigmaRejectImageStackerResult : NSObject <LynkeosProcessingParameter,
                                             LynkeosMultiPassEnumeratorDelegate>
{
@public
   LynkeosStandardImageBuffer* _sum;          //!< Sum (all passes)
   LynkeosStandardImageBuffer* _sum2;         //!< square sum
   u_short                     _nStacked;     //!< Number of images in pass 1
   LynkeosStandardImageBuffer* _mean;         //!< Mean pixel value
   LynkeosStandardImageBuffer* _sigma;        //!< Standard deviation
   u_short*                    _count;        //!< Buffer of pixels count during pass2
   NSConditionLock*            _syncLock;     //!< Synchronisation barrier
}
@end

@interface MyImageStacker_SigmaReject(Private)
- (void) startNewPass ;
@end

@implementation SigmaRejectImageStackerResult
- (id) init
{
   self = [super init];
   if ( self != nil )
   {
      _sum = nil;
      _sum2 = nil;
      _nStacked = 0;
      _mean = nil;
      _sigma = nil;
      _count = NULL;
      _syncLock = [[NSConditionLock alloc] initWithCondition:0];
   }

   return( self );
}

- (void) dealloc
{
   if ( _sum != nil )
      [_sum release];
   if ( _sum2 != nil )
      [_sum2 release];
   if ( _mean != nil )
      [_mean release];
   if ( _sigma != nil )
      [_sigma release];
   if ( _count != NULL )
      free( _count );

   [super dealloc];
}

// This parameter is deleted at process end, it cannot be saved
- (void)encodeWithCoder:(NSCoder *)encoder
{
   [self doesNotRecognizeSelector:_cmd];
}
- (id)initWithCoder:(NSCoder *)decoder
{
   [self doesNotRecognizeSelector:_cmd];
   return( nil );
}

#pragma mark = LynkeosMultiPassEnumeratorDelegate protocol
- (BOOL) shouldPerformOneMorePass:(id<LynkeosMultiPassEnumerator>)enumerator
{
   return( [enumerator pass] == 1 );
}
@end

@implementation MyImageStacker_SigmaReject(Private)

- (void) startNewPass
{
   const u_short maxThread = ([MyImageStacker supportParallelization] ? numberOfCpus : 1);
   SigmaRejectImageStackerResult *res = (SigmaRejectImageStackerResult*)
      [_list getProcessingParameterWithRef:mySigmaRejectImageStackerResult
                             forProcessing:myImageStackerRef];
   REAL **p;
   u_short x, y, c;
   u_short nThread;

   NSAssert(res != nil, @"Nil temporary result in sigma reject stacker");

   [res->_syncLock lock];
   // Performed for each thread
   if ( _sum != nil )
   {
      // Recombine the stacks in the list
      if ( res->_sum == nil )
         res->_sum = [_sum retain];
      else
         [res->_sum add:_sum];
      [_sum release];
      _sum = nil;

      if ( res->_sum2 == nil )
         res->_sum2 = [_sum2 retain];
      else
         [res->_sum2 add:_sum2];
      [_sum2 release];
      _sum2 = nil;

      res->_nStacked += _nbStacked;
      _nbStacked = 0;
   }
   nThread = [res->_syncLock condition] + 1;

   // Performed after complete recombination
   if ( nThread == maxThread )
   {
      // Compute the mean
      REAL s = 1.0/(REAL)res->_nStacked;
      [res->_sum multiplyWithScalar:s];
      res->_mean = res->_sum;
      res->_sum = nil;

      // The variance
      [res->_sum2 multiplyWithScalar:s];
      LynkeosStandardImageBuffer *buf
         = [LynkeosStandardImageBuffer imageBufferWithNumberOfPlanes:
                                                            res->_mean->_nPlanes
                                                           width:res->_mean->_w
                                                          height:res->_mean->_h];
      [res->_mean multiplyWith:res->_mean result:buf];
      [res->_sum2 substract:buf];

      // Then the standard deviation from the variance
      p = (REAL**)[res->_sum2 colorPlanes];
      for( c = 0; c < res->_sum2->_nPlanes; c++ )
         for( y = 0; y < res->_sum2->_h; y++ )
            for( x = 0; x < res->_sum2->_w; x++ )
            {
               REAL v = sqrt(stdColorValue(res->_sum2, x, y, c));
               SET_SAMPLE(p[c], x, y, res->_sum2->_padw, v);
            }
      res->_sigma = res->_sum2;
      res->_sum2 = nil;

      // Finally, reset the enumerator
      [_params->_enumerator reset];
   }
   [res->_syncLock unlockWithCondition:nThread];

   NSAssert(nThread <= maxThread, @"More thread than maximum in sigma reject");

   if ( nThread != maxThread )
   {
      // Wait for complete recombination
      [res->_syncLock lockWhenCondition:maxThread];
      [res->_syncLock unlock];
   }
}

@end

@implementation MyImageStacker_SigmaReject

- (id) init
{
   if ( (self = [super init]) != nil )
   {
      _params = nil;
      _sum = nil;
      _sum2 = nil;
      _nbStacked = 0;
      _count = NULL;
      _list = nil;
   }

   return( self );
}

- (id) initWithParameters: (id <NSObject>)params
                     list: (id <LynkeosImageList>)list
{
   if ( (self = [self init]) != nil )
   {
      SigmaRejectImageStackerResult *res;

      NSAssert1( [params isMemberOfClass:[MyImageStackerParameters class]],
                @"Wrong parameter class %s for Image stacker (sigma reject)",
                class_getName([params class]) );
      _params = (MyImageStackerParameters*)[params retain];
      _list = list;

      // First thread initialization
      [_params->_stackLock lock];

      res = (SigmaRejectImageStackerResult*)
         [_list getProcessingParameterWithRef:mySigmaRejectImageStackerResult
                                forProcessing:myImageStackerRef];
      if ( res == nil )
      {
         res = [[[SigmaRejectImageStackerResult alloc] init] autorelease];

         [_list setProcessingParameter:res
                               withRef:mySigmaRejectImageStackerResult
                         forProcessing:myImageStackerRef];

         // It cannot be predicted which thread will finish last, therefore
         // the parameter is the delegate, as it lives as long as the longest
         [_params->_enumerator setDelegate:res];
      }
      [_params->_stackLock unlock];
   }
   
   return( self );
}

- (void) dealloc
{
   if ( _params != nil )
      [_params release];
   if ( _sum != nil )
      [_sum release];
   if ( _sum2 != nil )
      [_sum2 release];
   if ( _count != NULL )
      free( _count );

   [super dealloc];
}

- (void) processImage: (id <LynkeosImageBuffer>)image
{
   // Take into account the end of pass
   if ( [image isKindOfClass:[NSNull class]] )
   {
      [self startNewPass];
   }
   else
   {
      NSAssert( _sum == nil || _sum->_nPlanes == [image numberOfPlanes],
               @"heterogeneous planes numbers in sigma reject stacking" );

      // Extract the data in a local image buffer
      LynkeosStandardImageBuffer *buf
         = [LynkeosStandardImageBuffer imageBufferWithNumberOfPlanes: [image numberOfPlanes]
                                                               width: [image width]
                                                              height: [image height]];
      [image convertToPlanar:[buf colorPlanes] withPlanes:buf->_nPlanes lineWidth:buf->_padw];

      // If this is the first image, create the empty stack buffer with the same
      // number of planes (taking into account the expansion factor)
      if ( _sum == nil )
         _sum = [[LynkeosStandardImageBuffer imageBufferWithNumberOfPlanes: buf->_nPlanes
                                                                     width: buf->_w
                                                                    height: buf->_h]
                 retain];


      if ( [_params->_enumerator pass] == 1 )
      {
         // Allocate the square sum buffer if needed
         if ( _sum2 == nil )
            _sum2 = [[LynkeosStandardImageBuffer imageBufferWithNumberOfPlanes: buf->_nPlanes
                                                                         width: buf->_w
                                                                        height: buf->_h]
                     retain];

         // Accumulate
         [_sum add:buf];

         // And accumulate the square values
         [buf multiplyWith:buf result:buf];
         [_sum2 add:buf];
         _nbStacked ++;
      }
      else
      {
         u_short x, y, c;
         REAL **p = (REAL**)[_sum colorPlanes];
         SigmaRejectImageStackerResult *res
            = (SigmaRejectImageStackerResult*)[_list getProcessingParameterWithRef:
                                                                               mySigmaRejectImageStackerResult
                                                                     forProcessing:myImageStackerRef];

         // Allocate the count buffer if needed
         if ( _count == NULL )
            _count = (u_short*)calloc( buf->_nPlanes*buf->_w*buf->_h,
                                      sizeof(u_short) );

         // Perform pixel addition only when below the standard deviation threshold
         for( c = 0; c < buf->_nPlanes; c++ )
         {
            for( y = 0; y < buf->_h; y++ )
            {
               for( x = 0; x < buf->_w; x++ )
               {
                  REAL v = stdColorValue(buf, x, y, c);
                  REAL m = stdColorValue(res->_mean, x, y, c);
                  REAL s = stdColorValue(res->_sigma, x, y, c);
                  if ( fabs(v - m) <= s*_params->_method.sigma.threshold )
                  {
                     v += stdColorValue(_sum, x, y, c);
                     SET_SAMPLE(p[c], x, y, _sum->_padw, v);
                     _count[(c*buf->_h + y)*buf->_w + x]++;
                  }
               }
            }
         }
      }
   }
}

- (void) finishOneProcessingThreadInList:(id <LynkeosImageList>)list ;
{
   if ( _sum != nil )
   {
      u_short x, y, c;

      // Recombine the stacks in the list
      SigmaRejectImageStackerResult *res = (SigmaRejectImageStackerResult*)
         [list getProcessingParameterWithRef:mySigmaRejectImageStackerResult
                               forProcessing:myImageStackerRef];
      NSAssert(res != nil,
               @"Nil temporary result in sigma reject last recombining");

      if ( res->_sum == nil )
         res->_sum = [_sum retain];
      else
         [res->_sum add:_sum];

      if ( res->_count == NULL )
         res->_count = (u_short*)calloc(_sum->_nPlanes*_sum->_w*_sum->_h,
                                        sizeof(u_short));

      for( c = 0; c < _sum->_nPlanes; c++ )
         for( y = 0; y < _sum->_h; y++ )
            for( x = 0; x < _sum->_w; x++ )
               res->_count[(c*_sum->_h + y)*_sum->_w + x] +=
                                          _count[(c*_sum->_h + y)*_sum->_w + x];
   }
}

- (void) finishAllProcessingInList: (id <LynkeosImageList>)list;
{
   REAL **p;
   u_short x, y, c;

   // Calculate the stats
   SigmaRejectImageStackerResult *res = (SigmaRejectImageStackerResult*)
      [list getProcessingParameterWithRef:mySigmaRejectImageStackerResult
                            forProcessing:myImageStackerRef];
   NSAssert( res != nil, @"No stacking result at sigma reject pass end" );

   // Compute the second pass mean, and store it
   p = (REAL**)[res->_sum colorPlanes];
   for( c = 0; c < res->_sum->_nPlanes; c++ )
      for( y = 0; y < res->_sum->_h; y++ )
         for( x = 0; x < res->_sum->_w; x++ )
         {
            REAL v;
            u_short n = res->_count[(c*res->_sum->_h + y)*res->_sum->_w + x];
            if ( n == 0 )
               v = 0.0;
            else
               v = stdColorValue(res->_sum, x, y, c)
                   / (REAL)n;
            SET_SAMPLE(p[c], x, y, res->_sum->_padw, v);
         }
   if ( _sum != nil )
      [_sum release];
   _sum = [res->_sum retain];

   // And get rid of the recombining parameter
   [list setProcessingParameter:nil withRef:mySigmaRejectImageStackerResult
                  forProcessing:myImageStackerRef];
}

- (LynkeosStandardImageBuffer*) stackingResult { return( _sum ); }
@end