17 #include <objectReconstr.h> 19 #include <opencv2/opencv.hpp> 26 ObjectReconstr::ObjectReconstr()
28 currentState=STATE_WAIT;
30 visualizationOn=
false;
36 bool ObjectReconstr::configure(ResourceFinder &rf)
38 string robot=rf.check(
"robot",Value(
"icub")).asString().c_str();
39 string name=rf.check(
"name",Value(
"object-reconstruction")).asString().c_str();
40 setName(name.c_str());
41 outputDir=rf.getHomeContextPath().c_str();
42 computeBB=rf.check(
"computeBB",Value(
false)).asBool();
47 fileName =
"3Dobject";
51 string imL=slash + getName().c_str() +
"/left:i";
52 string imR=slash + getName().c_str() +
"/right:i";
53 imagePortInLeft.open(imL.c_str());
54 imagePortInRight.open(imR.c_str());
59 ok&=Network::connect((slash+robot+
"/camcalib/left/out").c_str(),imL.c_str());
60 ok&=Network::connect((slash+robot+
"/camcalib/right/out").c_str(),imR.c_str());
64 ok&=Network::connect((slash+robot+
"/cam/left").c_str(),imL.c_str());
65 ok&=Network::connect((slash+robot+
"/cam/right").c_str(),imR.c_str());
70 printf(
"Cameras not available, closing\n");
71 imagePortInLeft.close();
72 imagePortInRight.close();
75 segmentationPort.open((slash+getName().c_str()+
"/segmentation").c_str());
77 pointCloudPort.open((slash + getName().c_str() +
"/mesh:o").c_str());
78 rpc.open((slash + getName().c_str() +
"/rpc").c_str());
81 Property recRoutOptions;
82 recRoutOptions.put(
"ConfigDisparity",rf.check(
"ConfigDisparity",Value(
"icubEyes.ini")).asString().c_str());
83 recRoutOptions.put(
"CameraContext",rf.check(
"CameraContext",Value(
"cameraCalibration")).asString().c_str());
84 recRoutOptions.put(
"name",getName().c_str());
86 if (!recRoutine.open(recRoutOptions))
88 fprintf(stdout,
"Problem with thread, the module will be closed\n");
94 visThrd =
new VisThread(50,
"Cloud");
95 if (!visThrd->start())
99 cout <<
"\nERROR!!! visThread wasn't instantiated!!\n";
102 cout <<
"PCL visualizer Thread istantiated...\n";
108 bool ObjectReconstr::close()
110 imagePortInLeft.close();
111 imagePortInRight.close();
114 pointCloudPort.close();
115 segmentationPort.close();
129 Bottle ObjectReconstr::getPixelList()
131 Bottle toSend, reply;
132 toSend.addString(
"get_component_around");
133 toSend.addInt((
int)middlex);
134 toSend.addInt((
int)middley);
136 segmentationPort.write(toSend,reply);
141 void ObjectReconstr::savePointsPly(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud,
146 s<<outputDir +
"/" + name.c_str() <<number;
147 string filename=s.str();
148 string filenameNumb=filename+
".ply";
150 plyfile.open(filenameNumb.c_str());
152 plyfile <<
"format ascii 1.0\n";
153 plyfile <<
"element vertex " << cloud->width <<
"\n";
154 plyfile <<
"property float x\n";
155 plyfile <<
"property float y\n";
156 plyfile <<
"property float z\n";
157 plyfile <<
"property uchar diffuse_red\n";
158 plyfile <<
"property uchar diffuse_green\n";
159 plyfile <<
"property uchar diffuse_blue\n";
160 plyfile <<
"end_header\n";
162 for (
unsigned int i=0; i<cloud->width; i++)
163 plyfile << cloud->at(i).x <<
" " << cloud->at(i).y <<
" " << cloud->at(i).z <<
" " << (int)cloud->at(i).r <<
" " << (int)cloud->at(i).g <<
" " << (int)cloud->at(i).b <<
"\n";
168 fprintf(stdout,
"Writing finished\n");
172 bool ObjectReconstr::updateCloud()
174 ImageOf<PixelRgb> *tmpL = imagePortInLeft.read(
true);
175 ImageOf<PixelRgb> *tmpR = imagePortInRight.read(
true);
179 if(tmpL!=NULL && tmpR!=NULL)
181 imgL= (IplImage*) tmpL->getIplImage();
182 imgR= (IplImage*) tmpR->getIplImage();
186 fprintf(stdout,
"Problem with image ports occurred\n");
190 if (currentState==STATE_RECONSTRUCT)
192 Bottle pixelList=getPixelList();
193 return recRoutine.reconstruct(imgL,imgR,pixelList);
196 return recRoutine.updateDisparity(imgL,imgR);
200 bool ObjectReconstr::updateModule()
210 case STATE_RECONSTRUCT:
212 recRoutine.resetClouds();
217 printf(
"Cloud reconstructed\n");
219 pcl::PointCloud<pcl::PointXYZRGB>::Ptr tmp=recRoutine.getPointCloudComplete();
222 printf(
"Empty cloud\n");
225 currentState=STATE_WAIT;
226 SurfaceMeshWithBoundingBox &pointCloudOnPort=pointCloudPort.prepare();
227 pointCloudOnPort.mesh.points.clear();
228 pointCloudOnPort.mesh.rgbColour.clear();
229 pointCloudPort.write();
233 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (
new pcl::PointCloud<pcl::PointXYZRGB>);
236 SurfaceMeshWithBoundingBox &pointCloudOnPort=pointCloudPort.prepare();
237 pointCloudOnPort.mesh.points.clear();
238 pointCloudOnPort.mesh.rgbColour.clear();
239 for (
unsigned int i=0; i<cloud->width; i++)
241 pointCloudOnPort.mesh.points.push_back(PointXYZ(cloud->at(i).x,cloud->at(i).y, cloud->at(i).z));
242 pointCloudOnPort.mesh.rgbColour.push_back(RGBA(cloud->at(i).rgba));
246 cout <<
" computing BB " << endl;
247 boundingBox=MinimumBoundingBox::getMinimumBoundingBox(cloud);
248 pointCloudOnPort.boundingBox=boundingBox.getBoundingBox();
250 pointCloudPort.write();
253 savePointsPly(tmp, fileName);
256 currentState=STATE_VISUALIZE;
258 currentState=STATE_WAIT;
266 case STATE_VISUALIZE:
268 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud=recRoutine.getPointCloud();
282 visThrd->updateCloud(cloud);
285 cout <<
"Plotting BB " << endl;
286 visThrd->addBoundingBox(
true);
289 currentState=STATE_WAIT;
297 void ObjectReconstr::filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in_filtered,
bool second)
299 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_tmp (
new pcl::PointCloud<pcl::PointXYZRGB>);
300 pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor;
301 sor.setInputCloud (cloud_in);
302 sor.setMeanK (cloud_in->size()/2);
303 sor.setStddevMulThresh (1.0);
304 sor.filter (*cloud_in_filtered);
308 bool ObjectReconstr::interruptModule()
313 imagePortInLeft.interrupt();
314 imagePortInRight.interrupt();
316 pointCloudPort.interrupt();
317 segmentationPort.interrupt();
323 double ObjectReconstr::getPeriod()
329 bool ObjectReconstr::respond(
const Bottle& command, Bottle& reply)
331 if (command.get(0).asString()==
"help")
334 reply.addString(
"To obtain a reconstruction, the module needs a segmentation algorithm that returns the set of pixels belonging to the object");
335 reply.addString(
"It typically uses the graphBasedSegmentation module");
336 reply.addString(
"Provide a point of the object first, in the format x y, for instance clicking on the segmentation image.");
337 reply.addString(
"Then, when you send the command 3Drec, you will be provided with the reconstructed object along with the minimum enclosing bounding box on the output port of the module -- typically /object-reconstruction/mesh:o.");
338 reply.addString(
"If you also want to visualize the result, the command is 3Drec visualize.");
339 reply.addString(
"If you want to reconstruct a single pixel, the command is get point (x y).");
343 if (command.get(0).asString()==
"set")
345 if (command.get(1).asString()==
"write")
347 if (command.size()>2)
349 if (command.get(2).asString()==
"on")
354 else if (command.get(2).asString()==
"off")
360 reply.addVocab(NACK);
366 reply.addVocab(NACK);
370 if (command.get(0).asString()==
"3Drec")
372 if (middlex==-1 || middley==-1)
374 reply.addVocab(NACK);
375 reply.addString(
"I need a pixel of the object");
379 currentState=STATE_RECONSTRUCT;
381 visualizationOn=
false;
383 if (command.size()==2)
384 if (command.get(1).asString()==
"visualize")
385 visualizationOn=
true;
391 if (command.get(0).asString()==
"name")
393 if (command.size()>=2)
395 fileName=command.get(1).asString().c_str();
400 reply.addVocab(NACK);
401 reply.addString(
"No name was provided");
407 if (command.get(0).asString()==
"get")
409 if (command.get(1).asString()==
"point" && command.size()==3)
411 if (currentState!=STATE_RECONSTRUCT)
416 ImageOf<PixelRgb> *tmpL = imagePortInLeft.read(
true);
417 ImageOf<PixelRgb> *tmpR = imagePortInRight.read(
true);
419 if(tmpL!=NULL && tmpR!=NULL)
421 imgL= (IplImage*) tmpL->getIplImage();
422 imgR= (IplImage*) tmpR->getIplImage();
426 reply.addVocab(NACK);
430 yarp::sig::Vector point2D(2);
431 Bottle *pixel=command.get(2).asList();
432 point2D[0]=pixel->get(0).asDouble();
433 point2D[1]=pixel->get(1).asDouble();
435 yarp::sig::Vector point3D(3);
437 bool done=recRoutine.triangulateSinglePoint(imgL,imgR,point2D,point3D);
441 Bottle &result=reply.addList();
442 result.addDouble(point3D[0]);
443 result.addDouble(point3D[1]);
444 result.addDouble(point3D[2]);
447 reply.addVocab(NACK);
451 reply.addVocab(NACK);
452 reply.addString(
"Still processing");
458 reply.addVocab(NACK);
463 if (command.size()==2)
465 if (command.get(0).asInt()!=0 && command.get(1).asInt()!=0)
467 middlex=(double)command.get(0).asInt();
468 middley=(double)command.get(1).asInt();
474 reply.addVocab(NACK);
479 reply.addVocab(NACK);