YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ImageUtils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7#include <cstring>
8#include <algorithm> // std::math
9#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12#if defined (YARP_HAS_OPENCV)
13#include <yarp/cv/Cv.h>
14#include <opencv2/highgui/highgui.hpp>
15#include <opencv2/imgproc/imgproc.hpp>
16#include <opencv2/videoio/videoio.hpp>
17#endif
18
19using namespace yarp::sig;
20
21static bool checkImages(const Image& bigImg, const Image& smallImg1, const Image& smallImg2)
22{
23 return bigImg.getPixelCode() == smallImg1.getPixelCode() &&
24 bigImg.getPixelCode() == smallImg2.getPixelCode() &&
25 smallImg1.width() == smallImg2.width() &&
26 smallImg1.height() == smallImg2.height() &&
27 bigImg.getRawImageSize() == 2*smallImg1.getRawImageSize();
28
29}
30
31
32bool utils::vertSplit(const Image& inImg, Image& outImgL, Image& outImgR)
33{
34 outImgL.resize(inImg.width()/2, inImg.height());
35 outImgR.resize(inImg.width()/2, inImg.height());
36
37 if (!checkImages(inImg, outImgL, outImgR)) {
38 return false;
39 }
40
41 size_t inHeight = inImg.height();
42 size_t singleImage_rowSizeByte = outImgL.getRowSize();
43 unsigned char *pixelLeft = outImgL.getRawImage();
44 unsigned char *pixelRight = outImgR.getRawImage();
45 unsigned char *pixelInput = inImg.getRawImage();
46
47 for(size_t h=0; h<inHeight; h++)
48 {
49 // Copy the memory
50 memcpy(pixelLeft + h*singleImage_rowSizeByte, pixelInput, singleImage_rowSizeByte);
51 memcpy(pixelRight + h*singleImage_rowSizeByte, pixelInput+=singleImage_rowSizeByte, singleImage_rowSizeByte);
52
53 // Update the pointers
54 pixelInput+= singleImage_rowSizeByte;
55 }
56 return true;
57}
58
59bool utils::horzSplit(const Image& inImg, Image& outImgUp, Image& outImgDown)
60{
61 outImgUp.resize(inImg.width(), inImg.height()/2);
62 outImgDown.resize(inImg.width(), inImg.height()/2);
63
64 if (!checkImages(inImg, outImgUp, outImgDown)) {
65 return false;
66 }
67 // Copy the memory
68 size_t imgSize = outImgUp.getRawImageSize();
69 memcpy(outImgUp.getRawImage(), inImg.getRawImage(), imgSize);
70 memcpy(outImgDown.getRawImage(), inImg.getRawImage() + imgSize, imgSize);
71 return true;
72}
73
74
75
76bool utils::horzConcat(const Image& inImgL, const Image& inImgR, Image& outImg)
77{
78 outImg.resize(inImgL.width()*2, inImgL.height());
79
80 if (!checkImages(outImg, inImgL, inImgR)) {
81 return false;
82 }
83
84 size_t singleImage_rowSizeByte = inImgL.getRowSize();
85 unsigned char * pixelLeft = inImgL.getRawImage();
86 unsigned char * pixelRight = inImgR.getRawImage();
87 unsigned char * pixelOutLeft = outImg.getRawImage();
88 unsigned char * pixelOutRight = outImg.getRawImage() + singleImage_rowSizeByte;
89
90 size_t height = inImgL.height();
91
92 for(size_t h=0; h<height; h++)
93 {
94 // Copy the memory
95 memcpy(pixelOutLeft, pixelLeft, singleImage_rowSizeByte);
96 memcpy(pixelOutRight, pixelRight, singleImage_rowSizeByte);
97
98 // Update the pointers
99 pixelOutLeft += 2*singleImage_rowSizeByte;
100 pixelOutRight += 2*singleImage_rowSizeByte;
101 pixelLeft += singleImage_rowSizeByte;
102 pixelRight += singleImage_rowSizeByte;
103 }
104 return true;
105}
106
107bool utils::vertConcat(const Image& inImgUp, const Image& inImgDown, Image& outImg)
108{
109 outImg.resize(inImgUp.width(), inImgUp.height()*2);
110
111 if (!checkImages(outImg, inImgUp, inImgDown)) {
112 return false;
113 }
114
115 // Copy the memory
116 size_t imgSize = inImgUp.getRawImageSize();
117 memcpy(outImg.getRawImage(), inImgUp.getRawImage(), imgSize);
118 memcpy(outImg.getRawImage() + imgSize, inImgDown.getRawImage(), imgSize);
119 return true;
120}
121
123 const std::pair<unsigned int, unsigned int>& vertex1,
124 const std::pair<unsigned int, unsigned int>& vertex2,
125 yarp::sig::Image& outImg)
126{
127 if (inImg.getPixelCode() != outImg.getPixelCode()) {
128 return false;
129 }
130
131 // Normalize vertices: upper-left (tlx,tly) and bottom-right (brx,bry) corners
132 auto tlx = std::min(vertex1.first, vertex2.first);
133 auto tly = std::min(vertex1.second, vertex2.second);
134 auto brx = std::max(vertex1.first, vertex2.first);
135 auto bry = std::max(vertex1.second, vertex2.second);
136
137 if (!inImg.isPixel(brx, bry)) {
138 return false;
139 }
140
141 outImg.resize(brx - tlx + 1, bry - tly + 1); // width, height
142
143 auto * pixelOut = outImg.getRawImage();
144
145 for (unsigned int row = 0; row < outImg.height(); row++) {
146 const auto * pixelIn = inImg.getPixelAddress(tlx, tly + row);
147 memcpy(pixelOut, pixelIn, outImg.getRowSize());
148 pixelOut += outImg.getRowSize();
149 }
150
151 return true;
152}
153
154bool utils::sum(Image& OutImg, const Image& InImg, bool enable_colorkey, int colorkey, bool enable_alpha, float alpha, size_t offset_x, size_t offset_y)
155{
156 if (OutImg.getPixelCode() != InImg.getPixelCode())
157 {
158 yError() << "utils::sum() Input and Output images must have the same pixelcode";
159 return false;
160 }
161
162 if (InImg.getPixelCode() != VOCAB_PIXEL_RGB)
163 {
164 yError() << "utils::sum() Unimplemented pixelcode";
165 return false;
166 }
167
168 yarp::sig::PixelRgb ColorkeyRGB;
169 ColorkeyRGB = *reinterpret_cast<yarp::sig::PixelRgb*>(&colorkey);
170
171 size_t yis = InImg.height();
172 size_t xis = InImg.width();
173 size_t yos = OutImg.height();
174 size_t xos = OutImg.width();
175
176 for (size_t y = 0; y < yis; ++y)
177 {
178 for (size_t x = 0; x < xis; ++x)
179 {
180 size_t xo = x + offset_x;
181 size_t yo = y + offset_y;
182 if (xo > xos) {
183 xo = xos;
184 }
185 if (yo > yos) {
186 yo = yos;
187 }
188
189 unsigned char* layer_pointer = InImg.getPixelAddress(x, y);
190 unsigned char* outimg_pointer = OutImg.getPixelAddress(xo, yo);
191
192 yarp::sig::PixelRgb* layer_pointer_rgb = reinterpret_cast<yarp::sig::PixelRgb*>(layer_pointer);
193 yarp::sig::PixelRgb* outimg_pointer_rgb = reinterpret_cast<yarp::sig::PixelRgb*>(outimg_pointer);
194
195 if (enable_colorkey && layer_pointer_rgb->r == ColorkeyRGB.r && layer_pointer_rgb->g == ColorkeyRGB.g && layer_pointer_rgb->b == ColorkeyRGB.b)
196 {
197 continue;
198 }
199 else if (enable_alpha)
200 {
201 outimg_pointer_rgb->r = layer_pointer_rgb->r * alpha + outimg_pointer_rgb->r * (1 - alpha);
202 outimg_pointer_rgb->g = layer_pointer_rgb->g * alpha + outimg_pointer_rgb->g * (1 - alpha);
203 outimg_pointer_rgb->b = layer_pointer_rgb->b * alpha + outimg_pointer_rgb->b * (1 - alpha);
204 }
205 else
206 {
207 outimg_pointer_rgb->r = layer_pointer_rgb->r;
208 outimg_pointer_rgb->g = layer_pointer_rgb->g;
209 outimg_pointer_rgb->b = layer_pointer_rgb->b;
210 }
211 }
212 }
213
214 return true;
215}
216
218 const yarp::sig::Image& InImg,
219 RotateOption rotateflag,
220 FlipOption flipflag)
221{
222#if !defined (YARP_HAS_OPENCV)
223 yError() << "libopencv not available";
224 return false;
225#else
226
227 // Resolve rotation flag locally from enum
228 const bool doRotate = (rotateflag != RotateOption::rotate_none);
229 ::cv::RotateFlags rot_flags = ::cv::ROTATE_90_CLOCKWISE; // default, overridden below
230 if (doRotate)
231 {
232 switch (rotateflag)
233 {
234 case RotateOption::rotate_cw: rot_flags = ::cv::ROTATE_90_CLOCKWISE; break;
235 case RotateOption::rotate_ccw: rot_flags = ::cv::ROTATE_90_COUNTERCLOCKWISE; break;
236 case RotateOption::rotate_180: rot_flags = ::cv::ROTATE_180; break;
237 default: break;
238 }
239 }
240
241 // Resolve flip code locally from enum
242 const bool doFlip = (flipflag != FlipOption::flip_none);
243 int flip_code = 0; // default, overridden below
244 if (doFlip)
245 {
246 switch (flipflag)
247 {
248 case FlipOption::flip_x: flip_code = 0; break;
249 case FlipOption::flip_y: flip_code = 1; break;
250 case FlipOption::flip_xy: flip_code = -1; break;
251 default: break;
252 }
253 }
254
255 // Beware in the following code, OutImg.copy() is required because
256 // cv::Mat is a temporary object and will be destroyed at the end of the statement.
257 // Also notice that OutImg = yarp::cv::fromCvMat(...) is not enough because it will not copy the data,
258 // will call the Move assignment operator, since yarp::cv::fromCvMat() is an rvalue.
259 if (InImg.getPixelCode() == VOCAB_PIXEL_RGB)
260 {
261 ::cv::Mat cvInImage = yarp::cv::toCvMat(InImg);
262 ::cv::Mat cvOutImage1;
263 ::cv::Mat cvOutImage2;
264
265 if (!doFlip && doRotate) {
266 // just rotation
267 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
268 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage1));
269 } else if (doFlip && !doRotate) {
270 // just flip
271 ::cv::flip(cvInImage, cvOutImage1, flip_code);
272 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage1));
273 } else if (!doFlip && !doRotate) {
274 // just copy
275 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvInImage));
276 } else {
277 // first a rotation, then a flip
278 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
279 ::cv::flip(cvOutImage1, cvOutImage2, flip_code);
280 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage2));
281 }
282 }
283 else if (InImg.getPixelCode() == VOCAB_PIXEL_MONO_FLOAT)
284 {
285 ::cv::Mat cvInImage = yarp::cv::toCvMat(InImg);
286 ::cv::Mat cvOutImage1;
287 ::cv::Mat cvOutImage2;
288
289 if (!doFlip && doRotate) {
290 // just rotation
291 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
292 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage1));
293 } else if (doFlip && !doRotate) {
294 // just flip
295 ::cv::flip(cvInImage, cvOutImage1, flip_code);
296 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage1));
297 } else if (!doFlip && !doRotate)
298 {
299 // just copy
300 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvInImage));
301 } else
302 {
303 // first a rotation, then a flip
304 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
305 ::cv::flip(cvOutImage1, cvOutImage2, flip_code);
306 OutImg.copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage2));
307 }
308 }
309 else
310 {
311 yError("Invalid Image type!");
312 return false;
313 }
314 return true;
315#endif
316}
static bool checkImages(const Image &bigImg, const Image &smallImg1, const Image &smallImg2)
@ VOCAB_PIXEL_MONO_FLOAT
Definition Image.h:53
@ VOCAB_PIXEL_RGB
Definition Image.h:44
#define yError(...)
Definition Log.h:361
Base class for storing images.
Definition Image.h:79
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition Image.h:197
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:462
bool copy(const Image &alt)
Copy operator.
Definition Image.cpp:611
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition Image.cpp:471
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:387
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
Definition Image.h:255
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:375
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition Image.h:245
::cv::Mat toCvMat(const yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
Definition Cv-inl.h:84
bool vertSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgL, yarp::sig::Image &outImgR)
Split vertically an image in two images of the same size.
bool cropRect(const yarp::sig::Image &inImg, const std::pair< unsigned int, unsigned int > &vertex1, const std::pair< unsigned int, unsigned int > &vertex2, yarp::sig::Image &outImg)
Crop a rectangle area out of an image given two opposite vertices.
bool horzSplit(const yarp::sig::Image &inImg, yarp::sig::Image &outImgUp, yarp::sig::Image &outImgDown)
Split horizontally an image in two images of the same size.
bool vertConcat(const yarp::sig::Image &inImgUp, const yarp::sig::Image &inImgDown, yarp::sig::Image &outImg)
Concatenate vertically two images of the same size in one with double height.
bool horzConcat(const yarp::sig::Image &inImgL, const yarp::sig::Image &inImgR, yarp::sig::Image &outImg)
Concatenate horizontally two images of the same size in one with double width.
bool sum(yarp::sig::Image &OutImg, const yarp::sig::Image &InImg, bool colorkey_enable, int colorkey, bool alpha_enable, float alpha, size_t off_x, size_t off_y)
applies an image on the top over another image.
bool rotate(yarp::sig::Image &OutImg, const yarp::sig::Image &InImg, RotateOption rotateflag=RotateOption::rotate_none, FlipOption flipflag=FlipOption::flip_none)
Rotate and/or flip an image.