IMP logo
IMP Reference Guide  develop.d36d3deffb,2026/11/09
The Integrative Modeling Platform
image_processing.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/image_processing.h
3  * \brief Image processing functions
4  *
5  * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 */
7 
8 #ifndef IMPEM2D_IMAGE_PROCESSING_H
9 #define IMPEM2D_IMAGE_PROCESSING_H
10 
11 #include <IMP/em2d/em2d_config.h>
13 #include "IMP/em2d/Matrix.h"
14 #include "IMP/base_types.h"
15 #include <algorithm>
16 #include <cmath>
17 #include <cereal/access.hpp>
18 
19 IMPEM2D_BEGIN_NAMESPACE
20 
21 //! Class to provide all the parameters to the segmentation function
22 class IMPEM2DEXPORT SegmentationParameters {
23 private:
24  friend class cereal::access;
25 
26  template<class Archive> void serialize(Archive &ar) {
27  ar(image_pixel_size, diffusion_beta, diffusion_timesteps,
28  fill_holes_stddevs, opening_kernel, remove_sizing_percentage,
29  binary_background, binary_foreground, threshold);
30  }
31 
32  public:
33  double image_pixel_size;
34  double diffusion_beta;
35  double diffusion_timesteps;
36  double fill_holes_stddevs;
37  DoubleMatrix opening_kernel;
38  double remove_sizing_percentage;
39  int binary_background;
40  int binary_foreground;
41  double threshold; // values of the image below the threshold will be set to 0
42 
44  : image_pixel_size(1),
45  diffusion_beta(45),
46  diffusion_timesteps(200),
47  fill_holes_stddevs(1.0),
48  remove_sizing_percentage(0.1),
49  binary_background(0),
50  binary_foreground(1),
51  threshold(0.0) {
52  opening_kernel = DoubleMatrix::Ones(3, 3);
53  }
54 
55  SegmentationParameters(double apix, double diff_beta,
56  unsigned int diff_timesteps, double fh_stddevs,
57  const DoubleMatrix &kr, int background, int foreground)
58  : image_pixel_size(apix),
59  diffusion_beta(diff_beta),
60  diffusion_timesteps(diff_timesteps),
61  fill_holes_stddevs(fh_stddevs),
62  opening_kernel(kr),
63  binary_background(background),
64  binary_foreground(foreground) {};
65 
66  void show(std::ostream &out = std::cout) const {
67  out << "Diffusion parameters: " << std::endl;
68  out << "image_pixel_size " << image_pixel_size << std::endl;
69  out << "diffusion_beta " << diffusion_beta << std::endl;
70  out << "diffusion_timesteps " << diffusion_timesteps << std::endl;
71  out << "fill_holes_stddevs " << fill_holes_stddevs << std::endl;
72  out << "Opening kernel " << opening_kernel.rows() << "x"
73  << opening_kernel.cols() << std::endl;
74  out << "binary_background " << binary_background << std::endl;
75  out << "binary_foreground " << binary_foreground << std::endl;
76  }
77 };
79 
80 /*! Information for the match of a template in one image. The pair is the
81  pixel of the image where the match is found.
82 */
83 class IMPEM2DEXPORT MatchTemplateResult {
84  public:
85  IntPair pair;
86  double cross_correlation;
87  MatchTemplateResult(IntPair p, double cc) : pair(p), cross_correlation(cc) {}
88 
90 
91  void show(std::ostream &out = std::cout) const {
92  out << "MatchTemplateResult: Pair (" << pair.first << "," << pair.second
93  << ") ccc = " << cross_correlation << std::endl;
94  }
95 private:
96  friend class cereal::access;
97 
98  template<class Archive> void serialize(Archive &ar) {
99  ar(pair.first, pair.second, cross_correlation);
100  }
101 };
103 
104 //! Applies a binary mask to an image.
105 /*!
106  \param[in] m Input matrix
107  \param[in] result matrix with the result
108  \param[in] mask If the mask is 0, the result has the value of val. Otherwise
109  is the value of m.
110  \param[in] val value to apply when the mask is 0
111 */
112 IMPEM2DEXPORT void apply_mask(const DoubleMatrix &m, DoubleMatrix &result,
113  const IntMatrix &mask, double val);
114 
115 //! Applies a circular to a matrix. The center of the mask is the center of the
116 //! matrix.
117 //! \deprecated_at{2.26} This function is not used in IMP, and will be removed.
118 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
119 IMPEM2DEXPORT void apply_circular_mask(const DoubleMatrix &mat,
120  DoubleMatrix &result,
121  int radius, double value = 0.0);
122 
123 /*! Mean of an matrix inside a mask
124  \param[in] mat The matrix
125  \param[in] mask If a pixel is not zero, is considered for computing the mean
126  \returns The mean
127 */
128 IMPEM2DEXPORT double get_mean(const DoubleMatrix &mat, const IntMatrix &mask);
129 
130 /*! Creates a circular matrix that is a mask
131  \param[in] rows
132  \param[in] cols
133  \param[in] radius The radius of the mask. The center of the mask is the
134  center of the matrix
135  \returns A binary matrix (0/1) with value 1 inside the radius
136 */
137 IMPEM2DEXPORT IntMatrix create_circular_mask(int rows, int cols, int radius);
138 
139 //! Removes small objects from a matrix of integers.
140 /*!
141  \param[in] m the matrix
142  \param[in] percentage The percentage respect to the largest object that
143  other objects have to be in order to survive the removal.
144  \param[in] background value for the background after removed
145  \param[in] foreground value for the foreground after removed
146 */
147 IMPEM2DEXPORT void do_remove_small_objects(IntMatrix &m, double percentage,
148  int background = 0,
149  int foreground = 1);
150 
151 //! \deprecated_at{2.26} This function is not used in IMP, and will be removed.
152 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
153 IMPEM2DEXPORT void do_histogram_stretching(DoubleMatrix &m, int boxes,
154  int offset);
155 
156 //! (U. Adiga, 2005)
157 /*!
158  \param[in] m binary matrix to dilate and shrink
159  \param[in] grayscale grayscale matrix that controls the shrinking
160  \param[in] kernel dilation kernel
161  \note Only tested with binary matrices m with background =0 and foreground = 1
162  \deprecated_at{2.26} This function is not used in IMP, and will be removed.
163 */
164 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
165 void IMPEM2DEXPORT do_dilate_and_shrink_warp(DoubleMatrix &m,
166  const DoubleMatrix &grayscale,
167  DoubleMatrix &kernel);
168 
169 //! morphologic grayscale reconstruction (L Vincent, 1993)
170 /*!
171  \param[in] mask image to reconstruct
172  \param[out] marker this image contains the initial marker points and will
173  contain the final result
174  \param[in] neighbors_mode number of neighbors for a pixel to consider when
175  doing the morphologic reconstruction (values: 4, 8).
176 */
177 void IMPEM2DEXPORT do_morphologic_reconstruction(const DoubleMatrix &mask,
178  DoubleMatrix &marker,
179  int neighbors_mode = 4);
180 
181 //! Labeling function for a matrix
182 /*!
183  \param[in] m binary matrix to scan. The matrix needs to contain zeros and
184  ones but they can be stored as doubles, floats or ints
185  \param[out] mat_to_label matrix it is returned as a matrix of ints
186  \return labels The number of labels in the image
187 */
188 IMPEM2DEXPORT int do_labeling(const IntMatrix &m, IntMatrix &mat_to_label);
189 
190 //! Segmentation of images
191 /*!
192  \param[in] m The EM image to segment
193  \param[in] result The segmented image, with the shape of the molecule
194  \param[in] params
195 */
196 IMPEM2DEXPORT void do_segmentation(const DoubleMatrix &m, DoubleMatrix &result,
197  const SegmentationParameters &params);
198 
199 //! Smoothing filter by application of the reaction-diffusion
200 //! equation of Beltrami flow. Adiga, JSB, 2005
201 /*!
202  `beta` is the contribution of diffusion versus edge enhancement:
203  0 - pure reaction, 90 - pure diffusion
204  \note The function only works for matrices containing doubles
205 */
206 IMPEM2DEXPORT void apply_diffusion_filter(const DoubleMatrix &m,
207  DoubleMatrix &result,
208  double beta, double pixelsize,
209  unsigned int time_steps);
210 
211 //! Partial derivative with respect to time for an image filtered with
212 //! diffusion-reaction
213 /*!
214  \param[in] m input matrix
215  \param[in] der output derivative matrix
216  \param[in] dx - step for x
217  \param[in] dy - step for y
218  \param[in] ang - parameter for weight diffusion and edge detection (90-0)
219 */
221  const DoubleMatrix &m, DoubleMatrix &der, double dx, double dy, double ang);
222 
223 //! Fills the holes in the matrix m of height h
224 /*!
225  \param[in] m the input matrix
226  \param[in] result the result matrix
227  \param[in] h the height
228  \note The function does not work in-place
229 */
230 IMPEM2DEXPORT void do_fill_holes(const DoubleMatrix &m, DoubleMatrix &result,
231  double h);
232 
233 //! Gets the domes of m with height h
234 /*!
235  \param[in] m the input matrix
236  \param[in] result the result matrix
237  \param[in] h the height
238  \note The function does not work in-place
239  \deprecated_at{2.26} This function is not used in IMP, and will be removed.
240 */
241 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
242 IMPEM2DEXPORT void get_domes(DoubleMatrix &m, DoubleMatrix &result, double h);
243 
244 //! Combines the fill holes and thresholding operations together with normalize
245 /*!
246  \param[in] m Input matrix
247  \param[out] result the result matrix
248  \param[in] n_stddevs Number of standard deviations used to compute the holes
249  \param[in] threshold Removes values below the threshold value
250  \note The function does normalize -> fill_holes -> normalize -> threshold ->
251  normalize
252 */
254  DoubleMatrix &result,
255  double n_stddevs,
256  double threshold = 0.0);
257 
258 //! Computes the histogram of a matrix.
259 /*!
260  \param[in] m Matrix with the data
261  \param[in] bins Number of bins to use in the histogram
262  \return vector with the values for each bin
263 */
264 IMPEM2DEXPORT Floats get_histogram(const DoubleMatrix &m, int bins);
265 
266 //! Variance filter for an image. Computes the variance for each pixel using
267 //! the surrounding ones.
268 /*!
269  \param[in] input image with the data
270  \param[out] filtered matrix result of the filtering with the variances
271  \param[in] kernelsize The variance is computed using kernelsize x kernelsize
272  pixels around each one. Kernelsize can only be odd.
273 */
274 IMPEM2DEXPORT void apply_variance_filter(const DoubleMatrix &input,
275  DoubleMatrix &filtered,
276  int kernelsize);
277 
278 //!Add noise to the values of a matrix.
279 /*!
280  Supported distributions:
281  - uniform distribution, giving the range (lower, upper). DEFAULT
282  - Gaussian distribution, giving the mean and the standard deviation
283  \code
284  add_noise(v1,0, 1);
285  // uniform distribution between 0 and 1
286 
287  v1.add_noise(0, 1, "uniform");
288  // the same
289 
290  v1.add_noise(0, 1, "gaussian");
291  // Gaussian distribution with 0 mean and stddev=1
292 
293  \endcode
294 */
295 IMPEM2DEXPORT void add_noise(DoubleMatrix &v, double op1, double op2,
296  const String &mode = "uniform", double df = 3);
297 
298 //! Resamples a matrix to polar coordinates.
299 /*!
300  \param[in] input matrix to resample
301  \param[out] resampled result matrix to contain the resampling
302  \param[in] polar_params Parameters used for the resampling. Extremely useful
303  for speeding up the procedure if they are given with the
304  transformation maps, that can be built in the
305  PolarResamplingParameters class
306 */
307 IMPEM2DEXPORT void do_resample_polar(
308  const DoubleMatrix &input, DoubleMatrix &resampled,
309  const PolarResamplingParameters &polar_params);
310 
311 //! Normalize a matrix to mean 0 and stddev 1. It is done in place
312 IMPEM2DEXPORT void do_normalize(DoubleMatrix &m);
313 
314 //! Applies a transformation to a matrix. First rotates the matrix using the
315 //! matrix center as the origin of the rotation, and then applies
316 //! the translation
317 IMPEM2DEXPORT void get_transformed(const DoubleMatrix &input,
318  DoubleMatrix &transformed,
319  const algebra::Transformation2D &T);
320 
321 /*! Extends the borders of an image
322  \param[in] orig The image to extend
323  \param[in] dst The image destination
324  \param[in] pix number of pixels to extend the borders
325 */
326 IMPEM2DEXPORT void do_extend_borders(const DoubleMatrix &orig,
327  DoubleMatrix &dst, unsigned int pix);
328 
329 /*! Applies a threshold to an image
330  `threshold` is a value such that all pixels below this value are set to zero
331 */
332 IMPEM2DEXPORT void apply_threshold(DoubleMatrix &m, DoubleMatrix &result,
333  double threshold = 0.0);
334 
335 /*! morphologic enhancement of the contrast
336  This function detects areas in the images and enhances the contrast between
337  them
338  \param[in] m Input matrix
339  \param[out] result
340  \param[in] kernel morphologic kernel to use
341  \param[in] iterations Higher number, more contrast
342  \deprecated_at{2.26} This function is not used in IMP, and will be removed.
343 */
344 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
345 IMPEM2DEXPORT void do_morphologic_contrast_enhancement(
346  const DoubleMatrix &m, DoubleMatrix &result, const DoubleMatrix &kernel,
347  unsigned int iterations);
348 /*! Morphologic gradient: dilation-erosion
349  \param[in] m Input matrix
350  \param[out] result
351  \param[in] kernel morphologic kernel
352  \deprecated_at{2.26} This function is not used in IMP, and will be removed.
353 */
354 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
355 IMPEM2DEXPORT void get_morphologic_gradient(const DoubleMatrix &m,
356  DoubleMatrix &result,
357  const DoubleMatrix &kernel);
358 
359 /*! Get the percentage of overlap between two matrices.
360  Two images are overlapping in a pixel if both have values > 0.
361  The overlap is (pixels overlapping) / (pixels > 0 in m2)
362 
363  \param[in] m1 First matrix
364  \param[in] m2 Matrix used to check the overlap. This matrix can be of the
365  same size of m1 or smaller.
366  \param[in] center Indicates the position of m1 where to put
367  the center m2. E.g., if center is (32,16) the center
368  of m2 will be in the pixel (32,16) of m1.
369  \deprecated_at{2.26} This function is not used in IMP, and will be removed.
370 */
371 IMPEM2D_DEPRECATED_FUNCTION_DECL(2.26)
372 IMPEM2DEXPORT double get_overlap_percentage(DoubleMatrix &m1, DoubleMatrix &m2,
373  const IntPair &center);
374 
375 /*! Gets the n first matches between an image and a template
376  \param[in] m Matrix
377  \param[in] templ Matrix with a template to be found in m
378  \param[in] n Number of positions to recover
379 */
380 IMPEM2DEXPORT MatchTemplateResults
381  get_best_template_matches(const DoubleMatrix &m, const DoubleMatrix &templ,
382  unsigned int n);
383 
384 /*! Crop an image
385  \param[in] m Matrix to crop
386  \param[in] center The pixel used as the center for cropping
387  \param[in] size The size of the new image
388  \return A matrix with the cropped region
389 */
390 IMPEM2DEXPORT DoubleMatrix crop(const DoubleMatrix &m, const IntPair &center,
391  int size);
392 
393 IMPEM2D_END_NAMESPACE
394 
395 #endif /* IMPEM2D_IMAGE_PROCESSING_H */
void do_dilate_and_shrink_warp(DoubleMatrix &m, const DoubleMatrix &grayscale, DoubleMatrix &kernel)
(U. Adiga, 2005)
Floats get_histogram(const DoubleMatrix &m, int bins)
Computes the histogram of a matrix.
void do_combined_fill_holes_and_threshold(DoubleMatrix &m, DoubleMatrix &result, double n_stddevs, double threshold=0.0)
Combines the fill holes and thresholding operations together with normalize.
Basic types used by IMP.
void get_morphologic_gradient(const DoubleMatrix &m, DoubleMatrix &result, const DoubleMatrix &kernel)
void do_segmentation(const DoubleMatrix &m, DoubleMatrix &result, const SegmentationParameters &params)
Segmentation of images.
DoubleMatrix crop(const DoubleMatrix &m, const IntPair &center, int size)
Class to provide all the parameters to the segmentation function.
void apply_mask(const DoubleMatrix &m, DoubleMatrix &result, const IntMatrix &mask, double val)
Applies a binary mask to an image.
IntMatrix create_circular_mask(int rows, int cols, int radius)
void do_histogram_stretching(DoubleMatrix &m, int boxes, int offset)
void do_extend_borders(const DoubleMatrix &orig, DoubleMatrix &dst, unsigned int pix)
void do_resample_polar(const DoubleMatrix &input, DoubleMatrix &resampled, const PolarResamplingParameters &polar_params)
Resamples a matrix to polar coordinates.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
void get_domes(DoubleMatrix &m, DoubleMatrix &result, double h)
Gets the domes of m with height h.
void do_morphologic_reconstruction(const DoubleMatrix &mask, DoubleMatrix &marker, int neighbors_mode=4)
morphologic grayscale reconstruction (L Vincent, 1993)
double get_mean(const DoubleMatrix &mat, const IntMatrix &mask)
void apply_diffusion_filter(const DoubleMatrix &m, DoubleMatrix &result, double beta, double pixelsize, unsigned int time_steps)
void do_normalize(DoubleMatrix &m)
Normalize a matrix to mean 0 and stddev 1. It is done in place.
void apply_threshold(DoubleMatrix &m, DoubleMatrix &result, double threshold=0.0)
MatchTemplateResults get_best_template_matches(const DoubleMatrix &m, const DoubleMatrix &templ, unsigned int n)
Matrices used to store image data Copyright 2007-2026 IMP Inventors. All rights reserved.
Functions related with rotations in em2d Copyright 2007-2026 IMP Inventors. All rights reserved...
void do_remove_small_objects(IntMatrix &m, double percentage, int background=0, int foreground=1)
Removes small objects from a matrix of integers.
void get_diffusion_filtering_partial_derivative(const DoubleMatrix &m, DoubleMatrix &der, double dx, double dy, double ang)
void apply_circular_mask(const DoubleMatrix &mat, DoubleMatrix &result, int radius, double value=0.0)
int do_labeling(const IntMatrix &m, IntMatrix &mat_to_label)
Labeling function for a matrix.
void apply_variance_filter(const DoubleMatrix &input, DoubleMatrix &filtered, int kernelsize)
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
void do_morphologic_contrast_enhancement(const DoubleMatrix &m, DoubleMatrix &result, const DoubleMatrix &kernel, unsigned int iterations)
void do_fill_holes(const DoubleMatrix &m, DoubleMatrix &result, double h)
Fills the holes in the matrix m of height h.
Eigen::Array< int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > IntMatrix
Matrix of ints, used for masks and labels.
Definition: Matrix.h:32
DensityMap * get_transformed(const DensityMap *input, const algebra::Transformation3D &tr, double threshold)
Return a new density map containing a rotated version of the old one.
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > DoubleMatrix
Matrix of doubles, used to store the data of an image.
Definition: Matrix.h:28
double get_overlap_percentage(DoubleMatrix &m1, DoubleMatrix &m2, const IntPair &center)
void add_noise(DoubleMatrix &v, double op1, double op2, const String &mode="uniform", double df=3)
Add noise to the values of a matrix.
std::string String
Basic string value.
Definition: types.h:43