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