IMP logo
IMP Reference Guide  2.20.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-2022 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 override {
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 override {
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 override {
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 override {}
56 
57  void read_from_floats(const String &, em::ImageHeader &,
58  cv::Mat &) const override {}
59 
60  //! Writes an EM image in JPG 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 8-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 override {
70  IMP_UNUSED(header);
71  // discard header
72  IMP_LOG(IMP::WARNING,
73  "Writing with JPGImageReaderWriter "
74  "discards image header "
75  << std::endl);
76  // check extension
77  String ext = boost::filesystem::extension(filename);
78  IMP_LOG_VERBOSE("JPGImageReaderWriter writing to " << filename
79  << std::endl);
80  if (ext != ".jpg" && ext != ".jpeg") {
81  IMP_THROW(
82  "JPGImageReaderWriter: The filename extension is not .jpg "
83  "or .jpeg",
84  IOException);
85  }
86 
87  // Convert the data value to 8-bit so it can be written as JPG
88  cv::Mat jpg_data;
89  double max, min;
90  cv::minMaxLoc(data, &min, &max);
91  double jpg_max = 255;
92  double jpg_min = 0;
93  double alpha = (jpg_max - jpg_min) / (max - min);
94  double beta = jpg_min - alpha * min;
95  data.convertTo(jpg_data, CV_8UC1, alpha, beta);
96  // write image
97  std::vector<int> flags;
98 #if CV_VERSION_MAJOR >= 3
99  flags.push_back(cv::IMWRITE_JPEG_QUALITY);
100 #else
101  flags.push_back(CV_IMWRITE_JPEG_QUALITY);
102 #endif
103  flags.push_back(100); // 100% quality image
104  cv::imwrite(filename, jpg_data, flags);
105  }
106 
108 };
109 
110 IMPEM2D_END_NAMESPACE
111 
112 #endif /* IMPEM2D_JPG_IMAGE_READER_WRITER_H */
Class to deal with the header of Electron Microscopy images in IMP.
Definition: ImageHeader.h:28
Output only warnings.
Definition: enums.h:26
#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.
Interface with OpenCV Copyright 2007-2022 IMP Inventors. All rights reserved.
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
void read_from_ints(const String &filename, em::ImageHeader &header, cv::Mat &data) const override
Reads an image file in JPG format.
An input/output exception.
Definition: exception.h:173
#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 write_to_ints(const String &filename, em::ImageHeader &header, const cv::Mat &data) const override
Writes an EM image in JPG format.
Header for EM images. Compatible with Spider and Xmipp formats Copyright 2007-2022 IMP Inventors...
std::string String
Basic string value.
Definition: types.h:43