7 #ifndef IMPEM2D_JPG_IMAGE_READER_WRITER_H
8 #define IMPEM2D_JPG_IMAGE_READER_WRITER_H
10 #include <IMP/em2d/em2d_config.h>
13 #include <boost/filesystem.hpp>
15 IMPEM2D_BEGIN_NAMESPACE
23 cv::Mat &data)
const override {
24 this->read_from_ints(filename, header, data);
28 const cv::Mat &data)
const override {
29 this->write_to_ints(filename, header, data);
42 cv::Mat &data)
const override {
45 cv::Mat temp = cv::imread(filename, 0);
49 temp.assignTo(data, CV_64FC1);
55 const cv::Mat &)
const override {}
58 cv::Mat &)
const override {}
69 const cv::Mat &data)
const override {
73 "Writing with JPGImageReaderWriter "
74 "discards image header "
77 String ext = boost::filesystem::path(filename).extension().string();
80 if (ext !=
".jpg" && ext !=
".jpeg") {
82 "JPGImageReaderWriter: The filename extension is not .jpg "
90 cv::minMaxLoc(data, &min, &max);
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);
97 std::vector<int> flags;
98 #if CV_VERSION_MAJOR >= 3
99 flags.push_back(cv::IMWRITE_JPEG_QUALITY);
101 flags.push_back(CV_IMWRITE_JPEG_QUALITY);
103 flags.push_back(100);
104 cv::imwrite(filename, jpg_data, flags);
110 IMPEM2D_END_NAMESPACE
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
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)
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.
#define IMP_UNUSED(variable)
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
void write_to_ints(const String &filename, em::ImageHeader &header, const cv::Mat &data) const override
Writes an EM image in JPG format.
std::string String
Basic string value.