iCub-main
findLocation.cpp
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
8 #include <stdio.h>
9 #include <yarp/os/Network.h>
10 #include <yarp/os/BufferedPort.h>
11 #include <yarp/sig/Image.h>
12 #include <yarp/os/Time.h>
13 #include <yarp/os/Property.h>
14 
15 #include <string>
16 
17 using namespace yarp::sig;
18 using namespace yarp::os;
19 
20 int main(int argc, char *argv[])
21 {
22  Network yarp;
23 
24  BufferedPort<ImageOf<PixelRgb> > imagePort; // make a port for reading images
25  BufferedPort<ImageOf<PixelRgb> > outPort;
26 
27  imagePort.open("/imageProc/image/in"); // give the port a name
28  outPort.open("/imageProc/image/out");
29 
30  while (1) { // repeat forever
31  ImageOf<PixelRgb> *image = imagePort.read(); // read an image
32  ImageOf<PixelRgb> &outImage = outPort.prepare(); //get an output image
33 
34  outImage=*image;
35 
36  if (image!=NULL) { // check we actually got something
37  //printf("We got an image of size %dx%d\n", image->width(), image->height());
38  double xMean = 0;
39  double yMean = 0;
40  int ct = 0;
41  for (int x=0; x<image->width(); x++) {
42  for (int y=0; y<image->height(); y++) {
43  PixelRgb& pixel = image->pixel(x,y);
44  // very simple test for blueishness
45  // make sure blue level exceeds red and green by a factor of 2
46  if (pixel.b>pixel.r*1.2+10 && pixel.b>pixel.g*1.2+10) {
47  // there's a blueish pixel at (x,y)!
48  // let's find the average location of these pixels
49  xMean += x;
50  yMean += y;
51  ct++;
52 
53  outImage(x,y).r=255;
54  }
55  }
56  }
57  if (ct>0) {
58  xMean /= ct;
59  yMean /= ct;
60  }
61  if (ct>(image->width()/20)*(image->height()/20)) {
62  printf("Best guess at blue target: %g %g\n", xMean, yMean);
63  }
64 
65  outPort.write();
66  }
67  }
68  return 0;
69 }
70 
71 
72 
int main(int argc, char *argv[])
Copyright (C) 2008 RobotCub Consortium.