Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Mean.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 
26 #include "Mean.hh"
27 
28 #include <Base/Common/BIASpragma.hh>
29 
30 using namespace BIAS;
31 using namespace std;
32 
33 //////////////////////////////////////////////////////////////////////////
34 // implementation
35 //////////////////////////////////////////////////////////////////////////
36 
37 template <class InputStorageType, class OutputStorageType>
40  : Convolution<InputStorageType, OutputStorageType>()
41 {
42  _MeanSize = 2;
43 }
44 
45 template <class InputStorageType, class OutputStorageType>
48  : _MeanSize(other._MeanSize)
49 {
50 }
51 
52 template <class InputStorageType, class OutputStorageType>
55 {
56 }
57 
58 
59 template <class InputStorageType, class OutputStorageType>
62 {
63  BIASCDOUT(D_FILTERBASE_CALLSTACK, "Mean::Filter\n");
64  int res=0;
65  switch (_MeanSize){
66  case 1:
67  dst=src;
68  break;
69  case 2:
70  // \bug mean filter moves image content to left top corner by at least one pixel!
71  res=FilterMean2x2(src, dst);
72  break;
73  default:
74  _SetKernel();
76  break;
77  }
78  return res;
79 }
80 
81 template <class InputStorageType, class OutputStorageType>
84 {
85  BIASERR("Unimplemented interface, calling generic Filter");
86  return Filter(src, dst);
87 }
88 
89 template <class InputStorageType, class OutputStorageType>
92 {
93  BIASERR("Unimplemented interface, calling generic Filter");
94  return Filter(src, dst);
95 }
96 
97 template <class InputStorageType, class OutputStorageType>
100 {
101  BIASCDOUT(D_FILTERBASE_CALLSTACK, "Mean::FilterMean\n");
102  BIASERR("deprecated");
103  return -10;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////
107 
108 template <class InputStorageType, class OutputStorageType>
112 {
113  BIASCDOUT(D_FILTERBASE_CALLSTACK, "Mean::FilterMean2x2Grey\n");
114 #ifdef BIAS_DEBUG
115  BIASASSERT(src.SamePixelAndChannelCount(dst));
116  BIASASSERT(src.GetChannelCount()==1);
117  BIASASSERT(src.GetColorModel()==ImageBase::CM_Grey);
118  BIASASSERT(((void*)src.GetImageData())!= ((void*)dst.GetImageData()));
119 #endif
120  unsigned minx, miny, maxx, maxy;
121  src.GetROI()->GetCorners(minx, miny, maxx, maxy);
122 
123  //why?
124  maxx--;
125  maxy--;
126 
127  dst.GetROI()->SetCorners(minx, miny, maxx, maxy);
128 
129  register double sum, lsum;
130  register const InputStorageType *srcu, *srcl;
131  register OutputStorageType *pdst, *dstend, *dstlend;
132  register const int width=src.GetWidth();
133  register const int step=width-(maxx-minx);
134 
135  //first line start + offset from roi minx (row nr.) miny*width (col nr.),
136  srcl = srcu = src.GetImageData() + minx+(miny*width);
137  //second starting row
138  srcl += width;
139  //first row pointer in destination
140  pdst = dst.GetImageData() + minx+(miny*width);
141  //end of dest line
142  dstlend = pdst + width-step;
143  //end of dest
144  dstend = dst.GetImageData() + maxx+(maxy-1)*width;
145 
146  while (pdst < dstend){ //goes over the image
147  lsum = (double)*srcu++ + (double)*srcl++; //first two elements
148  while (pdst < dstlend){
149  sum = (double)*srcu++ + (double)*srcl++; //second two elements
150  // BUG !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
151  //*pdst++ = (OutputStorageType)((sum + lsum)/ 4.0);
152  *pdst++ = (OutputStorageType)((sum + lsum)/ 2.0); // tried to correct
153  lsum = sum =0.0;
154  }
155  srcu += step;
156  srcl += step;
157  pdst += step+1;
158  dstlend += width;
159  }
160 
161  return 0;
162 }
163 
164 
165 ////////////////////////////////////////////////////////////////////////
166 
167 template <class InputStorageType, class OutputStorageType>
171 {
172  BIASCDOUT(D_FILTERBASE_CALLSTACK, "Mean::FilterMean2x2\n");
173  int res=0;
174  if (src.GetChannelCount() == 1 && src.GetColorModel()==ImageBase::CM_Grey){
175  if (! src.SamePixelAndChannelCount(dst)) {
176  if (!dst.IsEmpty()) dst.Release();
177  dst.Init(src.GetWidth(), src.GetHeight(), src.GetChannelCount());
178  }
179  // \bug moves content to left top corner
180  res = FilterMean2x2Grey(src, dst);
181  } else {
182  _SetKernel();
184  }
185  return res;
186 }
187 
188 //////////////////////////////////////////////////////////////////////////
189 // protected functions
190 //////////////////////////////////////////////////////////////////////////
191 
192 template <class InputStorageType, class OutputStorageType>
194  const FM_FLOAT val = 1.0f/(FM_FLOAT)(_MeanSize);
195  Vector<FM_FLOAT> H(_MeanSize), V(_MeanSize);
196  for (int i=0; i< _MeanSize; i++){
197  H[i] = V[i] = val;
198  }
199  _fm.Init(H,V);
200  // compute the int approximation
201  int thebits = _fm.ComputeIntPrecisionBits(sizeof(InputStorageType)*8,
202  sizeof(CONV_INT)*8-1);
203  _fm.CreateIntFilter(thebits,thebits,thebits);
204 }
205 
206 template <class InputStorageType, class OutputStorageType>
208 GetBordersValid_(int &border_x, int &border_y) const
209 {
210  border_x = border_y = _MeanSize/2;
211 }
212 
213 //////////////////////////////////////////////////////////////////////////
214 // instantiation
215 //////////////////////////////////////////////////////////////////////////
216 
217 namespace BIAS{
218 #define FILTER_INSTANTIATION_CLASS Mean
219 #include "Filterinst.hh"
220 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
int _MeanSize
Definition: Mean.hh:88
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
averages over a region with constant weights
Definition: Mean.cpp:61
generic convolution class.
Definition: Convolution.hh:66
gray values, 1 channel
Definition: ImageBase.hh:130
int SetCorners(unsigned UpperLeftX, unsigned UpperLeftY, unsigned LowerRightX, unsigned LowerRightY)
Sets a rectangular region of interest.
Definition: ROI.cpp:287
void GetCorners(unsigned &UpperLeftX, unsigned &UpperLeftY, unsigned &LowerRightX, unsigned &LowerRightY) const
Return the region of interest, by saving the coordinates within the variables defined by the paramete...
Definition: ROI.hh:443
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
virtual void GetBordersValid_(int &border_x, int &border_y) const
Definition: Mean.cpp:208
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
decides on the image types which FilterFunction should be called
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual int FilterInt(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
only calls filter, for consistency of params
Definition: Mean.cpp:83
virtual int FilterFloat(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
only calls filter, for consistency of params
Definition: Mean.cpp:91
Mean()
Definition: Mean.cpp:39
ROI * GetROI()
Returns a pointer to the roi object.
Definition: ImageBase.hh:615
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void _SetKernel()
sets the vector members of Convolution
Definition: Mean.cpp:193
int FilterMean2x2Grey(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
dst must be initialized: dst width = src width - 1, dst height = src height - 1 Does support ROI...
Definition: Mean.cpp:110
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
~Mean()
Definition: Mean.cpp:54
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
int FilterMean(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
deprecated
Definition: Mean.cpp:99
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
average mean filter
Definition: Mean.hh:41
int FilterMean2x2(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
dst width = src width - 1, dst height = src height - 1
Definition: Mean.cpp:169