IMP  2.3.0
The Integrative Modeling Platform
JPGImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/JPGImageReaderWriter.h
3  * \brief Management of JPG format for EM images
4  * Copyright 2007-2014 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  public:
21 
22  void read(const String &filename, em::ImageHeader &header,
23  cv::Mat &data) const {
24  this->read_from_ints(filename, header, data);
25  }
26 
27  void write(const String &filename, em::ImageHeader &header,
28  const cv::Mat &data) const {
29  this->write_to_ints(filename, header, data);
30  }
31 
32  //! Reads an image file in JPG format
33  /*!
34  \param[in] filename file to read
35  \param[in] header header to store the info
36  \param[in] data a matrix to store the grid of data of the image
37  \warning: The header passed is filled with standard values
38  If you need full header information you must work with other
39  format. Eg, Spider.
40  */
41  void read_from_ints(const String &filename, em::ImageHeader &header,
42  cv::Mat &data) const {
43  IMP_LOG_VERBOSE("reading with JPGImageReaderWriter" << std::endl);
44  // read
45  cv::Mat temp = cv::imread(filename, 0);
46  if (temp.empty()) {
47  IMP_THROW("Error reading from JPG Image " << filename, IOException);
48  }
49  temp.assignTo(data, CV_64FC1);
50  // fill the header with default values
51  header.clear();
52  }
53 
54  void write_to_floats(const String &, em::ImageHeader &,
55  const cv::Mat &) const {}
56 
57  void read_from_floats(const String &, em::ImageHeader &, cv::Mat &) const {}
58 
59  //! Writes an EM image in JPG format
60  /*!
61  \param[in] filename file to write
62  \param[in] header header with the image info
63  \param[in] data a matrix with the grid of data of the image
64  \warning: Careful: This function writes a 8-bit image.
65  You might be discarding float information.
66  */
67  void write_to_ints(const String &filename, em::ImageHeader &header,
68  const cv::Mat &data) const {
69  IMP_UNUSED(header);
70  // discard header
71  IMP_LOG(IMP::base::WARNING,
72  "Writing with JPGImageReaderWriter "
73  "discards image header "
74  << std::endl);
75  // check extension
76  String ext = boost::filesystem::extension(filename);
77  IMP_LOG_VERBOSE("JPGImageReaderWriter writing to " << filename
78  << std::endl);
79  if (ext != ".jpg" && ext != ".jpeg") {
80  IMP_THROW(
81  "JPGImageReaderWriter: The filename extension is not .jpg "
82  "or .jpeg",
83  IOException);
84  }
85 
86  // Convert the data value to 8-bit so it can be written as JPG
87  cv::Mat jpg_data;
88  double max, min;
89  cv::minMaxLoc(data, &min, &max);
90  double jpg_max = 255;
91  double jpg_min = 0;
92  double alpha = (jpg_max - jpg_min) / (max - min);
93  double beta = jpg_min - alpha * min;
94  data.convertTo(jpg_data, CV_8UC1, alpha, beta);
95  // write image
96  std::vector<int> flags;
97  flags.push_back(CV_IMWRITE_JPEG_QUALITY);
98  flags.push_back(100); // 100% quality image
99  cv::imwrite(filename, jpg_data, flags);
100  }
101 
103 };
104 
105 IMPEM2D_END_NAMESPACE
106 
107 #endif /* IMPEM2D_JPG_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
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.
Interface with OpenCV Copyright 2007-2014 IMP Inventors. All rights reserved.
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:94
#define IMP_UNUSED(variable)
#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.
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-2014 IMP Inventors...
std::string String
Basic string value.
Definition: types.h:44