SMIL  0.9.1
DMorphoFilter.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_MORPHO_FILTER_HPP
31 #define _D_MORPHO_FILTER_HPP
32 
33 #include "Core/include/DImage.h"
34 #include "DMorphImageOperations.hxx"
35 
36 
37 namespace smil
38 {
53  template <class T>
54  RES_T asfClose(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
55  {
56  ASSERT_ALLOCATED(&imIn, &imOut);
57  ASSERT_SAME_SIZE(&imIn, &imOut);
58 
59  ImageFreezer freeze(imOut);
60 
61  Image<T> tmpIm(imIn, true); // clone
62  for (UINT i=1;i<=se.size;i++)
63  {
64  ASSERT((close(tmpIm, imOut, se(i))==RES_OK));
65  ASSERT((open(imOut, tmpIm, se(i))==RES_OK));
66  }
67  ASSERT((copy(tmpIm, imOut)==RES_OK));
68 
69  return RES_OK;
70  }
71 
78  template <class T>
79  RES_T asfOpen(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
80  {
81  ASSERT_ALLOCATED(&imIn, &imOut);
82  ASSERT_SAME_SIZE(&imIn, &imOut);
83 
84  ImageFreezer freeze(imOut);
85 
86  Image<T> tmpIm(imIn, true); // clone
87  for (UINT i=1;i<=se.size;i++)
88  {
89  ASSERT((open(tmpIm, imOut, se(i))==RES_OK));
90  ASSERT((close(imOut, tmpIm, se(i))==RES_OK));
91  }
92  ASSERT((copy(tmpIm, imOut)==RES_OK));
93 
94  return RES_OK;
95  }
96 
97 
98  template <class T>
99  class meanFunct : public MorphImageFunctionBase<T, T>
100  {
101  public:
103 
104  virtual inline void processPixel(size_t pointOffset, vector<int> &dOffsetList)
105  {
106  double meanVal = 0;
107  vector<int>::iterator dOffset = dOffsetList.begin();
108  while(dOffset!=dOffsetList.end())
109  {
110  meanVal += double(parentClass::pixelsIn[pointOffset + *dOffset]);
111  dOffset++;
112  }
113  parentClass::pixelsOut[pointOffset] = T(meanVal / double(dOffsetList.size()));
114  }
115  };
116 
121  template <class T>
122  RES_T mean(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
123  {
124  ASSERT_ALLOCATED(&imIn, &imOut);
125  ASSERT_SAME_SIZE(&imIn, &imOut);
126 
127  meanFunct<T> f;
128 
129  ASSERT((f._exec(imIn, imOut, se)==RES_OK));
130 
131  return RES_OK;
132 
133  }
134 
135  template <class T>
136  class medianFunct : public MorphImageFunctionBase<T, T>
137  {
138  public:
140 
141  virtual inline void processPixel(size_t pointOffset, vector<int> &dOffsetList)
142  {
143  vector<T> vals;
144  vector<int>::iterator dOffset = dOffsetList.begin();
145  while(dOffset!=dOffsetList.end())
146  {
147  vals.push_back(parentClass::pixelsIn[pointOffset + *dOffset]);
148  dOffset++;
149  }
150  sort(vals.begin(), vals.end());
151  parentClass::pixelsOut[pointOffset] = vals[dOffsetList.size()/2];
152  }
153  };
154 
155 
156 
157 
158 
163  template <class T>
164  RES_T median(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
165  {
166  ASSERT_ALLOCATED(&imIn, &imOut);
167  ASSERT_SAME_SIZE(&imIn, &imOut);
168 
169  medianFunct<T> f;
170 
171  ASSERT((f._exec(imIn, imOut, se)==RES_OK));
172 
173  return RES_OK;
174 
175  }
176 
177 
178 
179  template <class T>
180  class rankFunct : public MorphImageFunctionBase<T, T>
181  {
182  public:
184 
185  rankFunct(double per) : MorphImageFunctionBase<T, T>(),percentile(per)
186  {}
187  virtual inline void processPixel(size_t pointOffset, vector<int> &dOffsetList)
188  {
189  vector<T> vals;
190  vector<int>::iterator dOffset = dOffsetList.begin();
191  while(dOffset!=dOffsetList.end())
192  {
193  vals.push_back(parentClass::pixelsIn[pointOffset + *dOffset]);
194  dOffset++;
195  }
196  sort(vals.begin(), vals.end());
197  parentClass::pixelsOut[pointOffset] = vals[static_cast<int>(dOffsetList.size()*this->percentile)];
198  }
199  private:
200  double percentile;
201  };
202 
203 
207  template <class T>
208  RES_T rank(const Image<T> &imIn, Image<T> &imOut, double percentile, const StrElt &se=DEFAULT_SE)
209  {
210  ASSERT_ALLOCATED(&imIn, &imOut);
211  ASSERT_SAME_SIZE(&imIn, &imOut);
212 
213  rankFunct<T> f(percentile);
214 
215  ASSERT((f._exec(imIn, imOut, se)==RES_OK));
216 
217  return RES_OK;
218 
219  }
220 
223 } // namespace smil
224 
225 
226 
227 #endif // _D_MORPHO_FILTER_HPP
228 
Vector_double meanVal(const Image< T > &imIn, bool onlyNonZero=false)
Mean value and standard deviation.
Definition: DMeasures.hpp:145
Definition: DColorConvert.h:38
RES_T copy(const Image< T1 > &imIn, size_t startX, size_t startY, size_t startZ, size_t sizeX, size_t sizeY, size_t sizeZ, Image< T2 > &imOut, size_t outStartX=0, size_t outStartY=0, size_t outStartZ=0)
Copy image.
Definition: DImageArith.hpp:114
Definition: DBaseImage.h:235
RES_T median(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Median filter.
Definition: DMorphoFilter.hpp:164
RES_T mean(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Mean filter.
Definition: DMorphoFilter.hpp:122
RES_T close(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Morphological grayscale closing.
Definition: DMorphoBase.hpp:127
Definition: DMorphoFilter.hpp:99
Base structuring element.
Definition: DStructuringElement.h:51
Base morpho operator class.
Definition: DMorphImageOperations.hpp:54
Main Image class.
Definition: DQVtkViewer.hpp:44
RES_T asfClose(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Alternate Sequential Filter beginning by a closing.
Definition: DMorphoFilter.hpp:54
RES_T open(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Morphological grayscale opening.
Definition: DMorphoBase.hpp:159
Definition: DMorphoFilter.hpp:180
Definition: DMorphoFilter.hpp:136
RES_T asfOpen(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
Alternate Sequential Filter beginning by an opening.
Definition: DMorphoFilter.hpp:79
RES_T rank(const Image< T > &imIn, Image< T > &imOut, double percentile, const StrElt &se=DEFAULT_SE)
Rank filter.
Definition: DMorphoFilter.hpp:208