31 template<
typename IteratorT>
36 typedef typename IteratorT::value_type::IntensityType
value_type;
37 typedef typename IteratorT::value_type::IntensityType&
reference;
38 typedef typename IteratorT::value_type::IntensityType*
pointer;
47 return base->getIntensity();
50 template<
typename IndexT>
53 return base[index].getIntensity();
69 IteratorT tmp = *
this;
89 template<
typename IteratorT>
139 defaults_.setValue(
"struc_elem_length", 3.0,
"Length of the structuring element. This should be wider than the expected peak width.");
140 defaults_.setValue(
"struc_elem_unit",
"Thomson",
"The unit of the 'struct_elem_length'.");
141 defaults_.setValidStrings(
"struc_elem_unit", {
"Thomson",
"DataPoints"});
143 defaults_.setValue(
"method",
"tophat",
144 "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 "
145 "documentation of MorpthologicalFilter.");
146 defaults_.setValidStrings(
"method", {
"identity",
"erosion",
"dilation",
"opening",
"closing",
"gradient",
"tophat",
"bothat",
"erosion_simple",
"dilation_simple"});
167 template<
typename InputIterator,
typename OutputIterator>
168 void filterRange(InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
171 static std::vector<typename InputIterator::value_type> buffer;
172 const UInt size = input_end - input_begin;
175 if (struct_size_in_datapoints_ == 0)
177 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
181 std::string method = param_.getValue(
"method");
182 if (method ==
"identity")
184 std::copy(input_begin, input_end, output_begin);
186 else if (method ==
"erosion")
188 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
190 else if (method ==
"dilation")
192 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
194 else if (method ==
"opening")
196 if (buffer.size() < size)
198 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
199 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
201 else if (method ==
"closing")
203 if (buffer.size() < size)
205 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
206 applyErosion_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
208 else if (method ==
"gradient")
210 if (buffer.size() < size)
212 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
213 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
214 for (
UInt i = 0; i < size; ++i)
215 output_begin[i] -= buffer[i];
217 else if (method ==
"tophat")
219 if (buffer.size() < size)
221 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
222 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
223 for (
UInt i = 0; i < size; ++i)
224 output_begin[i] = input_begin[i] - output_begin[i];
226 else if (method ==
"bothat")
228 if (buffer.size() < size)
230 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
231 applyErosion_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
232 for (
UInt i = 0; i < size; ++i)
233 output_begin[i] = input_begin[i] - output_begin[i];
235 else if (method ==
"erosion_simple")
237 applyErosionSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
239 else if (method ==
"dilation_simple")
241 applyDilationSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
244 struct_size_in_datapoints_ = 0;
267 if (spectrum.size() <= 1)
273 if (param_.getValue(
"struc_elem_unit") ==
"Thomson")
275 const double struc_elem_length = (double)param_.getValue(
"struc_elem_length");
276 const double mz_diff = spectrum.back().getMZ() - spectrum.begin()->getMZ();
277 struct_size_in_datapoints_ = (
UInt)(ceil(struc_elem_length * (
double)(spectrum.size() - 1) / mz_diff));
281 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
285 ++struct_size_in_datapoints_;
288 std::vector<Peak1D::IntensityType> output(spectrum.size());
292 for (
Size i = 0; i < spectrum.size(); ++i)
294 spectrum[i].setIntensity(output[i]);
306 startProgress(0, exp.
size(),
"filtering baseline");
307 for (
UInt i = 0; i < exp.
size(); ++i)
323 template<
typename InputIterator,
typename OutputIterator>
324 void applyErosion_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
326 typedef typename InputIterator::value_type ValueType;
327 const Int size = input_end - input;
328 const Int struc_size_half = struc_size / 2;
330 static std::vector<ValueType> buffer;
331 if (
Int(buffer.size()) < struc_size)
332 buffer.resize(struc_size);
341 if (size <= struc_size || size <= 5)
343 applyErosionSimple_(struc_size, input, input_end, output);
349 for (++ii; ii < struc_size_half; ++ii)
350 if (current > input[ii])
352 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
354 if (current > input[ii])
356 output[oi] = current;
361 for (anchor = struc_size; anchor <= size - struc_size; anchor += struc_size)
366 for (i = 1; i < struc_size; ++i, ++ii)
368 if (current > input[ii])
373 oi = ii + struc_size_half;
375 for (i = 1; i < struc_size; ++i, --ii, --oi)
377 if (current > input[ii])
379 output[oi] = std::min(buffer[struc_size - i], current);
381 if (current > input[ii])
383 output[oi] = current;
391 for (--ii; ii >= size - struc_size_half; --ii)
392 if (current > input[ii])
394 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
396 if (current > input[ii])
398 output[oi] = current;
400 anchor = size - struc_size;
404 for (i = 1; i < struc_size; ++i, ++ii)
406 if (current > input[ii])
411 oi = ii + struc_size_half;
413 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
415 if (current > input[ii])
417 output[oi] = std::min(buffer[struc_size - i], current);
421 if (current > input[ii])
423 output[oi] = current;
433 template<
typename InputIterator,
typename OutputIterator>
434 void applyDilation_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
436 typedef typename InputIterator::value_type ValueType;
437 const Int size = input_end - input;
438 const Int struc_size_half = struc_size / 2;
440 static std::vector<ValueType> buffer;
441 if (
Int(buffer.size()) < struc_size)
442 buffer.resize(struc_size);
451 if (size <= struc_size || size <= 5)
453 applyDilationSimple_(struc_size, input, input_end, output);
459 for (++ii; ii < struc_size_half; ++ii)
460 if (current < input[ii])
462 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
464 if (current < input[ii])
466 output[oi] = current;
471 for (anchor = struc_size; anchor <= size - struc_size; anchor += struc_size)
476 for (i = 1; i < struc_size; ++i, ++ii)
478 if (current < input[ii])
483 oi = ii + struc_size_half;
485 for (i = 1; i < struc_size; ++i, --ii, --oi)
487 if (current < input[ii])
489 output[oi] = std::max(buffer[struc_size - i], current);
491 if (current < input[ii])
493 output[oi] = current;
501 for (--ii; ii >= size - struc_size_half; --ii)
502 if (current < input[ii])
504 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
506 if (current < input[ii])
508 output[oi] = current;
510 anchor = size - struc_size;
514 for (i = 1; i < struc_size; ++i, ++ii)
516 if (current < input[ii])
521 oi = ii + struc_size_half;
523 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
525 if (current < input[ii])
527 output[oi] = std::max(buffer[struc_size - i], current);
531 if (current < input[ii])
533 output[oi] = current;
540 template<
typename InputIterator,
typename OutputIterator>
541 void applyErosionSimple_(
Int struc_size, InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
543 typedef typename InputIterator::value_type ValueType;
544 const int size = input_end - input_begin;
545 const Int struc_size_half = struc_size / 2;
546 for (
Int index = 0; index < size; ++index)
548 Int start = std::max(0, index - struc_size_half);
549 Int stop = std::min(size - 1, index + struc_size_half);
550 ValueType value = input_begin[start];
551 for (
Int i = start + 1; i <= stop; ++i)
552 if (value > input_begin[i])
553 value = input_begin[i];
554 output_begin[index] = value;
560 template<
typename InputIterator,
typename OutputIterator>
563 typedef typename InputIterator::value_type ValueType;
564 const int size = input_end - input_begin;
565 const Int struc_size_half = struc_size / 2;
566 for (
Int index = 0; index < size; ++index)
568 Int start = std::max(0, index - struc_size_half);
569 Int stop = std::min(size - 1, index + struc_size_half);
570 ValueType value = input_begin[start];
571 for (
Int i = start + 1; i <= stop; ++i)
572 if (value < input_begin[i])
573 value = input_begin[i];
574 output_begin[index] = value;
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
An iterator wrapper to access peak intensities instead of the peak itself.
Definition: MorphologicalFilter.h:33
bool operator==(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:74
IteratorT base
Definition: MorphologicalFilter.h:85
std::forward_iterator_tag iterator_category
Definition: MorphologicalFilter.h:35
bool operator!=(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:79
IteratorT::difference_type difference_type
Definition: MorphologicalFilter.h:39
IteratorT::value_type::IntensityType value_type
Definition: MorphologicalFilter.h:36
IteratorT::value_type::IntensityType * pointer
Definition: MorphologicalFilter.h:38
difference_type operator-(IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:56
value_type operator*()
Definition: MorphologicalFilter.h:45
IntensityIteratorWrapper operator++(int)
Definition: MorphologicalFilter.h:67
IntensityIteratorWrapper & operator++()
Definition: MorphologicalFilter.h:61
IteratorT::value_type::IntensityType & reference
Definition: MorphologicalFilter.h:37
value_type operator[](const IndexT &index)
Definition: MorphologicalFilter.h:51
IntensityIteratorWrapper(const IteratorT &rhs)
Definition: MorphologicalFilter.h:41
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:46
Size size() const noexcept
The number of spectra.
Definition: MSExperiment.h:121
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
This class implements baseline filtering operations using methods from mathematical morphology.
Definition: MorphologicalFilter.h:133
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:434
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:168
void filter(MSSpectrum &spectrum)
Applies the morphological filtering operation to an MSSpectrum.
Definition: MorphologicalFilter.h:261
void filterExperiment(PeakMap &exp)
Applies the morphological filtering operation to an MSExperiment.
Definition: MorphologicalFilter.h:304
UInt struct_size_in_datapoints_
Member for struct size in data points.
Definition: MorphologicalFilter.h:317
MorphologicalFilter()
Constructor.
Definition: MorphologicalFilter.h:136
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:561
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:324
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:541
~MorphologicalFilter() override
Destructor.
Definition: MorphologicalFilter.h:152
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
void setType(SpectrumType type)
sets the spectrum type
@ PROFILE
profile data
Definition: SpectrumSettings.h:48
int Int
Signed integer type.
Definition: Types.h:72
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:207
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:90
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19