YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
imageRotation.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "imageRotation.h"
7
9#include <yarp/os/LogStream.h>
10
11#include <yarp/sig/Image.h>
12
13#include <algorithm>
14#include <cmath>
15#include <opencv2/highgui/highgui.hpp>
16#include <opencv2/imgproc/imgproc.hpp>
17#include <opencv2/videoio/videoio.hpp>
18
19using namespace yarp::os;
20using namespace yarp::sig;
21
22namespace {
24 "yarp.carrier.portmonitor.image_rotation",
28 nullptr)
29
30void split(const std::string& s, char delim, std::vector<std::string>& elements)
31{
32 std::istringstream iss(s);
33 std::string item;
34 while (std::getline(iss, item, delim))
35 {
36 elements.push_back(item);
37 }
38}
39} //anonymous namespace
40
42{
43 // parse the user parameters
45 yCDebug(IMAGEROTATION) << "user params:" << options.toString();
46 std::string str = options.find("carrier").asString();
48 yCDebug(IMAGEROTATION) << "parsed params:" << m_user_params.toString();
49
50 // get the value of the parameters
51 if (m_user_params.check("options_rotate")) {
52 m_options_rotate_str = m_user_params.find("options_rotate").asString();
53 }
54 if (m_user_params.check("options_flip")) {
55 m_options_flip_str = m_user_params.find("options_flip").asString();
56 }
57
58 // translate the parameters in opencv
59 if (m_options_rotate_str == std::string("rotate_cw")) {
60 m_rot_flags = cv::ROTATE_90_CLOCKWISE;
61 } else if (m_options_rotate_str == std::string("rotate_ccw")) {
62 m_rot_flags = cv::ROTATE_90_COUNTERCLOCKWISE;
63 } else if (m_options_rotate_str == std::string("rotate_180")) {
64 m_rot_flags = cv::ROTATE_180;
65 } else if (m_options_rotate_str == std::string("rotate_none")) {
66 } else {
67 yCDebug(IMAGEROTATION) << "Invalid value of `options_rotate` parameter";
68 return false;
69 }
70
71 if (m_options_flip_str == std::string("flip_x")) {
72 m_flip_code = 0;
73 } else if (m_options_flip_str == std::string("flip_y")) {
74 m_flip_code = 1;
75 } else if (m_options_flip_str == std::string("flip_xy")) {
76 m_flip_code = -1;
77 } else if (m_options_flip_str == std::string("flip_none")) {
78 } else {
79 yCDebug(IMAGEROTATION) << "Invalid value of `options_flip` parameter";
80 return false;
81 }
82
83 return true;
84}
85
89
91{
92 // Split command line string using '+' delimiter
93 std::vector<std::string> parameters;
94 split(carrierString, '+', parameters);
95
96 // Iterate over result strings
97 for (std::string param : parameters) {
98 // If there is no '.', then the param is bad formatted, skip it.
99 auto pointPosition = param.find('.');
100 if (pointPosition == std::string::npos) {
101 continue;
102 }
103
104 // Otherwise, separate key and value
105 std::string paramKey = param.substr(0, pointPosition);
107 std::string s = param.substr(pointPosition + 1, param.length());
108 paramValue.fromString(s.c_str());
109
110 // and append to the returned property
111 prop.put(paramKey, paramValue);
112 }
113 return;
114}
115
117{
118 return false;
119}
120
122{
123 return false;
124}
125
127{
128 auto* img = thing.cast_as<yarp::sig::Image>();
129 if (img == nullptr) {
130 yCError(IMAGEROTATION, "Expected type Image, but got wrong data type!");
131 return false;
132 }
133 if (img->getPixelCode() != VOCAB_PIXEL_RGB && img->getPixelCode() != VOCAB_PIXEL_MONO_FLOAT) {
134 yCError(IMAGEROTATION, "Received image with invalid/unsupported pixelCode!");
135 return false;
136 }
137 return true;
138}
139
141{
142 yarp::sig::Image* yarpimg = thing.cast_as<yarp::sig::Image>();
143 if (yarpimg->getPixelCode() == VOCAB_PIXEL_RGB) {
144 m_cvInImage = yarp::cv::toCvMat(*yarpimg);
145
146 m_outImgRgb.resize(yarpimg->width(), yarpimg->height());
147 m_outImgRgb.zero();
148
149 if (m_options_flip_str == "flip_none" && m_options_rotate_str != "rotation_none") {
150 // just rotation
151 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
152 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage1);
153 } else if (m_options_flip_str != "flip_none" && m_options_rotate_str == "rotation_none") {
154 // just flip
155 cv::flip(m_cvInImage, m_cvOutImage1, m_flip_code);
156 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage1);
157 } else if (m_options_flip_str == "flip_none" && m_options_rotate_str == "rotation_none") {
158 // just copy
159 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvInImage);
160 } else {
161 // first a rotation, then a flip
162 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
163 cv::flip(m_cvOutImage1, m_cvOutImage2, m_flip_code);
164 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage2);
165 }
166 m_th.setPortWriter(&m_outImgRgb);
167 } else if (yarpimg->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT) {
168 m_cvInImage = yarp::cv::toCvMat(*yarpimg);
169
170 m_outImgFloat.resize(yarpimg->width(), yarpimg->height());
171 m_outImgFloat.zero();
172
173 if (m_options_flip_str == "flip_none") {
174 // just rotation
175 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
176 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage1);
177 } else if (m_options_flip_str == "rotation_none") {
178 // just flip
179 cv::flip(m_cvInImage, m_cvOutImage1, m_flip_code);
180 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage1);
181 } else {
182 // first a rotation, then a flip
183 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
184 cv::flip(m_cvOutImage1, m_cvOutImage2, m_flip_code);
185 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage2);
186 }
187 m_th.setPortWriter(&m_outImgFloat);
188 } else {
189 yCError(IMAGEROTATION, "Invalid Image type!");
190 }
191 return m_th;
192}
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_RGB
Definition Image.h:44
bool accept(yarp::os::Things &thing) override
This will be called when the data reach the portmonitor object.
bool setparam(const yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are set via YARP admin port.
void destroy() override
This will be called when the portmonitor object destroyes.
void getParamsFromCommandLine(std::string carrierString, yarp::os::Property &prop)
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
bool getparam(yarp::os::Property &params) override
This will be called when the portmonitor carrier parameters are requested via YARP admin port.
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
A mini-server for performing network communication in the background.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:873
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:833
@ LogTypeReserved
Definition Log.h:98
A class for storing options and configuration information.
Definition Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
Base class for generic things.
Definition Things.h:18
T * cast_as()
Definition Things.h:53
void setPortWriter(yarp::os::PortWriter *writer)
Set the reference to a PortWriter object.
Definition Things.cpp:26
A single value (typically within a Bottle).
Definition Value.h:43
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
Base class for storing images.
Definition Image.h:79
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:402
void zero()
Set all pixels to 0.
Definition Image.cpp:395
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:390
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
::cv::Mat toCvMat(yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
Definition Cv-inl.h:84
An interface to the operating system, including Port based communication.