35 #ifndef OPENMS_FILTERING_BASELINE_MORPHOLOGICALFILTER_H 36 #define OPENMS_FILTERING_BASELINE_MORPHOLOGICALFILTER_H 59 template <
typename IteratorT>
61 public std::iterator<std::forward_iterator_tag, typename IteratorT::value_type::IntensityType>
64 typedef typename IteratorT::value_type::IntensityType
value_type;
65 typedef typename IteratorT::value_type::IntensityType &
reference;
66 typedef typename IteratorT::value_type::IntensityType *
pointer;
76 return base->getIntensity();
79 template <
typename IndexT>
82 return base[index].getIntensity();
98 IteratorT tmp = *
this;
118 template <
typename IteratorT>
171 struct_size_in_datapoints_(0)
174 defaults_.setValue(
"struc_elem_length", 3.0,
"Length of the structuring element. This should be wider than the expected peak width.");
175 defaults_.setValue(
"struc_elem_unit",
"Thomson",
"The unit of the 'struct_elem_length'.");
176 defaults_.setValidStrings(
"struc_elem_unit", ListUtils::create<String>(
"Thomson,DataPoints"));
178 defaults_.setValue(
"method",
"tophat",
"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 documentation of MorpthologicalFilter.");
179 defaults_.setValidStrings(
"method", ListUtils::create<String>(
"identity,erosion,dilation,opening,closing,gradient,tophat,bothat,erosion_simple,dilation_simple"));
200 template <
typename InputIterator,
typename OutputIterator>
201 void filterRange(InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
204 static std::vector<typename InputIterator::value_type> buffer;
205 const UInt size = input_end - input_begin;
208 if (struct_size_in_datapoints_ == 0)
210 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
214 String method = param_.getValue(
"method");
215 if (method ==
"identity")
217 std::copy(input_begin, input_end, output_begin);
219 else if (method ==
"erosion")
221 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
223 else if (method ==
"dilation")
225 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
227 else if (method ==
"opening")
229 if (buffer.size() < size) buffer.resize(size);
230 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
231 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
233 else if (method ==
"closing")
235 if (buffer.size() < size) buffer.resize(size);
236 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
237 applyErosion_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
239 else if (method ==
"gradient")
241 if (buffer.size() < size) buffer.resize(size);
242 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
243 applyDilation_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
244 for (
UInt i = 0; i < size; ++i) output_begin[i] -= buffer[i];
246 else if (method ==
"tophat")
248 if (buffer.size() < size) buffer.resize(size);
249 applyErosion_(struct_size_in_datapoints_, input_begin, input_end, buffer.begin());
250 applyDilation_(struct_size_in_datapoints_, buffer.begin(), buffer.begin() + size, output_begin);
251 for (
UInt i = 0; i < size; ++i) output_begin[i] = input_begin[i] - output_begin[i];
253 else if (method ==
"bothat")
255 if (buffer.size() < size) buffer.resize(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) output_begin[i] = input_begin[i] - output_begin[i];
260 else if (method ==
"erosion_simple")
262 applyErosionSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
264 else if (method ==
"dilation_simple")
266 applyDilationSimple_(struct_size_in_datapoints_, input_begin, input_end, output_begin);
269 struct_size_in_datapoints_ = 0;
292 if (spectrum.size() <= 1) {
return; }
295 if ((
String)(param_.getValue(
"struc_elem_unit")) ==
"Thomson")
297 const double struc_elem_length = (
double)param_.getValue(
"struc_elem_length");
298 const double mz_diff = spectrum.back().getMZ() - spectrum.begin()->getMZ();
299 struct_size_in_datapoints_ = (
UInt)(ceil(struc_elem_length*(
double)(spectrum.size() - 1)/mz_diff));
303 struct_size_in_datapoints_ = (
UInt)(
double)param_.getValue(
"struc_elem_length");
306 if (!
Math::isOdd(struct_size_in_datapoints_)) ++struct_size_in_datapoints_;
309 std::vector<Peak1D::IntensityType> output(spectrum.size());
316 for (
Size i = 0; i < spectrum.size(); ++i)
318 spectrum[i].setIntensity(output[i]);
330 startProgress(0, exp.
size(),
"filtering baseline");
331 for (
UInt i = 0; i < exp.
size(); ++i)
348 template <
typename InputIterator,
typename OutputIterator>
349 void applyErosion_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
351 typedef typename InputIterator::value_type ValueType;
352 const Int size = input_end - input;
353 const Int struc_size_half = struc_size / 2;
355 static std::vector<ValueType> buffer;
356 if (
Int(buffer.size()) < struc_size) buffer.resize(struc_size);
365 if (size <= struc_size || size <= 5)
367 applyErosionSimple_(struc_size, input, input_end, output);
373 for (++ii; ii < struc_size_half; ++ii) if (current > input[ii]) current = input[ii];
374 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
376 if (current > input[ii]) current = input[ii];
377 output[oi] = current;
382 for (anchor = struc_size;
383 anchor <= size - struc_size;
390 for (i = 1; i < struc_size; ++i, ++ii)
392 if (current > input[ii]) current = input[ii];
396 oi = ii + struc_size_half;
398 for (i = 1; i < struc_size; ++i, --ii, --oi)
400 if (current > input[ii]) current = input[ii];
401 output[oi] = std::min(buffer[struc_size - i], current);
403 if (current > input[ii]) current = input[ii];
404 output[oi] = current;
412 for (--ii; ii >= size - struc_size_half; --ii)
if (current > input[ii]) current = input[ii];
413 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
415 if (current > input[ii]) current = input[ii];
416 output[oi] = current;
418 anchor = size - struc_size;
422 for (i = 1; i < struc_size; ++i, ++ii)
424 if (current > input[ii]) current = input[ii];
428 oi = ii + struc_size_half;
430 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
432 if (current > input[ii]) current = input[ii];
433 output[oi] = std::min(buffer[struc_size - i], current);
437 if (current > input[ii]) current = input[ii];
438 output[oi] = current;
448 template <
typename InputIterator,
typename OutputIterator>
449 void applyDilation_(
Int struc_size, InputIterator input, InputIterator input_end, OutputIterator output)
451 typedef typename InputIterator::value_type ValueType;
452 const Int size = input_end - input;
453 const Int struc_size_half = struc_size / 2;
455 static std::vector<ValueType> buffer;
456 if (
Int(buffer.size()) < struc_size) buffer.resize(struc_size);
465 if (size <= struc_size || size <= 5)
467 applyDilationSimple_(struc_size, input, input_end, output);
473 for (++ii; ii < struc_size_half; ++ii)
if (current < input[ii]) current = input[ii];
474 for (; ii < std::min(
Int(struc_size), size); ++ii, ++oi)
476 if (current < input[ii]) current = input[ii];
477 output[oi] = current;
482 for (anchor = struc_size;
483 anchor <= size - struc_size;
490 for (i = 1; i < struc_size; ++i, ++ii)
492 if (current < input[ii]) current = input[ii];
496 oi = ii + struc_size_half;
498 for (i = 1; i < struc_size; ++i, --ii, --oi)
500 if (current < input[ii]) current = input[ii];
501 output[oi] = std::max(buffer[struc_size - i], current);
503 if (current < input[ii]) current = input[ii];
504 output[oi] = current;
512 for (--ii; ii >= size - struc_size_half; --ii)
if (current < input[ii]) current = input[ii];
513 for (; ii >= std::max(size -
Int(struc_size), 0); --ii, --oi)
515 if (current < input[ii]) current = input[ii];
516 output[oi] = current;
518 anchor = size - struc_size;
522 for (i = 1; i < struc_size; ++i, ++ii)
524 if (current < input[ii]) current = input[ii];
528 oi = ii + struc_size_half;
530 for (i = 1; (ii >= 0) && (i < struc_size); ++i, --ii, --oi)
532 if (current < input[ii]) current = input[ii];
533 output[oi] = std::max(buffer[struc_size - i], current);
537 if (current < input[ii]) current = input[ii];
538 output[oi] = current;
545 template <
typename InputIterator,
typename OutputIterator>
546 void applyErosionSimple_(
Int struc_size, InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
548 typedef typename InputIterator::value_type ValueType;
549 const int size = input_end - input_begin;
550 const Int struc_size_half = struc_size / 2;
551 for (
Int index = 0; index < size; ++index)
553 Int start = std::max(0, index - struc_size_half);
554 Int stop = std::min(size - 1, index + struc_size_half);
555 ValueType value = input_begin[start];
556 for (
Int i = start + 1; i <= stop; ++i) if (value > input_begin[i]) value = input_begin[i];
557 output_begin[index] = value;
563 template <
typename InputIterator,
typename OutputIterator>
566 typedef typename InputIterator::value_type ValueType;
567 const int size = input_end - input_begin;
568 const Int struc_size_half = struc_size / 2;
569 for (
Int index = 0; index < size; ++index)
571 Int start = std::max(0, index - struc_size_half);
572 Int stop = std::min(size - 1, index + struc_size_half);
573 ValueType value = input_begin[start];
574 for (
Int i = start + 1; i <= stop; ++i)
if (value < input_begin[i]) value = input_begin[i];
575 output_begin[index] = value;
UInt struct_size_in_datapoints_
Member for struct size in data points.
Definition: MorphologicalFilter.h:342
This class implements baseline filtering operations using methods from mathematical morphology...
Definition: MorphologicalFilter.h:161
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:127
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:119
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:449
IntensityIteratorWrapper(const IteratorT &rhs)
Definition: MorphologicalFilter.h:69
An iterator wrapper to access peak intensities instead of the peak itself.
Definition: MorphologicalFilter.h:60
value_type operator[](const IndexT &index)
Definition: MorphologicalFilter.h:80
A more convenient string class.
Definition: String.h:57
IteratorT::difference_type difference_type
Definition: MorphologicalFilter.h:67
MorphologicalFilter()
Constructor.
Definition: MorphologicalFilter.h:168
void filterRange(InputIterator input_begin, InputIterator input_end, OutputIterator output_begin)
Applies the morphological filtering operation to an iterator range.
Definition: MorphologicalFilter.h:201
IntensityIteratorWrapper & operator++()
Definition: MorphologicalFilter.h:90
IteratorT base
Definition: MorphologicalFilter.h:114
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
virtual ~MorphologicalFilter()
Destructor.
Definition: MorphologicalFilter.h:185
bool operator==(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:103
Raw data (also called profile data)
Definition: SpectrumSettings.h:75
Size size() const
Definition: MSExperiment.h:132
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
difference_type operator-(IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:85
void filter(MSSpectrum &spectrum)
Applies the morphological filtering operation to an MSSpectrum.
Definition: MorphologicalFilter.h:286
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
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, and used in some special cases.
Definition: MorphologicalFilter.h:564
void setType(SpectrumType type)
sets the spectrum type
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:349
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
IteratorT::value_type::IntensityType & reference
Definition: MorphologicalFilter.h:65
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
IteratorT::value_type::IntensityType * pointer
Definition: MorphologicalFilter.h:66
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, and used in some special cases.
Definition: MorphologicalFilter.h:546
void filterExperiment(PeakMap &exp)
Applies the morphological filtering operation to an MSExperiment.
Definition: MorphologicalFilter.h:328
bool operator!=(const IntensityIteratorWrapper &rhs) const
Definition: MorphologicalFilter.h:108
int Int
Signed integer type.
Definition: Types.h:103
IteratorT::value_type::IntensityType value_type
Definition: MorphologicalFilter.h:64
IntensityIteratorWrapper operator++(int)
Definition: MorphologicalFilter.h:96
value_type operator*()
Definition: MorphologicalFilter.h:74