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}
30
32{
33 // parse the user parameters
35 yCDebug(IMAGEROTATION) << "user params:" << options.toString();
36 std::string str = options.find("carrier").asString();
38 yCDebug(IMAGEROTATION) << "parsed params:" << m_user_params.toString();
39
40 // get the value of the parameters
41 if (m_user_params.check("options_rotate")) {
42 m_options_rotate_str = m_user_params.find("options_rotate").asString();
43 }
44 if (m_user_params.check("options_flip")) {
45 m_options_flip_str = m_user_params.find("options_flip").asString();
46 }
47
48 // translate the parameters in opencv
49 if (m_options_rotate_str == std::string("rotate_cw")) {
50 m_rot_flags = cv::ROTATE_90_CLOCKWISE;
51 } else if (m_options_rotate_str == std::string("rotate_ccw")) {
52 m_rot_flags = cv::ROTATE_90_COUNTERCLOCKWISE;
53 } else if (m_options_rotate_str == std::string("rotate_180")) {
54 m_rot_flags = cv::ROTATE_180;
55 } else if (m_options_rotate_str == std::string("rotate_none")) {
56 } else {
57 yCDebug(IMAGEROTATION) << "Invalid value of `options_rotate` parameter";
58 return false;
59 }
60
61 if (m_options_flip_str == std::string("flip_x")) {
62 m_flip_code = 0;
63 } else if (m_options_flip_str == std::string("flip_y")) {
64 m_flip_code = 1;
65 } else if (m_options_flip_str == std::string("flip_xy")) {
66 m_flip_code = -1;
67 } else if (m_options_flip_str == std::string("flip_none")) {
68 } else {
69 yCDebug(IMAGEROTATION) << "Invalid value of `options_flip` parameter";
70 return false;
71 }
72
73 return true;
74}
75
79
80void split(const std::string& s, char delim, std::vector<std::string>& elements)
81{
82 std::istringstream iss(s);
83 std::string item;
84 while (std::getline(iss, item, delim)) {
85 elements.push_back(item);
86 }
87}
88
90{
91 // Split command line string using '+' delimiter
92 std::vector<std::string> parameters;
93 split(carrierString, '+', parameters);
94
95 // Iterate over result strings
96 for (std::string param : parameters) {
97 // If there is no '.', then the param is bad formatted, skip it.
98 auto pointPosition = param.find('.');
99 if (pointPosition == std::string::npos) {
100 continue;
101 }
102
103 // Otherwise, separate key and value
104 std::string paramKey = param.substr(0, pointPosition);
106 std::string s = param.substr(pointPosition + 1, param.length());
107 paramValue.fromString(s.c_str());
108
109 // and append to the returned property
110 prop.put(paramKey, paramValue);
111 }
112 return;
113}
114
116{
117 return false;
118}
119
121{
122 return false;
123}
124
126{
127 auto* img = thing.cast_as<yarp::sig::Image>();
128 if (img == nullptr) {
129 yCError(IMAGEROTATION, "Expected type Image, but got wrong data type!");
130 return false;
131 }
132 if (img->getPixelCode() != VOCAB_PIXEL_RGB && img->getPixelCode() != VOCAB_PIXEL_MONO_FLOAT) {
133 yCError(IMAGEROTATION, "Received image with invalid/unsupported pixelCode!");
134 return false;
135 }
136 return true;
137}
138
140{
141 yarp::sig::Image* yarpimg = thing.cast_as<yarp::sig::Image>();
142 if (yarpimg->getPixelCode() == VOCAB_PIXEL_RGB) {
143 m_cvInImage = yarp::cv::toCvMat(*yarpimg);
144
145 m_outImgRgb.resize(yarpimg->width(), yarpimg->height());
146 m_outImgRgb.zero();
147
148 if (m_options_flip_str == "flip_none" && m_options_rotate_str != "rotation_none") {
149 // just rotation
150 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
151 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage1);
152 } else if (m_options_flip_str != "flip_none" && m_options_rotate_str == "rotation_none") {
153 // just flip
154 cv::flip(m_cvInImage, m_cvOutImage1, m_flip_code);
155 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage1);
156 } else if (m_options_flip_str == "flip_none" && m_options_rotate_str == "rotation_none") {
157 // just copy
158 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvInImage);
159 } else {
160 // first a rotation, then a flip
161 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
162 cv::flip(m_cvOutImage1, m_cvOutImage2, m_flip_code);
163 m_outImgRgb = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(m_cvOutImage2);
164 }
165 m_th.setPortWriter(&m_outImgRgb);
166 } else if (yarpimg->getPixelCode() == VOCAB_PIXEL_MONO_FLOAT) {
167 m_cvInImage = yarp::cv::toCvMat(*yarpimg);
168
169 m_outImgFloat.resize(yarpimg->width(), yarpimg->height());
170 m_outImgFloat.zero();
171
172 if (m_options_flip_str == "flip_none") {
173 // just rotation
174 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
175 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage1);
176 } else if (m_options_flip_str == "rotation_none") {
177 // just flip
178 cv::flip(m_cvInImage, m_cvOutImage1, m_flip_code);
179 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage1);
180 } else {
181 // first a rotation, then a flip
182 cv::rotate(m_cvInImage, m_cvOutImage1, m_rot_flags);
183 cv::flip(m_cvOutImage1, m_cvOutImage2, m_flip_code);
184 m_outImgFloat = yarp::cv::fromCvMat<yarp::sig::PixelFloat>(m_cvOutImage2);
185 }
186 m_th.setPortWriter(&m_outImgFloat);
187 } else {
188 yCError(IMAGEROTATION, "Invalid Image type!");
189 }
190 return m_th;
191}
void split(const std::string &s, char delim, std::vector< std::string > &elements)
@ 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
void split(const std::string &s, char delim, std::vector< std::string > &elements)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
::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.