IMP  2.1.1
The Integrative Modeling Platform
JPGImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file em2d/JPGImageReaderWriter.h
3  * \brief Management of JPG format for EM images
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_JPG_IMAGE_READER_WRITER_H
8 #define IMPEM2D_JPG_IMAGE_READER_WRITER_H
9 
10 #include "IMP/em2d/em2d_config.h"
12 #include "IMP/em/ImageHeader.h"
13 #include <boost/filesystem/convenience.hpp>
14 
15 IMPEM2D_BEGIN_NAMESPACE
16 
17 //! Class to read and write EM images in JPG format.
19 {
20 public:
22 
23  void read(const String &filename,
24  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  //! Reads an image file in JPG format
35  /*!
36  \param[in] filename file to read
37  \param[in] header header to store the info
38  \param[in] data a matrix to store the grid of data of the image
39  \warning: The header passed is filled with standard values
40  If you need full header information you must work with other
41  format. Eg, Spider.
42  */
43  void read_from_ints(const String &filename,
44  em::ImageHeader &header,cv::Mat &data) const {
45  IMP_LOG_VERBOSE("reading with JPGImageReaderWriter" << std::endl);
46  // read
47  cv::Mat temp= cv::imread(filename,0);
48  if(temp.empty()) {
49  IMP_THROW("Error reading from JPG Image " << filename,IOException);
50  }
51  temp.assignTo(data,CV_64FC1);
52  // fill the header with default values
53  header.clear();
54  }
55 
56  void write_to_floats(const String &, em::ImageHeader&, const cv::Mat &) const
57  {
58  }
59 
60  void read_from_floats(const String &, em::ImageHeader&, cv::Mat &) const {}
61 
62  //! Writes an EM image in JPG 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 8-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 {
72  IMP_UNUSED(header);
73  // discard header
74  IMP_LOG(IMP::WARNING,"Writing with JPGImageReaderWriter "
75  "discards image header " << std::endl);
76  // check extension
77  String ext=boost::filesystem::extension(filename);
78  IMP_LOG_VERBOSE("JPGImageReaderWriter writting to "
79  << filename <<std::endl);
80  if(ext!=".jpg" && ext!=".jpeg") {
81  IMP_THROW("JPGImageReaderWriter: The filename extension is not .jpg "
82  "or .jpeg",IOException);
83  }
84 
85  // Convert the data value to 8-bit so it can be written as JPG
86  cv::Mat jpg_data;
87  double max,min;
88  cv::minMaxLoc(data,&min,&max);
89  double jpg_max= 255;
90  double jpg_min=0;
91  double alpha = (jpg_max-jpg_min)/(max-min);
92  double beta = jpg_min-alpha*min;
93  data.convertTo(jpg_data,CV_8UC1,alpha,beta);
94  // write image
95  std::vector<int> flags;
96  flags.push_back(CV_IMWRITE_JPEG_QUALITY);
97  flags.push_back(100); // 100% quality image
98  cv::imwrite(filename,jpg_data,flags);
99  }
100 
102 };
103 
104 IMPEM2D_END_NAMESPACE
105 
106 #endif /* IMPEM2D_JPG_IMAGE_READER_WRITER_H */
Class to deal with the header of Electron Microsocopy images in IMP.
Definition: ImageHeader.h:28
Class to read and write EM images in JPG format.
Virtual class for reader/writers of images.
void write_to_ints(const String &filename, em::ImageHeader &header, const cv::Mat &data) const
Writes an EM image in JPG format.
#define IMP_UNUSED(variable)
inteface with OpenCV Copyright 2007-2013 IMP Inventors. All rights reserved.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
void clear()
Clear header data and sets a consistent header.
void read_from_ints(const String &filename, em::ImageHeader &header, cv::Mat &data) const
Reads an image file in JPG format.
Header for EM images. Compatible with Spider and Xmipp formats Copyright 2007-2013 IMP Inventors...
std::string String
Basic string value.
Definition: base/types.h:44
#define IMP_LOG_VERBOSE(expr)