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