PNGRecordingStrategy.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package visionx::imrec
17  * @author Christian R. G. Dreher <christian.dreher@student.kit.edu>
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // STD/STL
28 #include <filesystem>
29 #include <vector>
30 
31 // OpenCV 2
32 #include <opencv2/opencv.hpp>
33 
34 // ArmarX
36 
38 
40 {
41  // pass
42 }
43 
45  const std::filesystem::path& filePath,
46  const std::string& file_name,
47  unsigned int png_compression) :
48  visionx::imrec::AbstractSequencedRecordingStrategy(filePath / file_name, ".png"),
49  m_png_compression{png_compression}
50 {
51  ARMARX_CHECK_GREATER_EQUAL(png_compression, 1) << "Compression cannot be lower than 1.";
52  ARMARX_CHECK_LESS_EQUAL(png_compression, 9) << "Compression cannot be greater than 9.";
53 }
54 
56 {
57  // pass
58 }
59 
60 void
62  const std::filesystem::path& path)
63 {
64  std::filesystem::path snapshotPath(path);
65 
66  // Make sure that path does exist, ensure PNG extension
67  ARMARX_CHECK_EXPRESSION(std::filesystem::exists(path.parent_path()))
68  << "Cannot take snapshot, path '" + path.parent_path().string() + "' does ot exist";
69 
70  if (snapshotPath.extension() != ".png")
71  {
72  snapshotPath += ".png";
73  }
74 
75  const int snapshot_compression = 9;
76  std::vector<int> params{cv::IMWRITE_PNG_COMPRESSION,
77  snapshot_compression,
78  cv::IMWRITE_PNG_STRATEGY,
79  cv::IMWRITE_PNG_STRATEGY_RLE};
80  cv::imwrite(snapshotPath.string(), image, params);
81 }
82 
83 void
85  const std::filesystem::path& path)
86 {
87  // Covert to OpenCV image and run the CV method
88  cv::Mat cv_image;
89  visionx::imrec::convert(image, cv_image);
90  recordSnapshot(cv_image, path);
91 }
92 
93 void
95 {
97  writeMetadataLine("png_compression", "unsigned int", std::to_string(m_png_compression));
98 }
99 
100 void
102  const std::chrono::microseconds timestamp)
103 {
104  const auto& [sequence_number, frame_name] = writeMetadataFrame(frame, timestamp);
105  const std::filesystem::path path = deriveFramePath(sequence_number, frame_name);
106  std::vector<int> params{cv::IMWRITE_PNG_COMPRESSION,
107  static_cast<int>(m_png_compression),
108  cv::IMWRITE_PNG_STRATEGY,
109  cv::IMWRITE_PNG_STRATEGY_RLE};
110  cv::imwrite(path.string(), frame, params);
111 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::imrec::AbstractSequencedRecordingStrategy
An object of this class behaves likee a normal recording, but is in fact a sequence of images.
Definition: AbstractSequencedRecordingStrategy.h:40
visionx::imrec::strats::PNGRecordingStrategy::recordFrame
void recordFrame(const cv::Mat &frame, std::chrono::microseconds timestamp) override
Adds the given frame to the recording.
Definition: PNGRecordingStrategy.cpp:101
visionx::imrec::convert
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition: helper.cpp:40
visionx::imrec::strats::PNGRecordingStrategy::recordSnapshot
static void recordSnapshot(const cv::Mat &image, const std::filesystem::path &path)
Definition: PNGRecordingStrategy.cpp:61
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
ARMARX_CHECK_LESS_EQUAL
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
Definition: ExpressionException.h:109
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
visionx::imrec::strats::PNGRecordingStrategy::PNGRecordingStrategy
PNGRecordingStrategy()
Definition: PNGRecordingStrategy.cpp:39
visionx::imrec::strats::PNGRecordingStrategy::~PNGRecordingStrategy
~PNGRecordingStrategy() override
Definition: PNGRecordingStrategy.cpp:55
helper.h
PNGRecordingStrategy.h
visionx::imrec::AbstractSequencedRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AbstractSequencedRecordingStrategy.cpp:65
visionx::imrec::strats::PNGRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: PNGRecordingStrategy.cpp:94