IMP logo
IMP Reference Guide  2.19.0
The Integrative Modeling Platform
TIFFImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/TIFFImageReaderWriter.h
3  * \brief Management of reading/writing TIFF images
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM2D_TIFF_IMAGE_READER_WRITER_H
10 #define IMPEM2D_TIFF_IMAGE_READER_WRITER_H
11 
13 #include <boost/filesystem.hpp>
14 #include <IMP/log_macros.h>
15 
16 IMPEM2D_BEGIN_NAMESPACE
17 
18 //! Management of reading/writing TIFF images
20 
21  public:
23 
24  void read(const String &filename, em::ImageHeader &header,
25  cv::Mat &data) const override {
26  this->read_from_ints(filename, header, data);
27  }
28 
29  void write(const String &filename, em::ImageHeader &header,
30  const cv::Mat &data) const override {
31  this->write_to_ints(filename, header, data);
32  }
33 
34  void write_to_floats(const String &, em::ImageHeader &,
35  const cv::Mat &) const override {}
36 
37  void read_from_floats(const String &, em::ImageHeader &,
38  cv::Mat &) const override {}
39 
40  //! Reads an image file in TIFF format
41  /*!
42  \param[in] filename file to read
43  \param[in] header header to store the info
44  \param[in] data a matrix to store the grid of data of the image
45  \warning: The header passed is filled with standard values
46  If you need full header information you must work with other
47  format. Eg, Spider.
48  */
49  void read_from_ints(const String &filename, em::ImageHeader &header,
50  cv::Mat &data) const override {
51  IMP_LOG_VERBOSE("reading with TIFFImageReaderWriter" << std::endl);
52  // read
53  cv::Mat temp = cv::imread(filename, 0);
54  if (temp.empty()) {
55  IMP_THROW("Error reading from TIFF Image " << filename, IOException);
56  }
57  temp.assignTo(data, CV_64FC1);
58  // fill the header with default values
59  header.clear();
60  }
61 
62  //! Writes an EM image in TIFF format
63  /*!
64  \param[in] filename file to write
65  \param[in] header header with the image info
66  \param[in] data a matrix with the grid of data of the image
67  \warning: Careful: This function writes a 16-bit image.
68  You might be discarding float information.
69  */
70  void write_to_ints(const String &filename, em::ImageHeader &header,
71  const cv::Mat &data) const override {
72  IMP_UNUSED(header);
73  // discard header
74  IMP_LOG(IMP::WARNING,
75  "Writing with TIFFImageReaderWriter "
76  "discards image header "
77  << std::endl);
78  // check extension
79  String ext = boost::filesystem::extension(filename);
80  IMP_LOG_VERBOSE("TIFFImageReaderWriter writing to " << filename
81  << std::endl);
82  if (ext != ".tiff" && ext != ".tif") {
83  IMP_THROW(
84  "TIFFImageReaderWriter: The filename extension is not .tiff "
85  "or .tif",
86  IOException);
87  }
88 
89  // Convert the data value to 16-bit so it can be written as TIFF
90  cv::Mat TIFF_data;
91  double max, min;
92  cv::minMaxLoc(data, &min, &max);
93  double TIFF_max = 255;
94  double TIFF_min = 0;
95  double alpha = (TIFF_max - TIFF_min) / (max - min);
96  double beta = TIFF_min - alpha * min;
97  data.convertTo(TIFF_data, CV_8UC1, alpha, beta);
98  // write image
99  cv::minMaxLoc(TIFF_data, &min, &max);
100 
101  cv::imwrite(filename, TIFF_data);
102  }
103 
105 };
106 
107 IMPEM2D_END_NAMESPACE
108 
109 #endif /* IMPEM2D_TIFF_IMAGE_READER_WRITER_H */
Class to deal with the header of Electron Microscopy images in IMP.
Definition: ImageHeader.h:28
Output only warnings.
Definition: enums.h:26
void write_to_ints(const String &filename, em::ImageHeader &header, const cv::Mat &data) const override
Writes an EM image in TIFF format.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Virtual class for reader/writers of images.
void read_from_ints(const String &filename, em::ImageHeader &header, cv::Mat &data) const override
Reads an image file in TIFF format.
Virtual class for reader/writers of images Copyright 2007-2022 IMP Inventors. All rights reserved...
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
An input/output exception.
Definition: exception.h:173
#define IMP_UNUSED(variable)
Logging and error reporting support.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
void clear()
Clear header data and sets a consistent header.
std::string String
Basic string value.
Definition: types.h:43
Management of reading/writing TIFF images.