19 #include <yarp/cv/Cv.h>
20 #include "lumaChroma.h"
25 const int KERNSIZEMAX = 9;
28 using namespace yarp::os;
29 using namespace yarp::sig;
30 using namespace yarp::cv;
32 bool lumaChroma::configure(yarp::os::ResourceFinder &rf)
36 moduleName = rf.check(
"name",
38 "module name (string)").asString();
40 setName(moduleName.c_str());
42 imageType = rf.check(
"image",
44 "image type (string)").asString();
46 whichPort = rf.check(
"out",
48 "default port (string)").asString();
55 handlerPortName =
"/";
56 handlerPortName += getName();
58 if (!handlerPort.open(handlerPortName.c_str()))
60 cout << getName() <<
": Unable to open port " << handlerPortName << endl;
67 procThread =
new PROCThread(moduleName, imageType, whichPort);
75 bool lumaChroma::interruptModule()
77 handlerPort.interrupt();
81 bool lumaChroma::close()
85 cout <<
"starting the shutdown procedure " << endl;
86 procThread->interrupt();
88 fprintf(stdout,
"deleting thread \n");
90 fprintf(stdout,
"done deleting thread \n");
94 bool lumaChroma::respond(
const Bottle& command, Bottle& reply)
98 if (command.get(0).asString()==
"quit")
100 reply.addString(
"quitting");
103 else if (command.get(0).asString()==
"help")
105 cout <<
"Options:" << endl << endl;
106 cout <<
"\t--name name: module name (default: lumaChroma)" << endl;
107 cout <<
"\t--image type: image type to process (default: yuv)" << endl;
108 cout <<
"\t--out default port: duplicates the out port with a default name" << endl;
109 reply.addString(
"ok");
113 cout <<
"command not known - type help for more info" << endl;
120 bool lumaChroma::updateModule()
125 double lumaChroma::getPeriod()
130 PROCThread::~PROCThread()
135 PROCThread::PROCThread(
const string &moduleName,
const string &imgType,
const string &whichPort )
140 this->whichPort = whichPort;
143 this->moduleName = moduleName;
146 if (imgType ==
"yuv" || imgType ==
"YUV")
148 cout <<
"will run module using the YUV image colour space" << endl;
149 if (whichPort.size() == 0)
150 this->whichPort =
"Y";
152 else if (imgType ==
"hsv" || imgType ==
"HSV")
155 cout <<
"will run module using the HSV image colour space" << endl;
156 if (whichPort.size() == 0)
157 this->whichPort =
"S";
161 cout <<
"probably an error in the colour space will use default: YUV" << endl;
163 cout <<
"initialising Variables" << endl;
168 inputExtImage = NULL;
176 bool PROCThread::open()
191 inputPortName =
"/" + moduleName +
"/image:i";
192 BufferedPort<ImageOf<PixelRgb> >::open( inputPortName.c_str() );
194 outputPortName =
"/" + moduleName +
"/propImg:o";
195 imageOutPort.open( outputPortName.c_str() );
197 outputPortName1 =
"/" + moduleName +
"/" + temp[0] +
"/image:o";
198 imageOutPort1.open( outputPortName1.c_str() );
200 outputPortName2 =
"/" + moduleName +
"/" + temp[1] +
"/image:o";
201 imageOutPort2.open( outputPortName2.c_str() );
205 outputPortName3 =
"/" + moduleName +
"/" + temp[2] +
"/image:o";
206 imageOutPort3.open( outputPortName3.c_str() );
209 outputPortNameDefault =
"/" + moduleName +
"/image:o";
210 defaultPortOut.open( outputPortNameDefault.c_str() );
217 void PROCThread::onRead(ImageOf<yarp::sig::PixelRgb> &img)
219 lock_guard<mutex> lg(mtx);
225 if( !allocated || img.width() != img_out_Y->width() || img.height() != img_out_Y->height() )
230 extender( img, KERNSIZEMAX );
231 cv::Mat inputMat=toCvMat(*inputExtImage);
233 cv::cvtColor( inputMat, orig, CV_RGB2YCrCb);
235 cv::cvtColor( inputMat, orig, CV_RGB2HSV);
237 vector<cv::Mat> planes;
238 cv::split(orig, planes);
240 centerSurr->proc_im_8u( planes[0] );
242 cv::Mat y_img = centerSurr->get_centsur_norm8u();
243 y_img.copyTo(toCvMat(*img_Y));
244 csTot32f.setTo(cv::Scalar(0));
247 centerSurr->proc_im_8u( planes[1] );
249 cv::add(centerSurr->get_centsur_32f(), csTot32f, csTot32f);
252 cv::Mat s_img = centerSurr->get_centsur_norm8u();
253 s_img.copyTo(toCvMat(*img_UV));
256 centerSurr->proc_im_8u( planes[2] );
258 cv::add(centerSurr->get_centsur_32f(), csTot32f, csTot32f);
261 cv::Mat v_img = centerSurr->get_centsur_norm8u();
262 v_img.copyTo(toCvMat(*img_V));
267 double valueMin = 0.0f;
268 double valueMax = 0.0f;
269 cv::minMaxLoc(csTot32f, &valueMin, &valueMax);
270 if ( valueMax == valueMin )
272 valueMax = 255.0f; valueMin = 0.0f;
274 cv::convertScaleAbs( csTot32f, uvimg, 255/(valueMax - valueMin), -255*valueMin/(valueMax-valueMin) );
275 uvimg.copyTo(toCvMat(*img_UV));
278 unsigned char* imgY = img_Y->getPixelAddress( KERNSIZEMAX, KERNSIZEMAX );
279 unsigned char* imgUV = img_UV->getPixelAddress( KERNSIZEMAX, KERNSIZEMAX );
281 unsigned char* imgVo;
285 imgV = img_V->getPixelAddress( KERNSIZEMAX, KERNSIZEMAX );
286 imgVo = img_out_V->getRawImage();
288 unsigned char* imgYo = img_out_Y->getRawImage();
289 unsigned char* imgUVo = img_out_UV->getRawImage();
290 int rowsize= img_out_Y->getRowSize();
291 int rowsize2= img_Y->getRowSize();
292 for(
int row=0; row<origsize.height; row++)
294 for(
int col=0; col<origsize.width; col++)
306 imgYo+=rowsize - origsize.width;
307 imgUVo+=rowsize - origsize.width;
308 imgY+=rowsize2 - origsize.width;
309 imgUV+=rowsize2 - origsize.width;
312 imgVo+=rowsize - origsize.width;
313 imgV+=rowsize2 - origsize.width;
318 if ( imageOutPort1.getOutputCount()>0 )
320 imageOutPort1.prepare() = *img_out_Y;
321 imageOutPort1.write();
325 if ( imageOutPort2.getOutputCount()>0 )
327 imageOutPort2.prepare() = *img_out_UV;
328 imageOutPort2.write();
331 if ( !isYUV && imageOutPort3.getOutputCount()>0 )
333 imageOutPort3.prepare() = *img_out_V;
334 imageOutPort3.write();
337 if ( defaultPortOut.getOutputCount()>0 )
339 if (!isYUV && whichPort==
"v" || !isYUV && whichPort ==
"V" )
340 defaultPortOut.prepare() = *img_out_V;
341 else if (whichPort==
"y" || whichPort ==
"Y" || whichPort==
"h" || whichPort ==
"H")
342 defaultPortOut.prepare() = *img_out_Y;
343 else if (whichPort==
"u" || whichPort ==
"U" || whichPort==
"v" || whichPort ==
"V" || whichPort==
"uv" || whichPort ==
"UV" || whichPort==
"s" || whichPort ==
"S" )
344 defaultPortOut.prepare() = *img_out_UV;
346 fprintf(stdout,
"something went wrong\n");
348 imageOutPort.prepare() = img;
349 defaultPortOut.write();
350 imageOutPort.write();
354 void PROCThread::allocate( ImageOf<PixelRgb> &img )
356 origsize.width = img.width();
357 origsize.height = img.height();
359 srcsize.width = origsize.width + 2 * KERNSIZEMAX;
360 srcsize.height = origsize.height + KERNSIZEMAX;
362 cout <<
"Received input image dimensions: " << origsize.width <<
" " << origsize.height << endl;
363 cout <<
"Will extend these to: " << srcsize.width <<
" " << srcsize.height << endl;
365 orig = cv::Mat(srcsize.width, srcsize.height, CV_8UC3);
366 csTot32f = cv::Mat( srcsize.height, srcsize.width, CV_32FC1 );
367 uvimg = cv::Mat( srcsize.height, srcsize.width, CV_32FC1 );
370 centerSurr =
new CentSur( srcsize , ncsscale );
372 inputExtImage =
new ImageOf<PixelRgb>;
373 inputExtImage->resize( srcsize.width, srcsize.height );
375 img_Y =
new ImageOf<PixelMono>;
376 img_Y->resize( srcsize.width, srcsize.height );
378 img_out_Y =
new ImageOf<PixelMono>;
379 img_out_Y->resize( origsize.width, origsize.height );
381 img_UV =
new ImageOf<PixelMono>;
382 img_UV->resize( srcsize.width, srcsize.height );
384 img_out_UV =
new ImageOf<PixelMono>;
385 img_out_UV->resize( origsize.width, origsize.height );
387 img_V =
new ImageOf<PixelMono>;
388 img_V->resize( srcsize.width, srcsize.height );
390 img_out_V =
new ImageOf<PixelMono>;
391 img_out_V->resize( origsize.width, origsize.height );
394 cout <<
"done allocating" << endl;
397 void PROCThread::deallocate( )
405 delete inputExtImage;
409 inputExtImage = NULL;
423 void PROCThread::close()
425 cout <<
"now closing ports..." << endl;
426 lock_guard<mutex> lg(mtx);
427 imageOutPort.close();
428 imageOutPort1.close();
429 imageOutPort2.close();
431 imageOutPort3.close();
432 defaultPortOut.close();
435 cout <<
"deallocated all attempting to close read port..." << endl;
436 BufferedPort<ImageOf<PixelRgb> >::close();
437 cout <<
"finished closing the read port..." << endl;
440 void PROCThread::interrupt()
442 lock_guard<mutex> lg(mtx);
444 cout <<
"cleaning up..." << endl;
445 cout <<
"attempting to interrupt ports" << endl;
447 imageOutPort.interrupt();
448 imageOutPort1.interrupt();
449 imageOutPort2.interrupt();
452 imageOutPort3.interrupt();
454 defaultPortOut.interrupt();
456 BufferedPort<ImageOf<PixelRgb> >::interrupt();
457 cout <<
"finished interrupt ports" << endl;
460 ImageOf<PixelRgb>* PROCThread::extender(ImageOf<PixelRgb>& inputOrigImage,
int maxSize)
463 inputExtImage->copy( inputOrigImage, srcsize.width, srcsize.height );
465 const int sizeBlock = origsize.width / 2;
466 for(
int i = 0; i < maxSize; i++)
468 memcpy( inputExtImage->getPixelAddress( sizeBlock + maxSize, maxSize-1-i ),
469 inputExtImage->getPixelAddress( maxSize, maxSize+i ),
470 sizeBlock*
sizeof(PixelRgb));
471 memcpy( inputExtImage->getPixelAddress( maxSize, maxSize-1-i ),
472 inputExtImage->getPixelAddress( sizeBlock + maxSize, maxSize+i ),
473 sizeBlock*
sizeof(PixelRgb));
476 const int px = maxSize *
sizeof(PixelRgb);
478 for (
int row = 0; row < srcsize.height; row++)
480 memcpy ( inputExtImage->getPixelAddress( srcsize.width-maxSize, row ),
481 inputExtImage->getPixelAddress( maxSize,row ), px);
482 memcpy ( inputExtImage->getPixelAddress( 0, row ),
483 inputExtImage->getPixelAddress( srcsize.width- maxSize-maxSize, row ), px);
485 return inputExtImage;