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