iCub-main
CamCalibModule.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2007 Jonas Ruesch
5  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
6  *
7  */
8 
9 #include <iCub/CamCalibModule.h>
10 
11 using namespace std;
12 using namespace yarp::os;
13 using namespace yarp::sig;
14 
16 {
17  portImgOut=NULL;
18  calibTool=NULL;
19 
20  verbose=false;
21  t0=Time::now();
22 }
23 
24 void CamCalibPort::setPointers(yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > *_portImgOut, ICalibTool *_calibTool)
25 {
26  portImgOut=_portImgOut;
27  calibTool=_calibTool;
28 }
29 
30 void CamCalibPort::setSaturation(double satVal)
31 {
32  currSat = satVal;
33 }
34 
35 void CamCalibPort::onRead(ImageOf<PixelRgb> &yrpImgIn)
36 {
37  double t=Time::now();
38  // execute calibration
39  if (portImgOut!=NULL)
40  {
41  yarp::sig::ImageOf<PixelRgb> &yrpImgOut=portImgOut->prepare();
42 
43  if (verbose)
44  yDebug("received input image after %g [s] ... ",t-t0);
45 
46  double t1=Time::now();
47 
48  if (calibTool!=NULL)
49  {
50  calibTool->apply(yrpImgIn,yrpImgOut);
51 
52  for (int r =0; r <yrpImgOut.height(); r++)
53  {
54  for (int c=0; c<yrpImgOut.width(); c++)
55  {
56  unsigned char *pixel = yrpImgOut.getPixelAddress(c,r);
57  double mean = (1.0/3.0)*(pixel[0]+pixel[1]+pixel[2]);
58 
59  for(int i=0; i<3; i++)
60  {
61  double s=pixel[i]-mean;
62  double sn=currSat*s;
63  sn+=mean;
64 
65  if(sn<0.0)
66  sn=0.0;
67  else if(sn>255.0)
68  sn=255.0;
69 
70  pixel[i]=(unsigned char)sn;
71  }
72  }
73  }
74 
75  if (verbose)
76  yDebug("calibrated in %g [s]\n",Time::now()-t1);
77  }
78  else
79  {
80  yrpImgOut=yrpImgIn;
81 
82  if (verbose)
83  yDebug("just copied in %g [s]\n",Time::now()-t1);
84  }
85 
86  //timestamp propagation
87  yarp::os::Stamp stamp;
88  BufferedPort<ImageOf<PixelRgb> >::getEnvelope(stamp);
89  portImgOut->setEnvelope(stamp);
90 
91  portImgOut->writeStrict();
92  }
93 
94  t0=t;
95 }
96 
98 
99  _calibTool = NULL;
100 }
101 
103 
104 }
105 
106 bool CamCalibModule::configure(yarp::os::ResourceFinder &rf){
107 
108  string str = rf.check("name", Value("/camCalib"), "module name (string)").asString();
109  setName(str.c_str()); // modulePortName
110 
111  // pass configuration over to bottle
112  Bottle botConfig(rf.toString());
113  // Load from configuration group ([<group_name>]), if group option present
114  Value *valGroup; // check assigns pointer to reference
115  if(botConfig.check("group", valGroup, "Configuration group to load module options from (string)."))
116  {
117  string strGroup = valGroup->asString();
118  // is group a valid bottle?
119  if (botConfig.check(strGroup)){
120  Bottle &group=botConfig.findGroup(strGroup,"Loading configuration from group " + strGroup);
121  botConfig.fromString(group.toString());
122  }
123  else{
124  yError() << "Group " << strGroup << " not found.";
125  return false;
126  }
127  }
128  else
129  {
130  yError ("There seem to be an error loading parameters (group section missing), stopping module");
131  return false;
132  }
133 
134  string calibToolName = botConfig.check("projection",
135  Value("pinhole"),
136  "Projection/mapping applied to calibrated image [pinhole|spherical] (string).").asString();
137 
138  _calibTool = CalibToolFactories::getPool().get(calibToolName.c_str());
139  if (_calibTool!=NULL) {
140  bool ok = _calibTool->open(botConfig);
141  if (!ok) {
142  delete _calibTool;
143  _calibTool = NULL;
144  return false;
145  }
146  }
147 
148  if (yarp::os::Network::exists(getName("/in")))
149  {
150  yWarning() << "port " << getName("/in") << " already in use";
151  }
152  if (yarp::os::Network::exists(getName("/out")))
153  {
154  yWarning() << "port " << getName("/out") << " already in use";
155  }
156  if (yarp::os::Network::exists(getName("/conf")))
157  {
158  yWarning() << "port " << getName("/conf") << " already in use";
159  }
160  _prtImgIn.setSaturation(rf.check("saturation",Value(1.0)).asFloat64());
161  _prtImgIn.open(getName("/in"));
162  _prtImgIn.setPointers(&_prtImgOut,_calibTool);
163  _prtImgIn.setVerbose(rf.check("verbose"));
164  _prtImgIn.useCallback();
165  _prtImgOut.open(getName("/out"));
166  _configPort.open(getName("/conf"));
167 
168  attach(_configPort);
169  fflush(stdout);
170 
171  return true;
172 }
173 
175  _prtImgIn.close();
176  _prtImgOut.close();
177  _configPort.close();
178  if (_calibTool != NULL){
179  _calibTool->close();
180  delete _calibTool;
181  _calibTool = NULL;
182  }
183  return true;
184 }
185 
187  _prtImgIn.interrupt();
188  _prtImgOut.interrupt();
189  _configPort.interrupt();
190  return true;
191 }
192 
194  return true;
195 }
196 
198  return 1.0;
199 }
200 
201 bool CamCalibModule::respond(const Bottle& command, Bottle& reply)
202 {
203  reply.clear();
204 
205  if (command.get(0).asString()=="quit")
206  {
207  reply.addString("quitting");
208  return false;
209  }
210  else if (command.get(0).asString()=="sat" || command.get(0).asString()=="saturation")
211  {
212  double satVal = command.get(1).asFloat64();
213  _prtImgIn.setSaturation(satVal);
214 
215  reply.addString("ok");
216  }
217  else
218  {
219  yError() << "command not known - type help for more info";
220  }
221  return true;
222 }
223 
224 
static CalibToolFactories & getPool()
ICalibTool * get(const char *name)
virtual bool interruptModule()
virtual bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
virtual bool configure(yarp::os::ResourceFinder &rf)
Passes config on to CalibTool.
virtual bool updateModule()
virtual double getPeriod()
virtual bool close()
void setSaturation(double satVal)
void setPointers(yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > *_portImgOut, ICalibTool *_calibTool)
Interface to calibrate and project input image based on camera's internal parameters and projection m...
Definition: ICalibTool.h:19