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
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
59
60void
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
83void
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
93void
99
100void
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}
std::string timestamp()
virtual std::tuple< unsigned int, std::string > writeMetadataFrame(const CByteImage &frame, std::chrono::microseconds timestamp)
virtual void writeMetadataLine(const std::string &var_name, std::string_view var_type, std::string_view var_value)
void startRecording() override
Starts the recording manually if constructed empty.
AbstractSequencedRecordingStrategy()
Default constructor to manually start the recording.
std::filesystem::path deriveFramePath(const unsigned int sequence_number, const std::string &frame_name)
Returns the next sequenced full path and increments the sequence number.
void startRecording() override
Starts the recording manually if constructed empty.
static void recordSnapshot(const cv::Mat &image, const std::filesystem::path &path)
void recordFrame(const cv::Mat &frame, std::chrono::microseconds timestamp) override
Adds the given frame to the recording.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#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...
#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...
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition helper.cpp:40
ArmarX headers.