7 #include <yarp/sig/Image.h>
8 #include <yarp/os/all.h>
13 using namespace yarp::os;
14 using namespace yarp::sig;
17 void merge(
const ImageOf<PixelRgb> &imgR,
const ImageOf<PixelRgb> &imgL, ImageOf<PixelRgb> &
out,
size_t start_lx,
size_t start_ly,
size_t start_rx,
size_t start_ry,
double alpha1,
double alpha2)
19 size_t max_w = (imgR.width() > imgL.width()) ? imgR.width() : imgL.width();
20 size_t max_h = (imgR.height() > imgL.height()) ? imgR.height() : imgL.height();
21 if (
out.width() != max_w ||
out.height() != max_h)
out.resize(max_w, max_h);
23 start_lx = (start_lx < max_w) ? start_lx : max_w;
24 start_ly = (start_ly < max_h) ? start_ly : max_h;
25 start_rx = (start_rx < max_w) ? start_rx : max_w;
26 start_ry = (start_ry < max_h) ? start_ry : max_h;
27 size_t end_lx = (start_lx + imgL.width() < max_w) ? (start_lx + imgL.width()) : max_w;
28 size_t end_ly = (start_ly + imgL.height() < max_h) ? (start_ly + imgL.height()) : max_h;
29 size_t end_rx = (start_rx + imgR.width() < max_w) ? (start_rx + imgR.width()) : max_w;
30 size_t end_ry = (start_ry + imgR.height() < max_h) ? (start_ry + imgR.height()) : max_h;
33 for (
size_t r_dst = 0; r_dst < max_h; r_dst++)
35 unsigned char *tmp_dst =
out.getRow(r_dst);
37 for (
size_t c_dst = 0; c_dst < max_w; c_dst++)
39 tmp_dst[0 + c_dst * 3] = 0;
40 tmp_dst[1 + c_dst * 3] = 0;
41 tmp_dst[2 + c_dst * 3] = 0;
46 for (
size_t r_dst = start_ly, r_src = 0; r_dst < end_ly; r_dst++, r_src++)
48 unsigned char *tmp_dst =
out.getRow(r_dst);
49 const unsigned char *tmp_src = imgL.getRow(r_src);
51 for (
size_t c_dst = start_lx, c_src = 0; c_dst < end_lx; c_dst++, c_src++)
53 tmp_dst[0 + c_dst * 3] = tmp_dst[0 + c_dst * 3] + (
unsigned char)(
double(tmp_src[0 + c_src * 3]) * alpha1);
54 tmp_dst[1 + c_dst * 3] = tmp_dst[1 + c_dst * 3] + (
unsigned char)(
double(tmp_src[1 + c_src * 3]) * alpha1);
55 tmp_dst[2 + c_dst * 3] = tmp_dst[2 + c_dst * 3] + (
unsigned char)(
double(tmp_src[2 + c_src * 3]) * alpha1);
60 for (
size_t r_dst = start_ry, r_src = 0; r_dst < end_ry; r_dst++, r_src++)
62 unsigned char *tmp_dst =
out.getRow(r_dst);
63 const unsigned char *tmp_src = imgR.getRow(r_src);
65 for (
size_t c_dst = start_rx, c_src = 0; c_dst < end_rx; c_dst++, c_src++)
67 tmp_dst[0 + c_dst * 3] = tmp_dst[0 + c_dst * 3] + (
unsigned char)(
double(tmp_src[0 + c_src * 3]) * alpha2);
68 tmp_dst[1 + c_dst * 3] = tmp_dst[1 + c_dst * 3] + (
unsigned char)(
double(tmp_src[1 + c_src * 3]) * alpha2);
69 tmp_dst[2 + c_dst * 3] = tmp_dst[2 + c_dst * 3] + (
unsigned char)(
double(tmp_src[2 + c_src * 3]) * alpha2);
79 rf.configure(
argc, argv);
81 BufferedPort<ImageOf<PixelRgb> > right;
82 BufferedPort<ImageOf<PixelRgb> > left;
83 BufferedPort<ImageOf<PixelRgb> >
out;
85 right.open(
"/imageBlending/right");
86 left.open(
"/imageBlending/left");
88 out.open(
"/imageBlending/out");
96 if (rf.check(
"rx")) start_rx = (size_t) rf.find(
"rx").asFloat64();
97 if (rf.check(
"ry")) start_ry = (
size_t) rf.find(
"ry").asFloat64();
98 if (rf.check(
"lx")) start_lx = (
size_t) rf.find(
"lx").asFloat64();
99 if (rf.check(
"ly")) start_ly = (
size_t) rf.find(
"ly").asFloat64();
100 if (rf.check(
"alpha1")) alpha1 = rf.find(
"alpha1").asFloat64();
101 if (rf.check(
"alpha2")) alpha2 = rf.find(
"alpha2").asFloat64();
102 yDebug(
"left offset:%lu,%lu right offset:%lu,%lu, alpha1:%f, alpha2:%f", start_lx, start_ly, start_rx, start_ry, alpha1, alpha2);
103 if (rf.check(
"help"))
105 yDebug() <<
"Available options:";
110 yDebug() <<
"alpha1";
111 yDebug() <<
"alpha2";
117 ImageOf< PixelRgb> *imgR=right.read(
true);
118 ImageOf< PixelRgb> *imgL=left.read(
true);
120 ImageOf< PixelRgb> &outImg=
out.prepare();
122 if (imgR!=0 && imgL!=0)
124 merge(*imgR, *imgL, outImg, start_lx, start_ly, start_rx, start_ry,alpha1,alpha2);
129 printFrame(outImg.height(), outImg.width(), c);
137 printf(
"Frame #%d %dx%d \n", c,
h, w);
int main(int argc, char *argv[])
void merge(const ImageOf< PixelRgb > &imgR, const ImageOf< PixelRgb > &imgL, ImageOf< PixelRgb > &out, size_t start_lx, size_t start_ly, size_t start_rx, size_t start_ry, double alpha1, double alpha2)
void printFrame(int h, int w, int c)
Copyright (C) 2008 RobotCub Consortium.