Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageCalc.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "ImageCalc.hh"
26 
27 using namespace std;
28 using namespace BIAS;
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 // calculations between two images //
32 ////////////////////////////////////////////////////////////////////////////////
33 
34 template<class StorageType>
36  const Image<StorageType> &in2,
37  Image<StorageType> &out) {
38  // assert same size
39  BIASASSERT( in1.GetWidth() == in2.GetWidth()
40  && in1.GetHeight() == in2.GetHeight()
41  && in1.GetChannelCount() == in2.GetChannelCount())
42 
43  // init output image
44  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount(),
45  in1.GetStorageType());
46 
47  // get pointers to data
48  const StorageType *in1Data = in1.GetImageData();
49  const StorageType *in2Data = in2.GetImageData();
50  StorageType *outData = out.GetImageData();
51 
52  const unsigned int size = in1.GetWidth()
53  * in1.GetHeight()
54  * in1.GetChannelCount();
55 
56  // perform operation
57  for (unsigned int i = 0; i < size; i++) {
58  outData[i] = in1Data[i] + in2Data[i];
59  }
60 }
61 
62 template<class StorageType>
64  const Image<StorageType> &in2,
65  Image<StorageType> &out) {
66  // assert same size
67  BIASASSERT( in1.GetWidth() == in2.GetWidth()
68  && in1.GetHeight() == in2.GetHeight()
69  && in1.GetChannelCount() == in2.GetChannelCount())
70 
71  // init output image
72  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
73 
74  // get pointers to data
75  const StorageType *in1Data = in1.GetImageData();
76  const StorageType *in2Data = in2.GetImageData();
77  StorageType *outData = out.GetImageData();
78 
79  const unsigned int size = in1.GetWidth()
80  * in1.GetHeight()
81  * in1.GetChannelCount();
82 
83  // perform operation
84  for (unsigned int i = 0; i < size; i++) {
85  outData[i] = in1Data[i] - in2Data[i];
86  }
87 }
88 
89 template<class StorageType>
91  const Image<StorageType> &in2,
92  Image<StorageType> &out) {
93  // assert same size
94  BIASASSERT( in1.GetWidth() == in2.GetWidth()
95  && in1.GetHeight() == in2.GetHeight()
96  && in1.GetChannelCount() == in2.GetChannelCount())
97 
98  // init output image
99  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
100 
101  // get pointers to data
102  const StorageType *in1Data = in1.GetImageData();
103  const StorageType *in2Data = in2.GetImageData();
104  StorageType *outData = out.GetImageData();
105 
106  const unsigned int size = in1.GetWidth()
107  * in1.GetHeight()
108  * in1.GetChannelCount();
109 
110  // perform operation
111  for (unsigned int i = 0; i < size; i++) {
112  outData[i] = in1Data[i] * in2Data[i];
113  }
114 }
115 
116 template<class StorageType>
118  const Image<StorageType> &in2,
119  Image<StorageType> &out) {
120  // assert same size
121  BIASASSERT( in1.GetWidth() == in2.GetWidth()
122  && in1.GetHeight() == in2.GetHeight()
123  && in1.GetChannelCount() == in2.GetChannelCount())
124 
125  // init output image
126  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
127 
128  // get pointers to data
129  const StorageType *in1Data = in1.GetImageData();
130  const StorageType *in2Data = in2.GetImageData();
131  StorageType *outData = out.GetImageData();
132 
133  const unsigned int size = in1.GetWidth()
134  * in1.GetHeight()
135  * in1.GetChannelCount();
136 
137  // perform operation
138  for (unsigned int i = 0; i < size; i++) {
139  if (in2Data[i] != (StorageType)0) {
140  outData[i] = in1Data[i] / in2Data[i];
141  }
142  else {
143  BIASWARN("prevented division by zero")
144  outData[i] = (StorageType)0;
145  }
146  }
147 }
148 
149 template <class StorageType>
151  const Image<StorageType> &in2,
152  Image<StorageType> &out)
153 {
154  // assert same size
155  BIASASSERT( in1.GetWidth() == in2.GetWidth()
156  && in1.GetHeight() == in2.GetHeight()
157  && in1.GetChannelCount() == in2.GetChannelCount())
158 
159  // init output image
160  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
161 
162  // get pointers to data
163  const StorageType *in1Data = in1.GetImageData();
164  const StorageType *in2Data = in2.GetImageData();
165  StorageType *outData = out.GetImageData();
166 
167  const unsigned int size = in1.GetWidth()
168  * in1.GetHeight()
169  * in1.GetChannelCount();
170 
171  // perform operation
172  for (unsigned int i = 0; i < size; i++) {
173  if (in1Data[i] != 0 && in2Data[i] != 0) {
174  outData[i] = 1;
175  }
176  else {
177  outData[i] = 0;
178  }
179  }
180 }
181 
182 template <class StorageType>
184  const Image<StorageType> &in2,
185  Image<StorageType> &out)
186 {
187  // assert same size
188  BIASASSERT( in1.GetWidth() == in2.GetWidth()
189  && in1.GetHeight() == in2.GetHeight()
190  && in1.GetChannelCount() == in2.GetChannelCount())
191 
192  // init output image
193  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
194 
195  // get pointers to data
196  const StorageType *in1Data = in1.GetImageData();
197  const StorageType *in2Data = in2.GetImageData();
198  StorageType *outData = out.GetImageData();
199 
200  const unsigned int size = in1.GetWidth()
201  * in1.GetHeight()
202  * in1.GetChannelCount();
203 
204  // perform operation
205  for (unsigned int i = 0; i < size; i++) {
206  if (in1Data[i] == 0 || in2Data[i] == 0) { // = !(in1Data[i] != 0 && in2Data[i] != 0)
207  outData[i] = 1;
208  }
209  else {
210  outData[i] = 0;
211  }
212  }
213 }
214 
215 template <class StorageType>
217  const Image<StorageType> &in2,
218  Image<StorageType> &out)
219 {
220  // assert same size
221  BIASASSERT( in1.GetWidth() == in2.GetWidth()
222  && in1.GetHeight() == in2.GetHeight()
223  && in1.GetChannelCount() == in2.GetChannelCount())
224 
225  // init output image
226  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
227 
228  // get pointers to data
229  const StorageType *in1Data = in1.GetImageData();
230  const StorageType *in2Data = in2.GetImageData();
231  StorageType *outData = out.GetImageData();
232 
233  const unsigned int size = in1.GetWidth()
234  * in1.GetHeight()
235  * in1.GetChannelCount();
236 
237  // perform operation
238  for (unsigned int i = 0; i < size; i++) {
239  if (in1Data[i] != 0 || in2Data[i] != 0) {
240  outData[i] = 1;
241  }
242  else {
243  outData[i] = 0;
244  }
245  }
246 }
247 
248 template <class StorageType>
250  const Image<StorageType> &in2,
251  Image<StorageType> &out)
252 {
253  // assert same size
254  BIASASSERT( in1.GetWidth() == in2.GetWidth()
255  && in1.GetHeight() == in2.GetHeight()
256  && in1.GetChannelCount() == in2.GetChannelCount())
257 
258  // init output image
259  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
260 
261  // get pointers to data
262  const StorageType *in1Data = in1.GetImageData();
263  const StorageType *in2Data = in2.GetImageData();
264  StorageType *outData = out.GetImageData();
265 
266  const unsigned int size = in1.GetWidth()
267  * in1.GetHeight()
268  * in1.GetChannelCount();
269 
270  // perform operation
271  for (unsigned int i = 0; i < size; i++) {
272  if (in1Data[i] == 0 && in2Data[i] == 0) { // = !(in1Data[i] != 0 || in2Data[i] != 0)
273  outData[i] = 1;
274  }
275  else {
276  outData[i] = 0;
277  }
278  }
279 }
280 
281 template <class StorageType>
283  const Image<StorageType> &in2,
284  Image<StorageType> &out)
285 {
286  // assert same size
287  BIASASSERT( in1.GetWidth() == in2.GetWidth()
288  && in1.GetHeight() == in2.GetHeight()
289  && in1.GetChannelCount() == in2.GetChannelCount())
290 
291  // init output image
292  out.Init(in1.GetWidth(), in1.GetHeight(), in1.GetChannelCount());
293 
294  // get pointers to data
295  const StorageType *in1Data = in1.GetImageData();
296  const StorageType *in2Data = in2.GetImageData();
297  StorageType *outData = out.GetImageData();
298 
299  const unsigned int size = in1.GetWidth()
300  * in1.GetHeight()
301  * in1.GetChannelCount();
302 
303  // perform operation
304  for (unsigned int i = 0; i < size; i++) {
305  if ((in1Data[i] != 0 && in2Data[i] == 0)
306  ||
307  (in1Data[i] == 0 && in2Data[i] != 0)) {
308  outData[i] = 1;
309  }
310  else {
311  outData[i] = 0;
312  }
313  }
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 //calculations between one image and a scalar //
318 ////////////////////////////////////////////////////////////////////////////////
319 
320 template <class StorageType>
322  StorageType scalar,
323  Image<StorageType> &out) {
324  // init output image
325  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
326 
327  // get pointers to data
328  const StorageType *inData = in.GetImageData();
329  StorageType *outData = out.GetImageData();
330 
331  const unsigned int size = in.GetWidth()
332  * in.GetHeight()
333  * in.GetChannelCount();
334 
335  // perform operation
336  for (unsigned int i = 0; i < size; i++) {
337  outData[i] = inData[i] + scalar;
338  }
339 }
340 
341 template <class StorageType>
343  StorageType scalar,
344  Image<StorageType> &out) {
345  // init output image
346  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
347 
348  // get pointers to data
349  const StorageType *inData = in.GetImageData();
350  StorageType *outData = out.GetImageData();
351 
352  const unsigned int size = in.GetWidth()
353  * in.GetHeight()
354  * in.GetChannelCount();
355 
356  // perform operation
357  for (unsigned int i = 0; i < size; i++) {
358  outData[i] = inData[i] - scalar;
359  }
360 }
361 
362 template <class StorageType>
364  StorageType scalar,
365  Image<StorageType> &out) {
366  // init output image
367  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
368 
369  // get pointers to data
370  const StorageType *inData = in.GetImageData();
371  StorageType *outData = out.GetImageData();
372 
373  const unsigned int size = in.GetWidth()
374  * in.GetHeight()
375  * in.GetChannelCount();
376 
377  // perform operation
378  for (unsigned int i = 0; i < size; i++) {
379  outData[i] = inData[i] * scalar;
380  }
381 }
382 
383 template <class StorageType>
385  StorageType scalar,
386  Image<StorageType> &out) {
387  BIASASSERT(scalar != (StorageType)0)
388 
389  // init output image
390  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
391 
392  // get pointers to data
393  const StorageType *inData = in.GetImageData();
394  StorageType *outData = out.GetImageData();
395 
396  const unsigned int size = in.GetWidth()
397  * in.GetHeight()
398  * in.GetChannelCount();
399 
400  // perform operation
401  for (unsigned int i = 0; i < size; i++) {
402  outData[i] = inData[i] / scalar;
403  }
404 }
405 
406 ////////////////////////////////////////////////////////////////////////////////
407 // calculations on a single image //
408 ////////////////////////////////////////////////////////////////////////////////
409 
410 template <class StorageType>
412  Image<StorageType> &out) {
413  // init output image
414  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
415 
416  // get pointers to data
417  const StorageType *inData = in.GetImageData();
418  StorageType *outData = out.GetImageData();
419 
420  const unsigned int size = in.GetWidth()
421  * in.GetHeight()
422  * in.GetChannelCount();
423 
424  // perform operation
425  for (unsigned int i = 0; i < size; i++) {
426  outData[i] = StorageType(log(double(inData[i])));
427  }
428 }
429 
430 template <class StorageType>
432  Image<StorageType> &out) {
433  // init output image
434  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
435 
436  // get pointers to data
437  const StorageType *inData = in.GetImageData();
438  StorageType *outData = out.GetImageData();
439 
440  const unsigned int size = in.GetWidth()
441  * in.GetHeight()
442  * in.GetChannelCount();
443 
444  // perform operation
445  for (unsigned int i = 0; i < size; i++) {
446  outData[i] = StorageType(log10(double(inData[i])));
447  }
448 }
449 
450 template <class StorageType>
452  Image<StorageType> &out) {
453  // init output image
454  out.Init(in.GetWidth(), in.GetHeight(), in.GetChannelCount());
455 
456  // get pointers to data
457  const StorageType *inData = in.GetImageData();
458  StorageType *outData = out.GetImageData();
459 
460  const unsigned int size = in.GetWidth()
461  * in.GetHeight()
462  * in.GetChannelCount();
463 
464  // perform operation
465  for (unsigned int i = 0; i < size; i++) {
466  outData[i] = (StorageType)abs(inData[i]);
467  }
468 }
469 
470 // explicit instantiation
471 #define INSTANCE_ImageCalc(type)\
472 template class BIASImageUtilsBase_EXPORT ImageCalc<type>;
473 namespace BIAS{
474 INSTANCE_ImageCalc(unsigned char)
475 INSTANCE_ImageCalc(float)
476 #ifdef BUILD_IMAGE_INT
477  INSTANCE_ImageCalc(int)
478 #endif
479 #ifdef BUILD_IMAGE_CHAR
480  INSTANCE_ImageCalc(char)
481 #endif
482 #ifdef BUILD_IMAGE_SHORT
483  INSTANCE_ImageCalc(short)
484 #endif
485 #ifdef BUILD_IMAGE_USHORT
486  INSTANCE_ImageCalc(unsigned short)
487 #endif
488 #ifdef BUILD_IMAGE_DOUBLE
489  INSTANCE_ImageCalc(double)
490 #endif
491 #ifdef BUILD_IMAGE_UINT
492  INSTANCE_ImageCalc(unsigned int)
493 #endif
494 }
unsigned int GetWidth() const
Definition: ImageBase.hh:312
Performs pixelwise arithmetic and boolean operations on images.
Definition: ImageCalc.hh:52
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
The image template class for specific storage types.
Definition: Image.hh:78
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414