IMP  2.1.0
The Integrative Modeling Platform
TIFFImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file em2d/TIFFImageReaderWriter.h
3  * \brief Management of reading/writing TIFF images
4  *
5  * Copyright 2007-2013 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:
21 
23 
24  void read(const String &filename,
25  em::ImageHeader& header,
26  cv::Mat &data) const {
27  this->read_from_ints(filename, header,data);
28  }
29 
30  void write(const String &filename, em::ImageHeader& header,
31  const cv::Mat &data) const {
32  this->write_to_ints(filename, header,data);
33  }
34 
35  void write_to_floats(const String &, em::ImageHeader&, const cv::Mat &) const
36  {
37  }
38 
39  void read_from_floats(const String &, em::ImageHeader&, cv::Mat &) const {}
40 
41  //! Reads an image file in TIFF format
42  /*!
43  \param[in] filename file to read
44  \param[in] header header to store the info
45  \param[in] data a matrix to store the grid of data of the image
46  \warning: The header passed is filled with standard values
47  If you need full header information you must work with other
48  format. Eg, Spider.
49  */
50  void read_from_ints(const String &filename,
51  em::ImageHeader &header,cv::Mat &data) const {
52  IMP_LOG_VERBOSE("reading with TIFFImageReaderWriter" << std::endl);
53  // read
54  cv::Mat temp= cv::imread(filename,0);
55  if(temp.empty()) {
56  IMP_THROW("Error reading from TIFF Image " << filename,IOException);
57  }
58  temp.assignTo(data,CV_64FC1);
59  // fill the header with default values
60  header.clear();
61  }
62 
63  //! Writes an EM image in TIFF format
64  /*!
65  \param[in] filename file to write
66  \param[in] header header with the image info
67  \param[in] data a matrix with the grid of data of the image
68  \warning: Careful: This function writes a 16-bit image.
69  You might be discarding float information.
70  */
71  void write_to_ints(const String &filename,
72  em::ImageHeader& header,
73  const cv::Mat &data) const {
74  IMP_UNUSED(header);
75  // discard header
76  IMP_LOG(IMP::WARNING,"Writing with TIFFImageReaderWriter "
77  "discards image header " << std::endl);
78  // check extension
79  String ext=boost::filesystem::extension(filename);
80  IMP_LOG_VERBOSE("TIFFImageReaderWriter writting to "
81  << filename <<std::endl);
82  if(ext!=".tiff" && ext!=".tif") {
83  IMP_THROW("TIFFImageReaderWriter: The filename extension is not .tiff "
84  "or .tif",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 
106 IMPEM2D_END_NAMESPACE
107 
108 #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-2013 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)