12#if defined (YARP_HAS_OPENCV)
14#include <opencv2/highgui/highgui.hpp>
15#include <opencv2/imgproc/imgproc.hpp>
16#include <opencv2/videoio/videoio.hpp>
41 size_t inHeight = inImg.
height();
42 size_t singleImage_rowSizeByte = outImgL.
getRowSize();
47 for(
size_t h=0; h<inHeight; h++)
50 memcpy(pixelLeft + h*singleImage_rowSizeByte, pixelInput, singleImage_rowSizeByte);
51 memcpy(pixelRight + h*singleImage_rowSizeByte, pixelInput+=singleImage_rowSizeByte, singleImage_rowSizeByte);
54 pixelInput+= singleImage_rowSizeByte;
84 size_t singleImage_rowSizeByte = inImgL.
getRowSize();
87 unsigned char * pixelOutLeft = outImg.
getRawImage();
88 unsigned char * pixelOutRight = outImg.
getRawImage() + singleImage_rowSizeByte;
90 size_t height = inImgL.
height();
92 for(
size_t h=0; h<height; h++)
95 memcpy(pixelOutLeft, pixelLeft, singleImage_rowSizeByte);
96 memcpy(pixelOutRight, pixelRight, singleImage_rowSizeByte);
99 pixelOutLeft += 2*singleImage_rowSizeByte;
100 pixelOutRight += 2*singleImage_rowSizeByte;
101 pixelLeft += singleImage_rowSizeByte;
102 pixelRight += singleImage_rowSizeByte;
123 const std::pair<unsigned int, unsigned int>& vertex1,
124 const std::pair<unsigned int, unsigned int>& vertex2,
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);
137 if (!inImg.
isPixel(brx, bry)) {
141 outImg.
resize(brx - tlx + 1, bry - tly + 1);
145 for (
unsigned int row = 0; row < outImg.
height(); row++) {
147 memcpy(pixelOut, pixelIn, outImg.
getRowSize());
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)
158 yError() <<
"utils::sum() Input and Output images must have the same pixelcode";
164 yError() <<
"utils::sum() Unimplemented pixelcode";
168 yarp::sig::PixelRgb ColorkeyRGB;
169 ColorkeyRGB = *
reinterpret_cast<yarp::sig::PixelRgb*
>(&colorkey);
171 size_t yis = InImg.
height();
172 size_t xis = InImg.
width();
173 size_t yos = OutImg.
height();
174 size_t xos = OutImg.
width();
176 for (
size_t y = 0; y < yis; ++y)
178 for (
size_t x = 0; x < xis; ++x)
180 size_t xo = x + offset_x;
181 size_t yo = y + offset_y;
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);
195 if (enable_colorkey && layer_pointer_rgb->r == ColorkeyRGB.r && layer_pointer_rgb->g == ColorkeyRGB.g && layer_pointer_rgb->b == ColorkeyRGB.b)
199 else if (enable_alpha)
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);
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;
222#if !defined (YARP_HAS_OPENCV)
223 yError() <<
"libopencv not available";
228 const bool doRotate = (rotateflag != RotateOption::rotate_none);
229 ::cv::RotateFlags rot_flags = ::cv::ROTATE_90_CLOCKWISE;
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;
242 const bool doFlip = (flipflag != FlipOption::flip_none);
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;
262 ::cv::Mat cvOutImage1;
263 ::cv::Mat cvOutImage2;
265 if (!doFlip && doRotate) {
267 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
268 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage1));
269 }
else if (doFlip && !doRotate) {
271 ::cv::flip(cvInImage, cvOutImage1, flip_code);
272 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage1));
273 }
else if (!doFlip && !doRotate) {
275 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvInImage));
278 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
279 ::cv::flip(cvOutImage1, cvOutImage2, flip_code);
280 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvOutImage2));
286 ::cv::Mat cvOutImage1;
287 ::cv::Mat cvOutImage2;
289 if (!doFlip && doRotate) {
291 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
292 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage1));
293 }
else if (doFlip && !doRotate) {
295 ::cv::flip(cvInImage, cvOutImage1, flip_code);
296 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage1));
297 }
else if (!doFlip && !doRotate)
300 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvInImage));
304 ::cv::rotate(cvInImage, cvOutImage1, rot_flags);
305 ::cv::flip(cvOutImage1, cvOutImage2, flip_code);
306 OutImg.
copy(yarp::cv::fromCvMat<yarp::sig::PixelFloat>(cvOutImage2));
311 yError(
"Invalid Image type!");
static bool checkImages(const Image &bigImg, const Image &smallImg1, const Image &smallImg2)
Base class for storing images.
size_t width() const
Gets width of image in pixels.
size_t getRowSize() const
Size of the underlying image buffer rows.
unsigned char * getRawImage() const
Access to the internal image buffer.
bool copy(const Image &alt)
Copy operator.
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
bool isPixel(size_t x, size_t y) const
Check whether a coordinate lies within the image.
size_t height() const
Gets height of image in pixels.
virtual int getPixelCode() const
Gets pixel type identifier.
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
::cv::Mat toCvMat(const yarp::sig::Image &yarpImage)
Convert a yarp::sig::FlexImage to a cv::Mat object.
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.