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  visionx::imrec::convert_rgb2cbi(frame_cpy, out);
61 }
62 
63 
64 void
65 visionx::imrec::convert_rgb2cbi(const cv::Mat& in, CByteImage& out)
66 {
67  // TODO: Down the line this might be merged into convert with a conversion selection.
68  // Currently convert expects a BGR image but proceeds to convert to RGB, first.
69 
70  // Initialise CByteImage and copy pixel data
71  out.Set(in.size().width, in.size().height, ::CByteImage::ImageType::eRGB24);
72  std::memcpy(out.pixels, in.data, static_cast<unsigned int>(in.size().height * in.size().width * in.channels()));
73 }
74 
75 
76 std::string
77 visionx::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 
111 
112 std::string
113 visionx::imrec::timestamp_to_string(std::chrono::microseconds ts)
114 {
115  int us = ts.count() % 1000;
116  int ms = std::chrono::duration_cast<std::chrono::milliseconds>(ts).count() % 1000;
117  int s = std::chrono::duration_cast<std::chrono::seconds>(ts).count();
118  int m = std::chrono::duration_cast<std::chrono::minutes>(ts).count();
119  int h = std::chrono::duration_cast<std::chrono::hours>(ts).count();
120 
121  std::stringstream ss;
122  ss << std::setw(2) << std::setfill('0') << h << "_"; // hour
123  ss << std::setw(2) << std::setfill('0') << m << "_"; // minute
124  ss << std::setw(2) << std::setfill('0') << s << "_"; // second
125 
126  ss << std::setw(3) << std::setfill('0') << ms; // millisecond
127  ss << std::setw(3) << std::setfill('0') << us; // microsecond
128 
129  return ss.str();
130 }
visionx::imrec::convert_rgb2cbi
void convert_rgb2cbi(const cv::Mat &in, CByteImage &out)
Converts an OpenCV RGB Mat to IVT's CByteImage.
Definition: helper.cpp:65
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::timestamp_to_string
std::string timestamp_to_string(std::chrono::microseconds ts)
Definition: helper.cpp:113
visionx::imrec::datetime_to_string
std::string datetime_to_string(std::chrono::microseconds ts)
Definition: helper.cpp:77
helper.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33