57 template<
typename IteratorT>
62 typedef typename IteratorT::value_type::IntensityType
value_type;
63 typedef typename IteratorT::value_type::IntensityType&
reference;
64 typedef typename IteratorT::value_type::IntensityType*
pointer;
73 return base->getIntensity();
76 template<
typename IndexT>
79 return base[index].getIntensity();
95 IteratorT tmp = *
this;
115 template<
typename IteratorT>
165 defaults_.setValue(
"struc_elem_length", 3.0,
"Length of the structuring element. This should be wider than the expected peak width.");
166 defaults_.setValue(
"struc_elem_unit",
"Thomson",
"The unit of the 'struct_elem_length'.");
167 defaults_.setValidStrings(
"struc_elem_unit", {
"Thomson",
"DataPoints"});
169 defaults_.setValue(
"method",
"tophat",
170 "Method to use, the default is 'tophat'. Do not change this unless you know what you are doing. The other methods may be useful for tuning the parameters, see the class "
171 "documentation of MorpthologicalFilter.");
172 defaults_.setValidStrings(
"method", {
"identity",
"erosion",
"dilation",
"opening",
"closing",
"gradient",
"tophat",
"bothat",
"erosion_simple",
"dilation_simple"});
193 template<
typename InputIterator,
typename OutputIterator>
194 void filterRange(InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
197 static std::vector<typename InputIterator::value_type> buffer;
198 const UInt size = input_end - input_begin;
201 if (struct_size_in_datapoints_ == 0)
203 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
207 std::string method = param_.getValue(
"method");
208 if (method ==
"identity")
210 std::copy(input_begin, input_end, output_begin);
212 else if (method ==
"erosion")
214 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
216 else if (method ==
"dilation")
218 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
220 else if (method ==
"opening")
222 if (buffer.size() < size)
224 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
225 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
227 else if (method ==
"closing")
229 if (buffer.size() < size)
231 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
232 applyErosion_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
234 else if (method ==
"gradient")
236 if (buffer.size() < size)
238 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
239 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
240 for (
UInt i = 0; i < size; ++i)
241 output_begin[i] -= buffer[i];
243 else if (method ==
"tophat")
245 if (buffer.size() < size)
247 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
248 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
249 for (
UInt i = 0; i < size; ++i)
250 output_begin[i] = input_begin[i] - output_begin[i];
252 else if (method ==
"bothat")
254 if (buffer.size() < size)
256 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
257 applyErosion_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
258 for (
UInt i = 0; i < size; ++i)
259 output_begin[i] = input_begin[i] - output_begin[i];
261 else if (method ==
"erosion_simple")
263 applyErosionSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
265 else if (method ==
"dilation_simple")
267 applyDilationSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
270 struct_size_in_datapoints_ = 0;
293 if (spectrum.size() <= 1)
299 if (param_.getValue(
"struc_elem_unit") ==
"Thomson")
301 const double struc_elem_length = (double)param_.getValue(
"struc_elem_length");
302 const double mz_diff = spectrum.back().getMZ() - spectrum.begin()->getMZ();
303 struct_size_in_datapoints_ = (
UInt)(ceil(struc_elem_length * (
double)(spectrum.size() - 1) / mz_diff));
307 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
311 ++struct_size_in_datapoints_;
314 std::vector<Peak1D::IntensityType> output(spectrum.size());
318 for (
Size i = 0; i < spectrum.size(); ++i)
320 spectrum[i].setIntensity(output[i]);
332 startProgress(0, exp.
size(),
"filtering baseline");
333 for (
UInt i = 0; i < exp.
size(); ++i)
349 template<
typename InputIterator,
typename OutputIterator>
350 void applyErosion_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
352 typedef typename InputIterator::value_type ValueType;
353 const Int size = input_end - input;
354 const Int struc_size_half = struc_size / 2;
356 static std::vector<ValueType> buffer;
357 if (
Int(buffer.size()) < struc_size)
358 buffer.resize(struc_size);
367 if (size <= struc_size || size <= 5)
369 applyErosionSimple_(struc_size, input, input_end, output);
375 for (++ii; ii < struc_size_half; ++ii)
376 if (current > input[ii])
378 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
380 if (current > input[ii])
382 output[oi] = current;
387 for (anchor = struc_size; anchor <= size - struc_size; anchor += struc_size)
392 for (i = 1; i < struc_size; ++i, ++ii)
394 if (current > input[ii])
399 oi = ii + struc_size_half;
401 for (i = 1; i < struc_size; ++i, --ii, --oi)
403 if (current > input[ii])
405 output[oi] = std::min(buffer[struc_size - i], current);
407 if (current > input[ii])
409 output[oi] = current;
417 for (--ii; ii >= size - struc_size_half; --ii)
418 if (current > input[ii])
420 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
422 if (current > input[ii])
424 output[oi] = current;
426 anchor = size - struc_size;
430 for (i = 1; i < struc_size; ++i, ++ii)
432 if (current > input[ii])
437 oi = ii + struc_size_half;
439 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
441 if (current > input[ii])
443 output[oi] = std::min(buffer[struc_size - i], current);
447 if (current > input[ii])
449 output[oi] = current;
459 template<
typename InputIterator,
typename OutputIterator>
460 void applyDilation_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
462 typedef typename InputIterator::value_type ValueType;
463 const Int size = input_end - input;
464 const Int struc_size_half = struc_size / 2;
466 static std::vector<ValueType> buffer;
467 if (
Int(buffer.size()) < struc_size)
468 buffer.resize(struc_size);
477 if (size <= struc_size || size <= 5)
479 applyDilationSimple_(struc_size, input, input_end, output);
485 for (++ii; ii < struc_size_half; ++ii)
486 if (current < input[ii])
488 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
490 if (current < input[ii])
492 output[oi] = current;
497 for (anchor = struc_size; anchor <= size - struc_size; anchor += struc_size)
502 for (i = 1; i < struc_size; ++i, ++ii)
504 if (current < input[ii])
509 oi = ii + struc_size_half;
511 for (i = 1; i < struc_size; ++i, --ii, --oi)
513 if (current < input[ii])
515 output[oi] = std::max(buffer[struc_size - i], current);
517 if (current < input[ii])
519 output[oi] = current;
527 for (--ii; ii >= size - struc_size_half; --ii)
528 if (current < input[ii])
530 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
532 if (current < input[ii])
534 output[oi] = current;
536 anchor = size - struc_size;
540 for (i = 1; i < struc_size; ++i, ++ii)
542 if (current < input[ii])
547 oi = ii + struc_size_half;
549 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
551 if (current < input[ii])
553 output[oi] = std::max(buffer[struc_size - i], current);
557 if (current < input[ii])
559 output[oi] = current;
566 template<
typename InputIterator,
typename OutputIterator>
567 void applyErosionSimple_(
Int struc_size, InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
569 typedef typename InputIterator::value_type ValueType;
570 const int size = input_end - input_begin;
571 const Int struc_size_half = struc_size / 2;
572 for (
Int index = 0; index < size; ++index)
574 Int start = std::max(0, index - struc_size_half);
575 Int stop = std::min(size - 1, index + struc_size_half);
576 ValueType value = input_begin[start];
577 for (
Int i = start + 1; i <= stop; ++i)
578 if (value > input_begin[i])
579 value = input_begin[i];
580 output_begin[index] = value;
586 template<
typename InputIterator,
typename OutputIterator>
589 typedef typename InputIterator::value_type ValueType;
590 const int size = input_end - input_begin;
591 const Int struc_size_half = struc_size / 2;
592 for (
Int index = 0; index < size; ++index)
594 Int start = std::max(0, index - struc_size_half);
595 Int stop = std::min(size - 1, index + struc_size_half);
596 ValueType value = input_begin[start];
597 for (
Int i = start + 1; i <= stop; ++i)
598 if (value < input_begin[i])
599 value = input_begin[i];
600 output_begin[index] = value;
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
An iterator wrapper to access peak intensities instead of the peak itself.
Definition: MorphologicalFilter.h:59
bool operator==(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:100
IteratorT base
Definition: MorphologicalFilter.h:111
std::forward_iterator_tag iterator_category
Definition: MorphologicalFilter.h:61
bool operator!=(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:105
IteratorT::difference_type difference_type
Definition: MorphologicalFilter.h:65
IteratorT::value_type::IntensityType value_type
Definition: MorphologicalFilter.h:62
IteratorT::value_type::IntensityType * pointer
Definition: MorphologicalFilter.h:64
difference_type operator-(IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:82
value_type operator*()
Definition: MorphologicalFilter.h:71
IntensityIteratorWrapper operator++(int)
Definition: MorphologicalFilter.h:93
IntensityIteratorWrapper & operator++()
Definition: MorphologicalFilter.h:87
IteratorT::value_type::IntensityType & reference
Definition: MorphologicalFilter.h:63
value_type operator[](const IndexT &index)
Definition: MorphologicalFilter.h:77
IntensityIteratorWrapper(const IteratorT &rhs)
Definition: MorphologicalFilter.h:67
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:72
Size size() const
The number of spectra.
Definition: MSExperiment.h:147
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
This class implements baseline filtering operations using methods from mathematical morphology.
Definition: MorphologicalFilter.h:159
void applyDilation_(Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
Applies dilation. This implementation uses van Herk's method. Only 3 min/max comparisons are required...
Definition: MorphologicalFilter.h:460
MorphologicalFilter(const MorphologicalFilter &source)
copy constructor not implemented
void filterRange(InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
Applies the morphological filtering operation to an iterator range.
Definition: MorphologicalFilter.h:194
void filter(MSSpectrum &spectrum)
Applies the morphological filtering operation to an MSSpectrum.
Definition: MorphologicalFilter.h:287
void filterExperiment(PeakMap &exp)
Applies the morphological filtering operation to an MSExperiment.
Definition: MorphologicalFilter.h:330
UInt struct_size_in_datapoints_
Member for struct size in data points.
Definition: MorphologicalFilter.h:343
MorphologicalFilter()
Constructor.
Definition: MorphologicalFilter.h:162
void applyDilationSimple_(Int struc_size, InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
Applies dilation. Simple implementation, possibly faster if struc_size is very small,...
Definition: MorphologicalFilter.h:587
void applyErosion_(Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
Applies erosion. This implementation uses van Herk's method. Only 3 min/max comparisons are required ...
Definition: MorphologicalFilter.h:350
void applyErosionSimple_(Int struc_size, InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
Applies erosion. Simple implementation, possibly faster if struc_size is very small,...
Definition: MorphologicalFilter.h:567
~MorphologicalFilter() override
Destructor.
Definition: MorphologicalFilter.h:178
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:53
void setType(SpectrumType type)
sets the spectrum type
@ PROFILE
profile data
Definition: SpectrumSettings.h:74
int Int
Signed integer type.
Definition: Types.h:102
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:199
IntensityIteratorWrapper< IteratorT > intensityIteratorWrapper(const IteratorT &rhs)
make-function so that we need no write out all those type names to get the wrapped iterator.
Definition: MorphologicalFilter.h:116
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48