helper.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 <cstring>
29#include <iomanip>
30#include <sstream>
31
32// OpenCV 2
33#include <opencv2/core/core.hpp>
34#include <opencv2/imgproc/imgproc.hpp>
35
36// IVT
37#include <Image/ByteImage.h>
38
39void
40visionx::imrec::convert(const CByteImage& in, cv::Mat& out)
41{
42 // Initialise RGB OpenCV image from CByteImage
43 cv::Mat cv_frame(cv::Size(in.width, in.height), CV_8UC3, CV_MAT_CN(CV_8UC3));
44 std::memcpy(cv_frame.data,
45 in.pixels,
46 static_cast<unsigned int>(in.width * in.height * in.bytesPerPixel));
47
48 // Convert to BGR OpenCV image (OpenCV assumes this format by default)
49 cv::cvtColor(cv_frame, cv_frame, cv::COLOR_RGB2BGR);
50 out = cv_frame;
51}
52
53void
54visionx::imrec::convert(const cv::Mat& in, CByteImage& out)
55{
56 // Create a cv::Mat copy and convert to RGB format
57 cv::Mat frame_cpy(in.size(), in.type());
58 cv::cvtColor(in, frame_cpy, cv::COLOR_BGR2RGB);
59
60 visionx::imrec::convert_rgb2cbi(frame_cpy, out);
61}
62
63void
64visionx::imrec::convert_rgb2cbi(const cv::Mat& in, CByteImage& out)
65{
66 // TODO: Down the line this might be merged into convert with a conversion selection.
67 // Currently convert expects a BGR image but proceeds to convert to RGB, first.
68
69 // Initialise CByteImage and copy pixel data
70 out.Set(in.size().width, in.size().height, ::CByteImage::ImageType::eRGB24);
71 std::memcpy(out.pixels,
72 in.data,
73 static_cast<unsigned int>(in.size().height * in.size().width * in.channels()));
74}
75
76std::string
77visionx::imrec::datetime_to_string(std::chrono::microseconds ts)
78{
79 std::chrono::time_point<std::chrono::system_clock> tp{ts};
80 time_t time = std::chrono::system_clock::to_time_t(tp);
81
82 struct tm tstruct;
83 tstruct = *localtime(&time); // use locale time
84 //tstruct = *gmtime(&time); // use UTC time
85
86 int time_us = ts.count() % 1000;
87 int time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(ts).count() % 1000;
88 int time_s = tstruct.tm_sec;
89 int time_m = tstruct.tm_min;
90 int time_h = tstruct.tm_hour;
91
92 int date_d = tstruct.tm_mday;
93 int date_m = tstruct.tm_mon + 1;
94 int date_y = tstruct.tm_year + 1900;
95
96 std::stringstream ss;
97 ss << std::setw(4) << std::setfill('0') << date_y << "-"; // year
98 ss << std::setw(2) << std::setfill('0') << date_m << "-"; // month
99 ss << std::setw(2) << std::setfill('0') << date_d << "_"; // day
100
101 ss << std::setw(2) << std::setfill('0') << time_h << "-"; // hour
102 ss << std::setw(2) << std::setfill('0') << time_m << "-"; // minute
103 ss << std::setw(2) << std::setfill('0') << time_s << "-"; // second
104
105 ss << std::setw(3) << std::setfill('0') << time_ms; // millisecond
106 ss << std::setw(3) << std::setfill('0') << time_us; // microsecond
107
108 return ss.str();
109}
110
111std::string
112visionx::imrec::timestamp_to_string(std::chrono::microseconds ts)
113{
114 int us = ts.count() % 1000;
115 int ms = std::chrono::duration_cast<std::chrono::milliseconds>(ts).count() % 1000;
116 int s = std::chrono::duration_cast<std::chrono::seconds>(ts).count();
117 int m = std::chrono::duration_cast<std::chrono::minutes>(ts).count();
118 int h = std::chrono::duration_cast<std::chrono::hours>(ts).count();
119
120 std::stringstream ss;
121 ss << std::setw(2) << std::setfill('0') << h << "_"; // hour
122 ss << std::setw(2) << std::setfill('0') << m << "_"; // minute
123 ss << std::setw(2) << std::setfill('0') << s << "_"; // second
124
125 ss << std::setw(3) << std::setfill('0') << ms; // millisecond
126 ss << std::setw(3) << std::setfill('0') << us; // microsecond
127
128 return ss.str();
129}
std::string timestamp_to_string(std::chrono::microseconds ts)
Definition helper.cpp:112
std::string datetime_to_string(std::chrono::microseconds ts)
Definition helper.cpp:77
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition helper.cpp:40
void convert_rgb2cbi(const cv::Mat &in, CByteImage &out)
Converts an OpenCV RGB Mat to IVT's CByteImage.
Definition helper.cpp:64