IMP logo
IMP Reference Guide  2.20.2
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-2022 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 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  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 column
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 image 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) {
126  reader->set_was_used(true);
128  reader->read(filename, header_, data_);
129  }
130 
131  //! Writes the image to a file (the image matrix of data is stored as floats
132  //! when writing)
133  /**
134  * Writes the image to a file (the image matrix of data is stored as floats
135  * when writing)
136  * @param filename The output file
137  * @param writer The image writer to use
138  */
139  void write(const String &filename, const ImageReaderWriter *writer) {
140  update_header(); // adjust the header to guarantee consistence
141  writer->write(filename, header_, data_);
142  }
143 
144  /**
145  * Shows information about the class
146  * @param out Stream used to show the information
147  */
148  void show(std::ostream &out) const {
149  out << "Image name : " << name_;
150  header_.show(out);
151  }
152 
153  //! Define the basic things needed by any Object
154  // IMP_OBJECT_METHODS(Image)
156 
157  /**
158  * Set the name of the image
159  * @param name
160  */
161  void set_name(const String &name) { name_ = name; }
162 
163  /**
164  * Get the name of the image
165  * @return A string with the name
166  */
167  String get_name() const { return name_; }
168 
169  /**
170  * Recover the minimum and maximum values in the image
171  * @return A range. The first value is the minimum. The second one is the
172  * maximum.
173  */
174  FloatRange get_min_and_max_values() const;
175 
176  protected:
177  void set_size_data(int rows, int cols);
178  void set_defaults();
179 
180  //! Name of the image. Frequently it will be the name of the file
182  //! Matrix with the data for the image
183  // cv::Mat data_;
184  cv::Mat data_;
185  //! Header for the image with all the pertinent information
187 
188 }; // Image
189 
190 //! A vector of reference counted pointers to EM images of type double
192 
193 //! Reads images from files (For compatibility with SPIDER format,
194 //! the images are read from floats)
195 /*!
196  \param[in] names filenames of the images
197  \param[in] rw reader/writer to use
198 */
199 IMPEM2DEXPORT Images
200  read_images(const Strings &names, const ImageReaderWriter *rw);
201 
202 //! Saves images to files (For compatibility with SPIDER format,
203 //! the images are written to floats)
204 /*!
205  \param[in] images Images to save
206  \param[in] names filenames of the images
207  \param[in] rw reader/writer to use
208 */
209 IMPEM2DEXPORT void save_images(Images images, const Strings &names,
210  const ImageReaderWriter *rw);
211 
212 //! Cross correlation between two images
213 /**
214  * Get the cross correlation coefficient between 2 images
215  * @param im1 First image
216  * @param im2 Second image
217  * @return The value of the coefficient. Ranges from 0 to 1
218  */
219 IMPEM2DEXPORT double get_cross_correlation_coefficient(Image *im1, Image *im2);
220 
221 /**
222  * Compute the autocorrelation for an image
223  * @param im1 The input image
224  * @param im2 The output image
225  */
226 inline void get_autocorrelation2d(Image *im1, Image *im2) {
227  get_autocorrelation2d(im1->get_data(), im2->get_data());
228 }
229 
230 /**
231  * Compute the cross correlation matrix between two images
232  * @param im1 First input image
233  * @param im2 First second image
234  * @param corr The output image of cross correlation values
235  */
236 inline void get_correlation2d(Image *im1, Image *im2, Image *corr) {
237  get_correlation2d(im1->get_data(), im2->get_data(), corr->get_data());
238 }
239 
240 /**
241  * Normalize an image subtracting the mean and dividing by the standard
242  * deviation
243  * @param im Image
244  * @param force If true, the image is normalized even if the header says that
245  * it is already normalized
246  */
247 IMPEM2DEXPORT void do_normalize(Image *im, bool force = false);
248 
249 /**
250  * Gets an histogram of the values of the image
251  * @param img The image
252  * @param bins The number of bins to use to build the histogram
253  * @return The histogram: number of points per bin
254  */
255 inline Floats get_histogram(Image *img, int bins) {
256  return get_histogram(img->get_data(), bins);
257 }
258 
259 /**
260  * Apply a variance filter to an image
261  * @param input Input image
262  * @param filtered The image containing the result
263  * @param kernelsize The size of the kernel to use. The matrix used as kernel
264  * will have the same number of rows and columns
265  *
266  */ inline void apply_variance_filter(Image *input, Image *filtered,
267  int kernelsize) {
268  apply_variance_filter(input->get_data(), filtered->get_data(), kernelsize);
269 }
270 
271 /**
272  * Apply the diffusion filter
273  * @param input Input image
274  * @param filtered The image containing the result
275  * @param beta The beta parameter of the diffusion filter
276  * @param pixelsize The pixel size of the image
277  * @param time_steps The number of time steps used for the diffusion
278  */
279 inline void apply_diffusion_filter(Image *input, Image *filtered, double beta,
280  double pixelsize, int time_steps) {
281  apply_diffusion_filter(input->get_data(), filtered->get_data(), beta,
282  pixelsize, time_steps);
283 }
284 
285 inline void do_fill_holes(Image *input, Image *result, double n_stddevs) {
286  do_fill_holes(input->get_data(), result->get_data(), n_stddevs);
287 }
288 
289 inline void do_combined_fill_holes_and_threshold(Image *input, Image *result,
290  double n_stddevs) {
291  do_combined_fill_holes_and_threshold(input->get_data(), result->get_data(),
292  n_stddevs);
293 }
294 
295 inline void do_extend_borders(Image *im1, Image *im2, unsigned int pix) {
296  do_extend_borders(im1->get_data(), im2->get_data(), pix);
297 }
298 
299 inline void do_segmentation(Image *input, Image *result,
300  const SegmentationParameters &params) {
301  do_segmentation(input->get_data(), result->get_data(), params);
302 }
303 
304 IMPEM2DEXPORT void do_remove_small_objects(Image *input, double percentage,
305  int background = 0,
306  int foreground = 1);
307 
308 /**
309  * Subtract two images
310  * @param first First image
311  * @param second The second image is subtracted from the second
312  * @param result Result image
313  */
314 IMPEM2DEXPORT void do_subtract_images(Image *first, Image *second,
315  Image *result);
316 
317 IMPEM2DEXPORT void add_noise(Image *im1, double op1, double op2,
318  const String &mode = "uniform", double df = 3);
319 
320 /**
321  * Resample an image to polar coordinates
322  * @param im1 The input image
323  * @param im2 The output image
324  * @param polar_params The parameters used for the sampling
325  */
326 IMPEM2DEXPORT void do_resample_polar(
327  Image *im1, Image *im2, const PolarResamplingParameters &polar_params);
328 
329 /** Crops an image.
330  * @param[in] img Image to crop. It is modified in place
331  * @param[in] center The pixel used as the center for cropping
332  * @param[in] size The size of the new image
333  */
334 IMPEM2DEXPORT void crop(Image *img, const IntPair &center, int size);
335 
336 /**
337  * Fills the values that are outside a circle centered at the center of the
338  * image with the mean of the values inside the circle the matrix center
339  * @param[in] img Image. It is modified in situ
340  * @param[in] radius of the circle
341 */
342 IMPEM2DEXPORT void apply_mean_outside_mask(Image *img, double radius);
343 
344 IMPEM2D_END_NAMESPACE
345 
346 #endif /* IMPEM2D_IMAGE_H */
Image processing functions.
Basic types used by IMP.
void set_name(const String &name)
Definition: Image.h:161
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:167
#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:266
void apply_diffusion_filter(Image *input, Image *filtered, double beta, double pixelsize, int time_steps)
Definition: Image.h:279
Virtual class for reader/writers of images.
em::ImageHeader header_
Header for the image with all the pertinent information.
Definition: Image.h:186
Virtual class for reader/writers of images Copyright 2007-2022 IMP Inventors. All rights reserved...
Interface with OpenCV Copyright 2007-2022 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:80
em::ImageHeader & get_header()
Definition: Image.h:94
Floats get_histogram(Image *img, int bins)
Definition: Image.h:255
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:181
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
Common base class for heavy weight IMP objects.
Definition: Object.h:111
void get_correlation2d(Image *im1, Image *im2, Image *corr)
Definition: Image.h:236
Images read_images(const Strings &names, const ImageReaderWriter *rw)
Functions related with rotations in em2d Copyright 2007-2022 IMP Inventors. All rights reserved...
void crop(Image *img, const IntPair &center, int size)
double operator()(int i, int j) const
Definition: Image.h:88
cv::Mat data_
Matrix with the data for the image.
Definition: Image.h:184
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:139
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:226
void show(std::ostream &out) const
Definition: Image.h:148
Header for EM images. Compatible with Spider and Xmipp formats Copyright 2007-2022 IMP Inventors...
Operations involving FFT Copyright 2007-2022 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