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 
39 
40 void
41 visionx::imrec::convert(const CByteImage& in, cv::Mat& out)
42 {
43  // Initialise RGB OpenCV image from CByteImage
44  cv::Mat cv_frame(cv::Size(in.width, in.height), CV_8UC3, CV_MAT_CN(CV_8UC3));
45  std::memcpy(cv_frame.data, in.pixels, static_cast<unsigned int>(in.width * in.height * in.bytesPerPixel));
46 
47  // Convert to BGR OpenCV image (OpenCV assumes this format by default)
48  cv::cvtColor(cv_frame, cv_frame, cv::COLOR_RGB2BGR);
49  out = cv_frame;
50 }
51 
52 
53 void
54 visionx::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  // Initialise CByteImage and copy pixel data
61  out.Set(in.size().width, in.size().height, ::CByteImage::ImageType::eRGB24);
62  std::memcpy(out.pixels, frame_cpy.data, static_cast<unsigned int>(in.size().height * in.size().width * in.channels()));
63 }
64 
65 
66 std::string
67 visionx::imrec::datetime_to_string(std::chrono::microseconds ts)
68 {
69  std::chrono::time_point<std::chrono::system_clock> tp{ts};
70  time_t time = std::chrono::system_clock::to_time_t(tp);
71 
72  struct tm tstruct;
73  tstruct = *localtime(&time); // use locale time
74  //tstruct = *gmtime(&time); // use UTC time
75 
76  int time_us = ts.count() % 1000;
77  int time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(ts).count() % 1000;
78  int time_s = tstruct.tm_sec;
79  int time_m = tstruct.tm_min;
80  int time_h = tstruct.tm_hour;
81 
82  int date_d = tstruct.tm_mday;
83  int date_m = tstruct.tm_mon + 1;
84  int date_y = tstruct.tm_year + 1900;
85 
86  std::stringstream ss;
87  ss << std::setw(4) << std::setfill('0') << date_y << "-"; // year
88  ss << std::setw(2) << std::setfill('0') << date_m << "-"; // month
89  ss << std::setw(2) << std::setfill('0') << date_d << "_"; // day
90 
91  ss << std::setw(2) << std::setfill('0') << time_h << "-"; // hour
92  ss << std::setw(2) << std::setfill('0') << time_m << "-"; // minute
93  ss << std::setw(2) << std::setfill('0') << time_s << "-"; // second
94 
95  ss << std::setw(3) << std::setfill('0') << time_ms; // millisecond
96  ss << std::setw(3) << std::setfill('0') << time_us; // microsecond
97 
98  return ss.str();
99 }
100 
101 
102 std::string
103 visionx::imrec::timestamp_to_string(std::chrono::microseconds ts)
104 {
105  int us = ts.count() % 1000;
106  int ms = std::chrono::duration_cast<std::chrono::milliseconds>(ts).count() % 1000;
107  int s = std::chrono::duration_cast<std::chrono::seconds>(ts).count();
108  int m = std::chrono::duration_cast<std::chrono::minutes>(ts).count();
109  int h = std::chrono::duration_cast<std::chrono::hours>(ts).count();
110 
111  std::stringstream ss;
112  ss << std::setw(2) << std::setfill('0') << h << "_"; // hour
113  ss << std::setw(2) << std::setfill('0') << m << "_"; // minute
114  ss << std::setw(2) << std::setfill('0') << s << "_"; // second
115 
116  ss << std::setw(3) << std::setfill('0') << ms; // millisecond
117  ss << std::setw(3) << std::setfill('0') << us; // microsecond
118 
119  return ss.str();
120 }
visionx::imrec::convert
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's Mat.
Definition: helper.cpp:41
visionx::imrec::timestamp_to_string
std::string timestamp_to_string(std::chrono::microseconds ts)
Definition: helper.cpp:103
visionx::imrec::datetime_to_string
std::string datetime_to_string(std::chrono::microseconds ts)
Definition: helper.cpp:67
helper.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33