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