IMP  2.0.0
The Integrative Modeling Platform
Image.h
Go to the documentation of this file.
1 /**
2  * \file em2d/Image.h
3  * \brief IMP images for Electron Microscopy using openCV matrices
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_IMAGE_H
8 #define IMPEM2D_IMAGE_H
9 
10 #include "IMP/em2d/em2d_config.h"
15 #include "IMP/em2d/FFToperations.h"
16 #include "IMP/em/ImageHeader.h"
17 #include <IMP/Pointer.h>
18 #include <IMP/Object.h>
19 #include "IMP/Object.h"
20 #include "IMP/base_types.h"
21 #include <limits>
22 #include <typeinfo>
23 
24 
25 
26 
27 
28 
29 IMPEM2D_BEGIN_NAMESPACE
30 
31 //! 2D Electron Microscopy images in IMP
32 class IMPEM2DEXPORT Image : public IMP::base::Object
33 {
34 public:
35 
36  Image();
37 
38  /**
39  * Create an empty image
40  * @param rows The number of rows
41  * @param cols The number of columns
42  */
43  Image(int rows, int cols);
44 
45  /**
46  * Create an image reading from a file
47  * @param filename The name of the file containing the image
48  * @param reader The image reader to use
49  */
50  Image(String filename, const ImageReaderWriter *reader):
51  Object("Image%1%"){
52  read(filename,reader);
53  }
54 
55  /**
56  * Access to the matrix of data
57  * @return A reference to the matrix of data
58  */
59  cv::Mat &get_data() {
60  return data_;
61  }
62 
63  /**
64  * Sets the entire data of the matrix
65  * @param mat CV matrix containing the data for an image
66  */
67  void set_data(const cv::Mat &mat);
68 
69  /**
70  * Sets the image to zero
71  */
72  void set_zeros() {
73  set_value(0);
74  }
75 
76  /**
77  * Set all the pixels of an image
78  * @param val Value to use
79  * xxx
80  */
81  void set_value(double val) {
82  cv::Scalar s(val,0,0,0);
83  data_ = s;
84  }
85 
86  /**
87  * Sets the value for a pixel
88  * @param i row
89  * @param j column
90  * @param val value
91  */
92  void set_value(int i, int j, double val) {
93  data_.at<double>(i,j) = val;
94  }
95 
96  /**
97  * Recover the value of a pixel
98  * @param i row
99  * @param j colum
100  * @return the value
101  */
102  double operator()(int i, int j) const {
103  return data_.at<double>(i,j);
104  }
105 
106 
107  /**
108  * Access to the image header
109  * @return A reference to the header with the EM parameters
110  */
112  update_header();
113  return header_;
114  }
115 
116  /**
117  * Sets the size of the image
118  * @param rows
119  * @param cols
120  */
121  void set_size(int rows,int cols);
122 
123  /**
124  * Resize to the same size of the parameter image
125  * @param img The image to get the size from
126  */
127  void set_size(Image *img);
128 
129  //! Adjusts the information of the imager header taking into account the
130  //! dimensions of the data and setting the time, date, type, etc ...
131  /**
132  * Adjust the information of the image header taking into account the
133  * dimensions of the data and setting the time, date, type, etc ...
134  */
135  void update_header();
136 
137  /**
138  * Read an image from file
139  * @param filename The name of the file containing the image
140  * @param reader The image reader to use
141  */
142  void read(String filename, const ImageReaderWriter *reader) {
143  Pointer<const ImageReaderWriter> ptr(reader);
144  reader->read(filename,header_,data_);
145  }
146 
147  //! Writes the image to a file (the image matrix of data is stored as floats
148  //! when writing)
149  /**
150  * Writes the image to a file (the image matrix of data is stored as floats
151  * when writing)
152  * @param filename The output file
153  * @param writer The image writer to use
154  */
155  void write(const String &filename, const ImageReaderWriter *writer) {
156  update_header(); // adjust the header to guarantee consistence
157  writer->write(filename,header_,data_);
158  }
159 
160  /**
161  * Shows information about the class
162  * @param out Stream used to show the information
163  */
164  void show(std::ostream& out) const {
165  out << "Image name : " << name_ ;
166  header_.show(out);
167  }
168 
169  //! Define the basic things needed by any Object
170 // IMP_OBJECT_METHODS(Image)
172 
173  void destroyed_msg() {
174  IMP_LOG_TERSE( "Image destroyed " << this->name_ << std::endl);
175  }
176 
177  /**
178  * Set the name of the image
179  * @param name
180  */
181  void set_name(const String &name) {
182  name_ = name;
183  }
184 
185  /**
186  * Get the name of the image
187  * @return A string with the name
188  */
189  String get_name() const {
190  return name_;
191  }
192 
193  /**
194  * Recover the minimum and maximum values in the image
195  * @return A range. The first value is the minimum. The scond one is the
196  * maximum.
197  */
198  FloatRange get_min_and_max_values() const;
199 
200 
201 protected:
202  void set_size_data(int rows,int cols);
203  void set_defaults();
204 
205  //! Name of the image. Frequently it will be the name of the file
207  //! Matrix with the data for the image
208 // cv::Mat data_;
209  cv::Mat data_;
210  //! Header for the image with all the pertinent information
212 
213 }; // Image
214 
215 //! A vector of reference counted pointers to EM images of type double
217 
218 
219 
220 
221 //! Reads images from files (For compatibility with SPIDER format,
222 //! the images are read from floats)
223 /*!
224  \param[in] names filenames of the images
225  \param[in] rw reader/writer to use
226 */
227 IMPEM2DEXPORT Images read_images(const Strings &names,
228  const ImageReaderWriter *rw);
229 
230 //! Saves images to files (For compatibility with SPIDER format,
231 //! the images are written to floats)
232 /*!
233  \param[in] images Images to save
234  \param[in] names filenames of the images
235  \param[in] rw reader/writer to use
236 */
237 IMPEM2DEXPORT void save_images(Images images, const Strings &names,
238  const ImageReaderWriter *rw);
239 
240 //! Cross correlation between two images
241 /**
242  * Get the cross correlation coefficient between 2 images
243  * @param im1 First image
244  * @param im2 Second image
245  * @return The value of the coefficient. Ranges from 0 to 1
246  */
247 IMPEM2DEXPORT double get_cross_correlation_coefficient(Image *im1,Image *im2);
248 
249 /**
250  * Compute the autocorrelation for an image
251  * @param im1 The input image
252  * @param im2 The output image
253  */
254 inline void get_autocorrelation2d(Image *im1, Image *im2) {
255  get_autocorrelation2d(im1->get_data(),im2->get_data());
256 }
257 
258 /**
259  * Compute the cross correlation matrix between two images
260  * @param im1 First input image
261  * @param im2 First second image
262  * @param corr The output image of cross correlation values
263  */
264 inline void get_correlation2d(Image *im1,Image *im2,Image *corr) {
265  get_correlation2d(im1->get_data(),im2->get_data(),corr->get_data());
266 }
267 
268 /**
269  * Normalize an image subtracting the mean and dividing by the standard
270  * deviation
271  * @param im Image
272  * @param force If true, the image is normalized even if the header says that
273  * it is already normalized
274  */
275 IMPEM2DEXPORT void do_normalize(Image *im,bool force=false);
276 
277 
278 /**
279  * Gets an histogram of the values of the image
280  * @param img The image
281  * @param bins The number of bins to use to build the histogram
282  * @return The histogram: number of points per bin
283  */
284 inline Floats get_histogram(Image *img, int bins) {
285  return get_histogram(img->get_data(),bins);
286 }
287 
288 
289 /**
290  * Apply a variance filter to an image
291  * @param input Input image
292  * @param filtered The image containing the result
293  * @param kernelsize The size of the kernel to use. The matrix used as kernel
294  * will have the same number of rows and columns
295  *
296  */inline void apply_variance_filter(Image *input,
297  Image *filtered,int kernelsize) {
298  apply_variance_filter(input->get_data(),filtered->get_data(),kernelsize);
299 }
300 
301 /**
302  * Apply the diffusion filter
303  * @param input Input image
304  * @param filtered The image containing the result
305  * @param beta The beta parameter of the difussion filter
306  * @param pixelsize The pixel size of the image
307  * @param time_steps The number of time steps used for the diffusion
308  */
309 inline void apply_diffusion_filter(Image *input,
310  Image *filtered,
311  double beta,
312  double pixelsize,
313  int time_steps) {
315  filtered->get_data(),
316  beta,
317  pixelsize,
318  time_steps);
319 }
320 
321 
322 
323 inline void do_fill_holes(Image *input,Image *result,double n_stddevs) {
324  do_fill_holes(input->get_data(),result->get_data(),n_stddevs);
325 
326 }
327 
328 inline void do_combined_fill_holes_and_threshold(Image *input,
329  Image *result,
330  double n_stddevs) {
331  do_combined_fill_holes_and_threshold(input->get_data(),
332  result->get_data(),
333  n_stddevs);
334 }
335 
336 
337 
338 inline void do_extend_borders(Image *im1,Image *im2,unsigned int pix) {
339  do_extend_borders(im1->get_data(),im2->get_data(),pix);
340 }
341 
342 
343 inline void do_segmentation(Image *input,
344  Image *result,
345  const SegmentationParameters &params) {
346  do_segmentation(input->get_data(),result->get_data(),params);
347 }
348 
349 IMPEM2DEXPORT void do_remove_small_objects(Image *input,
350  double percentage,
351  int background=0,
352  int foreground=1);
353 
354 
355 /**
356  * Subtract two images
357  * @param first First image
358  * @param second The second image is subtracted from the second
359  * @param result Result image
360  */
361 IMPEM2DEXPORT void do_subtract_images(Image *first,Image *second,
362  Image *result);
363 
364 
365 IMPEM2DEXPORT void add_noise(Image *im1,double op1, double op2,
366  const String &mode = "uniform", double df = 3);
367 
368 
369 /**
370  * Resample an image to polar coordinates
371  * @param im1 The input image
372  * @param im2 The output image
373  * @param polar_params The parameters used for the sampling
374  */
375 IMPEM2DEXPORT void do_resample_polar(Image *im1,Image *im2,
376  const PolarResamplingParameters &polar_params);
377 
378 /** Crops an image.
379  * @param[in] img Image to crop. It is modified in place
380  * @param[in] center The pixel used as the center for cropping
381  * @param[in] size The size of the new image
382  */
383 IMPEM2DEXPORT void crop(Image *img, const IntPair &center, int size);
384 
385 
386 
387 /**
388  * Fills the values that are outside a circle centered at the center of the
389  * image with the mean of the values inside the circle the matrix center
390  * @param[in] img Image. It is modified in situ
391  * @param[in] radius of the circle
392 */
393 IMPEM2DEXPORT void apply_mean_outside_mask(Image *img, double radius);
394 
395 IMPEM2D_END_NAMESPACE
396 
397 #endif /* IMPEM2D_IMAGE_H */