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 <vector>
29 #include <filesystem>
30 
31 // OpenCV 2
32 #include <opencv2/opencv.hpp>
33 
34 // ArmarX
37 
38 
40 {
41  // pass
42 }
43 
44 
45 visionx::imrec::strats::PNGRecordingStrategy::PNGRecordingStrategy(const std::filesystem::path& filePath, const std::string& file_name, unsigned int png_compression) :
46  visionx::imrec::AbstractSequencedRecordingStrategy(filePath / file_name, ".png"),
47  m_png_compression{png_compression}
48 {
49  ARMARX_CHECK_GREATER_EQUAL(png_compression, 1) << "Compression cannot be lower than 1.";
50  ARMARX_CHECK_LESS_EQUAL(png_compression, 9) << "Compression cannot be greater than 9.";
51 }
52 
53 
55 {
56  // pass
57 }
58 
59 
60 void
61 visionx::imrec::strats::PNGRecordingStrategy::recordSnapshot(const cv::Mat& image, const std::filesystem::path& path)
62 {
63  std::filesystem::path snapshotPath(path);
64 
65  // Make sure that path does exist, ensure PNG extension
66  ARMARX_CHECK_EXPRESSION(std::filesystem::exists(path.parent_path())) << "Cannot take snapshot, path '" + path.parent_path().string() + "' does ot exist";
67 
68  if (snapshotPath.extension() != ".png")
69  {
70  snapshotPath += ".png";
71  }
72 
73  const int snapshot_compression = 9;
74  std::vector<int> params {cv::IMWRITE_PNG_COMPRESSION, snapshot_compression,
75  cv::IMWRITE_PNG_STRATEGY, cv::IMWRITE_PNG_STRATEGY_RLE};
76  cv::imwrite(snapshotPath.string(), image, params);
77 }
78 
79 
80 void
81 visionx::imrec::strats::PNGRecordingStrategy::recordSnapshot(const CByteImage& image, const std::filesystem::path& path)
82 {
83  // Covert to OpenCV image and run the CV method
84  cv::Mat cv_image;
85  visionx::imrec::convert(image, cv_image);
86  recordSnapshot(cv_image, path);
87 }
88 
89 
90 void
92 {
94  writeMetadataLine("png_compression", "unsigned int", std::to_string(m_png_compression));
95 }
96 
97 
98 void
99 visionx::imrec::strats::PNGRecordingStrategy::recordFrame(const cv::Mat& frame, const std::chrono::microseconds timestamp)
100 {
101  const auto& [sequence_number, frame_name] = writeMetadataFrame(frame, timestamp);
102  const std::filesystem::path path = deriveFramePath(sequence_number, frame_name);
103  std::vector<int> params {cv::IMWRITE_PNG_COMPRESSION, static_cast<int>(m_png_compression),
104  cv::IMWRITE_PNG_STRATEGY, cv::IMWRITE_PNG_STRATEGY_RLE};
105  cv::imwrite(path.string(), frame, params);
106 }
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:41
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:99
visionx::imrec::convert
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition: helper.cpp:41
visionx::imrec::strats::PNGRecordingStrategy::recordSnapshot
static void recordSnapshot(const cv::Mat &image, const std::filesystem::path &path)
Definition: PNGRecordingStrategy.cpp:61
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:40
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:54
helper.h
PNGRecordingStrategy.h
visionx::imrec::AbstractSequencedRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AbstractSequencedRecordingStrategy.cpp:67
visionx::imrec::strats::PNGRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: PNGRecordingStrategy.cpp:91