OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimDiscreteConvolutionKernel.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts
6 //
7 //*******************************************************************
8 // $Id: ossimDiscreteConvolutionKernel.cpp 12912 2008-05-28 15:05:54Z gpotts $
10 
11 
13  long height,
14  bool doWeightedAverage)
15  : theKernel(0),
16  theWidth(width),
17  theHeight(height),
18  theComputeWeightedAverageFlag(doWeightedAverage)
19 {
20  theWidth = theWidth <0?1:theWidth;
22 
23  theKernel = new NEWMAT::Matrix(theHeight, theWidth);
24  *theKernel = (1.0/(theHeight*theWidth));
25 }
26 
28  bool doWeightedAverage)
29  :theKernel(new NEWMAT::Matrix(kernel)),
30  theComputeWeightedAverageFlag(doWeightedAverage)
31 {
32  theWidth = theKernel->Ncols();
33  theHeight = theKernel->Nrows();
34 }
35 
37 {
38  if(theKernel)
39  {
40  delete theKernel;
41  theKernel = 0;
42  }
43 }
44 
45 void ossimDiscreteConvolutionKernel::buildSymmetric(const std::vector<float>& coefficients,
46  NEWMAT::Matrix& result)
47 {
48  ossim_uint32 size = (ossim_uint32)coefficients.size();
49  if(size > 0)
50  {
51 
52  NEWMAT::RowVector row(size);
53  NEWMAT::ColumnVector col(size);
54  for(ossim_uint32 index = 0; index < size; ++index)
55  {
56  row[index] = coefficients[index];
57  col[index] = coefficients[index];
58  }
59  result = col*row;
60  }
61 }
62 
64  long size,
65  NEWMAT::Matrix& result)
66 {
67  if(size > 0)
68  {
69  NEWMAT::RowVector row(size);
70  NEWMAT::ColumnVector col(size);
71  for(long index = 0; index < size; ++index)
72  {
73  row[index] = coefficients[index];
74  col[index] = coefficients[index];
75  }
76  result = col*row;
77  }
78 }
79 
80 void ossimDiscreteConvolutionKernel::setKernel(const NEWMAT::Matrix& kernel)
81 {
82  if(theKernel)
83  {
84  theKernel = new NEWMAT::Matrix;
85  }
86  *theKernel = kernel;
87  theWidth = theKernel->Ncols();
88  theHeight = theKernel->Nrows();
89 }
90 
92  double& result,
93  float nullPixel)const
94 {
95  result = 0;
96 
98  {
99  double divisor = 0.0;
100  for(long row=0; row < theHeight; ++row)
101  {
102  for(long col=0; col < theWidth; ++col)
103  {
104  if(*data != nullPixel)
105  {
106  divisor += (*theKernel)[row][col];
107  result += ((*theKernel)[row][col] * (*data));
108  }
109  ++data;
110  }
111  }
112  if(divisor > 0)
113  {
114  result /= divisor;
115  }
116  }
117  else
118  {
119  for(long row=0; row < theHeight; ++row)
120  {
121  for(long col=0; col < theWidth; ++col)
122  {
123  if(*data != nullPixel)
124  {
125  result += ((*theKernel)[row][col] * (*data));
126  }
127  ++data;
128  }
129  }
130  }
131 }
132 
134  long dataWidth,
135  double& result,
136  float nullPixel)const
137 {
138  const float* dataStart = 0;
139  result = 0;
140 
142  {
143  double divisor = 0.0;
144  for(long row=0; row < theHeight; ++row)
145  {
146  dataStart=data;
147  for(long col=0; col < theWidth; ++col)
148  {
149  if(*dataStart != nullPixel)
150  {
151  divisor += (*theKernel)[row][col];
152  result += ((*theKernel)[row][col] * (*dataStart));
153  }
154  ++dataStart;
155  }
156  data+=dataWidth;
157  }
158  if(divisor > 0)
159  {
160  result /= divisor;
161  }
162  }
163  else
164  {
165  for(long row=0; row < theHeight; ++row)
166  {
167  dataStart=data;
168  for(long col=0; col < theWidth; ++col)
169  {
170  if(*dataStart != nullPixel)
171  {
172  result += ((*theKernel)[row][col] * (*dataStart));
173  }
174  ++dataStart;
175  }
176  data+=dataWidth;
177  }
178  }
179 }
180 
181 void ossimDiscreteConvolutionKernel::convolve(const unsigned char* data,
182  double& result,
183  ossim_uint8 nullPixel)const
184 {
185  result = 0;
186 
188  {
189  double divisor = 0.0;
190  for(long row=0; row < theHeight; ++row)
191  {
192  for(long col=0; col < theWidth; ++col)
193  {
194  if(*data != nullPixel)
195  {
196  divisor += (*theKernel)[row][col];
197  result += ((*theKernel)[row][col] * (*data));
198  }
199  ++data;
200  }
201  }
202  if(divisor > 0)
203  {
204  result /= divisor;
205  }
206  }
207  else
208  {
209  for(long row=0; row < theHeight; ++row)
210  {
211  for(long col=0; col < theWidth; ++col)
212  {
213  if(*data != nullPixel)
214  {
215  result += ((*theKernel)[row][col] * (*data));
216  }
217  ++data;
218  }
219  }
220  }
221 }
222 
224  long dataWidth,
225  double& result,
226  ossim_uint8 nullPixel)const
227 {
228  const ossim_uint8* dataStart = 0;
229  result = 0;
230 
232  {
233  double divisor = 0.0;
234  for(long row=0; row < theHeight; ++row)
235  {
236  dataStart=data;
237  for(long col=0; col < theWidth; ++col)
238  {
239  if(*dataStart != nullPixel)
240  {
241  divisor += (*theKernel)[row][col];
242  result += ((*theKernel)[row][col] * (*dataStart));
243  }
244  ++dataStart;
245  }
246  data+=dataWidth;
247  }
248  if(divisor > 0)
249  {
250  result /= divisor;
251  }
252  }
253  else
254  {
255  for(long row=0; row < theHeight; ++row)
256  {
257  dataStart=data;
258  for(long col=0; col < theWidth; ++col)
259  {
260  if(*dataStart != nullPixel)
261  {
262  result += ((*theKernel)[row][col] * (*dataStart));
263  }
264  ++dataStart;
265  }
266  data+=dataWidth;
267  }
268  }
269 }
270 
271 
272 void ossimDiscreteConvolutionKernel::convolve(const unsigned short* data,
273  double& result,
274  ossim_uint16 nullPixel)const
275 {
276  result = 0;
277 
279  {
280  double divisor = 0.0;
281  for(long row=0; row < theHeight; ++row)
282  {
283  for(long col=0; col < theWidth; ++col)
284  {
285  if(*data != nullPixel)
286  {
287  divisor += (*theKernel)[row][col];
288  result += ((*theKernel)[row][col] * (*data));
289  }
290  ++data;
291  }
292  }
293  if(divisor > 0)
294  {
295  result /= divisor;
296  }
297  }
298  else
299  {
300  for(long row=0; row < theHeight; ++row)
301  {
302  for(long col=0; col < theWidth; ++col)
303  {
304  if(*data != nullPixel)
305  {
306  result += ((*theKernel)[row][col] * (*data));
307  }
308  ++data;
309  }
310  }
311  }
312 }
313 
314 void ossimDiscreteConvolutionKernel::convolveSubImage(const unsigned short* data,
315  long dataWidth,
316  double& result,
317  ossim_uint16 nullPixel)const
318 {
319  const ossim_uint16* dataStart = 0;
320  result = 0;
321 
323  {
324  double divisor = 0.0;
325  for(long row=0; row < theHeight; ++row)
326  {
327  dataStart=data;
328  for(long col=0; col < theWidth; ++col)
329  {
330  if(*dataStart != nullPixel)
331  {
332  divisor += (*theKernel)[row][col];
333  result += ((*theKernel)[row][col] * (*dataStart));
334  }
335  ++dataStart;
336  }
337  data+=dataWidth;
338  }
339  if(divisor > 0)
340  {
341  result /= divisor;
342  }
343  }
344  else
345  {
346  for(long row=0; row < theHeight; ++row)
347  {
348  dataStart=data;
349  for(long col=0; col < theWidth; ++col)
350  {
351  if(*dataStart != nullPixel)
352  {
353  result += ((*theKernel)[row][col] * (*dataStart));
354  }
355  ++dataStart;
356  }
357  data+=dataWidth;
358  }
359  }
360 }
361 
363  double& result,
364  ossim_sint16 nullPixel)const
365 {
366  result = 0;
367 
369  {
370  double divisor = 0.0;
371  for(long row=0; row < theHeight; ++row)
372  {
373  for(long col=0; col < theWidth; ++col)
374  {
375  if(*data != nullPixel)
376  {
377  divisor += (*theKernel)[row][col];
378  result += ((*theKernel)[row][col] * (*data));
379  }
380  ++data;
381  }
382  }
383  if(divisor > 0)
384  {
385  result /= divisor;
386  }
387  }
388  else
389  {
390  for(long row=0; row < theHeight; ++row)
391  {
392  for(long col=0; col < theWidth; ++col)
393  {
394  if(*data != nullPixel)
395  {
396  result += ((*theKernel)[row][col] * (*data));
397  }
398  ++data;
399  }
400  }
401  }
402 }
403 
405  long dataWidth,
406  double& result,
407  ossim_sint16 nullPixel)const
408 {
409  const ossim_sint16* dataStart = 0;
410  result = 0;
411 
413  {
414  double divisor = 0.0;
415  for(long row=0; row < theHeight; ++row)
416  {
417  dataStart=data;
418  for(long col=0; col < theWidth; ++col)
419  {
420  if(*dataStart != nullPixel)
421  {
422  divisor += (*theKernel)[row][col];
423  result += ((*theKernel)[row][col] * (*dataStart));
424  }
425  ++dataStart;
426  }
427  data+=dataWidth;
428  }
429  if(divisor > 0)
430  {
431  result /= divisor;
432  }
433  }
434  else
435  {
436  for(long row=0; row < theHeight; ++row)
437  {
438  dataStart=data;
439  for(long col=0; col < theWidth; ++col)
440  {
441  if(*dataStart != nullPixel)
442  {
443  result += ((*theKernel)[row][col] * (*dataStart));
444  }
445  ++dataStart;
446  }
447  data+=dataWidth;
448  }
449  }
450 }
451 
452 
454  double& result,
455  double nullPixel)const
456 {
457  result = 0;
458 
460  {
461  double divisor = 0.0;
462  for(long row=0; row < theHeight; ++row)
463  {
464  for(long col=0; col < theWidth; ++col)
465  {
466  if(*data != nullPixel)
467  {
468  divisor += (*theKernel)[row][col];
469  result += ((*theKernel)[row][col] * (*data));
470  }
471  ++data;
472  }
473  }
474  if(divisor > 0)
475  {
476  result /= divisor;
477  }
478  }
479  else
480  {
481  for(long row=0; row < theHeight; ++row)
482  {
483  for(long col=0; col < theWidth; ++col)
484  {
485  if(*data != nullPixel)
486  {
487  result += ((*theKernel)[row][col] * (*data));
488  }
489  ++data;
490  }
491  }
492  }
493 }
494 
496  long dataWidth,
497  double& result,
498  double nullPixel)const
499 {
500  const double* dataStart = 0;
501  result = 0;
502 
504  {
505  double divisor = 0.0;
506  for(long row=0; row < theHeight; ++row)
507  {
508  dataStart=data;
509  for(long col=0; col < theWidth; ++col)
510  {
511  if(*dataStart != nullPixel)
512  {
513  divisor += (*theKernel)[row][col];
514  result += ((*theKernel)[row][col] * (*dataStart));
515  }
516  ++dataStart;
517  }
518  data+=dataWidth;
519  }
520  if(divisor > 0)
521  {
522  result /= divisor;
523  }
524  }
525  else
526  {
527  for(long row=0; row < theHeight; ++row)
528  {
529  dataStart=data;
530  for(long col=0; col < theWidth; ++col)
531  {
532  if(*dataStart != nullPixel)
533  {
534  result += ((*theKernel)[row][col] * (*dataStart));
535  }
536  ++dataStart;
537  }
538  data+=dataWidth;
539  }
540  }
541 }
virtual void setKernel(const NEWMAT::Matrix &kernel)
unsigned short ossim_uint16
yy_size_t size
signed short ossim_sint16
unsigned int ossim_uint32
Definition: newmat.h:543
virtual void convolve(const float *data, double &result, float nullPixel=OSSIM_DEFAULT_NULL_PIX_FLOAT) const
static void buildSymmetric(const std::vector< float > &coefficients, NEWMAT::Matrix &result)
ossimDiscreteConvolutionKernel(long width, long height, bool doWeightedAverage=true)
virtual void convolveSubImage(const float *data, long dataWidth, double &result, float nullPixel=OSSIM_DEFAULT_NULL_PIX_FLOAT) const
unsigned char ossim_uint8