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