segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
SegmModule.cpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 /*
4  * Copyright (C) 2012 Elena Ceseracciu, RBCS - Istituto Italiano di Tecnologia
5  * CopyPolicy: Released under the terms of the GNU GPL v2.0 and later.
6  *
7  */
8 
9 #include <string>
10 #include <iostream>
11 using namespace std;
12 
13 
14 #include "SegmModule.h"
15 #include "segment-image.h"
16 
17 
18 // yarp
19 #include <yarp/os/Network.h>
20 using namespace yarp::os;
21 using namespace yarp::sig;
22 
23 void YarpImageToRGBImage(image<rgb> * im, ImageOf<PixelRgb> * yarpIm) {
24 
25  for (int y = 0; y < im->height(); y++) {
26 
27  for (int x = 0; x < im->width(); x++) {
28 
29  imRef(im, x, y).r = yarpIm->pixel(x,y).r;
30  imRef(im, x, y).g = yarpIm->pixel(x,y).g;
31  imRef(im, x, y).b = yarpIm->pixel(x,y).b;
32 
33 
34  }
35 
36  }
37 
38 }
39 
40 
41 
42 
43 
44 void RGBImageToYarpImage(image<rgb> * im, ImageOf<PixelRgb> * yarpIm) {
45 
46  for (int y = 0; y < im->height(); y++) {
47 
48  for (int x = 0; x < im->width(); x++) {
49 
50  yarpIm->pixel(x,y).r = (uchar) imRef(im, x, y).r;
51 
52  yarpIm->pixel(x,y).g = (uchar) imRef(im, x, y).g;
53 
54  yarpIm->pixel(x,y).b = (uchar) imRef(im, x, y).b;
55 
56 
57 
58  }
59 
60  }
61 
62 }
63 
64 void GBSegmModule::set_sigma(const double newValue) {sigma=(float)newValue;}
65 void GBSegmModule::set_k(const double newValue){k=(float)newValue;}
66 void GBSegmModule::set_minRegion(const double newValue){min_size=(int)newValue;}
67 double GBSegmModule::get_sigma(){return sigma;}
68 double GBSegmModule::get_k(){return k;}
69 double GBSegmModule::get_minRegion(){return min_size;}
70 int32_t GBSegmModule::get_num_components(){return num_components;}
71 std::vector< Pixel > GBSegmModule::get_component_around(const Pixel& objCenter)
72 {
73  vector<Pixel> result;
74  result.clear();
75  lock_guard<mutex> lg(segMutex);
76  rgb componentColor = imRef(seg, objCenter.x, objCenter.y);
77  for (int y = 0; y < seg->height(); y++) {
78 
79  for (int x = 0; x < seg->width(); x++) {
80 
81  if (imRef(seg, x, y) == componentColor)
82  result.push_back(Pixel(x, y));
83 
84 
85  }
86 
87  }
88 
89  return result;
90 }
91 
92 
93 GBSegmModule::GBSegmModule() : _stamp(0,0)
94 {
95  input = NULL;
96  seg = NULL;
97  // parameters default values
98  sigma = 0.8F;
99  k= 700;
100  min_size = 700;
101 }
102 
103 GBSegmModule::~GBSegmModule()
104 {
105  if(input)
106  delete input;
107  if (seg)
108  delete seg;
109 }
110 
111 
112 bool GBSegmModule::configure (yarp::os::ResourceFinder &rf)
113 {
114  if (rf.check("help","if present, display usage message")) {
115  printf("Call with --from configfile.ini\n");
116  return false;
117  }
118 
119  if (rf.check("name"))
120  setName(rf.find("name").asString().c_str());
121  else setName("GBSeg");
122 
123 
124  //override defaults if specified - TODO: range checking
125  if(rf.check("sigma")) sigma = (float)rf.find("sigma").asFloat64();
126  if(rf.check("k")) k = (float)rf.find("k").asFloat64();
127  if(rf.check("minRegion")) min_size = rf.find("minRegion").asInt32();
128 
129  std::string slash="/";
130  _imgPort.open(slash + getName("/rawImg:i"));
131  _configPort.open(slash + getName("/conf"));
132  _viewPort.open(slash +getName("/viewImg:o"));
133  attach(_configPort);
134 
135  //read an image to get the dimensions
136  ImageOf<PixelRgb> *yrpImgIn;
137  yrpImgIn = _imgPort.read();
138  if (yrpImgIn == NULL) // this is the case if module is requested to quit while waiting for image
139  return true;
140  input=new image<rgb>(yrpImgIn->width(), yrpImgIn->height(), true);
141 
142 //
143 // //THIS IS NOT REQUIRED - INPUT IMAGES ARE ALWAYS RGB
144 // //IF DIM == 3 THEN THE PROCESSING CLASS CONVERTS TO LUV
145 // //IF DIM == 1 THEN THE PROCESSING CLASS ONLY USES THE FIRST CHANNEL
146 // //WE USE HUE CHANNEL, SO MUST CONVERT FROM RGB TO HSV
147 // //check compatibility of image depth
148 // /*if (yrpImgIn->getPixelSize() != dim_)
149 // {
150 // cout << endl << "Incompatible image depth" << endl;
151 // return false;
152 // }*/
153 //
154 // //override internal image dimension if necessary
155 // if( width_ > orig_width_ )
156 // width_ = orig_width_;
157 // if( height_ > orig_height_ )
158 // height_ = orig_height_;
159 //
160 // //allocate memory for image buffers and get the pointers
161 //
162 // inputImage.resize(width_, height_); inputImage_ = inputImage.getRawImage();
163 // inputHsv.resize(width_, height_); inputHsv_ = inputHsv.getRawImage();
164 // inputHue.resize(width_, height_); inputHue_ = inputHue.getRawImage();
165 // filtImage.resize(width_, height_); filtImage_ = filtImage.getRawImage();
166 // segmImage.resize(width_, height_); segmImage_ = segmImage.getRawImage();
167 // gradMap.resize(width_, height_); gradMap_ = (float*)gradMap.getRawImage();
168 // confMap.resize(width_, height_); confMap_ = (float*)confMap.getRawImage();
169 // weightMap.resize(width_, height_); weightMap_ = (float*)weightMap.getRawImage();
170 // labelImage.resize(width_, height_);
171 // labelView.resize(width_, height_);
172 
173  return true;
174 }
175 
176 bool GBSegmModule::close()
177 {
178  _imgPort.close();
179  _viewPort.close();
180  _configPort.close();
181 
182  // also deallocate image _frame
183  return true;
184 }
185 
186 bool GBSegmModule::interruptModule(){
187 
188  _imgPort.interrupt();
189  _configPort.interrupt();
190  _viewPort.interrupt();
191 
192  return true;
193 }
194 
195 bool GBSegmModule::updateModule()
196 {
197  ImageOf<PixelRgb> *yrpImgIn;
198  static int cycles = 0;
199 
200  yrpImgIn = _imgPort.read();
201  if (yrpImgIn == NULL) // this is the case if module is requested to quit while waiting for image
202  return true;
203 
204  bool use_private_stamp;
205  Stamp s;
206  if(!_imgPort.getEnvelope(s))
207  {
208  cout << "No stamp found in input image. Will use private stamp" << endl;
209  use_private_stamp = true;
210  }
211  else
212  {
213  cout << "Received image #" << s.getCount() << " generated at time " << s.getTime() << endl;
214  use_private_stamp = false;
215  }
216 
217  if(cycles == 0)
218  _timestart = yarp::os::Time::now();
219  cycles++;
220 
221  cout << "converting image of size " << yrpImgIn->width() << yrpImgIn->height() <<" to size" << input->width() << input->height() << endl;
222  YarpImageToRGBImage(input, yrpImgIn);
223  cout << "converted" << endl;
224  segMutex.lock();
225  if(seg)
226  delete seg;
227  seg=segment_image(input, sigma, k, min_size, &num_components);
228  segMutex.unlock();
229 
230  cout << "processed" << endl;
231  //prepare timestamps
232  if(use_private_stamp)
233  {
234  _stamp.update();
235  _viewPort.setEnvelope(_stamp);
236  }
237  else
238  {
239  _viewPort.setEnvelope(s);
240  }
241 
242 
243 
244  ImageOf<PixelRgb> &yrpImgView = _viewPort.prepare();
245  //Rescale image if required
246 
247  yrpImgView.resize(seg->width(), seg->height());
248  RGBImageToYarpImage(seg, &yrpImgView);
249  _viewPort.write();
250 
251 
252  //report the frame rate
253  if(cycles % 100 == 0)
254  {
255  double cps = ((double)cycles)/(yarp::os::Time::now() - _timestart);
256  printf("fps: %02.2f\n", cps);
257  }
258  return true;
259 }
260 
261 
virtual std::vector< yarp::sig::Pixel > get_component_around(const yarp::sig::Pixel &objCenter)
Get the list of pixels corresponding to the component to which a given pixel belongs.
Definition: SegmModule.cpp:71
virtual void set_k(const double newValue)
Set k (scale factor for boundary-detection threshold function) parameter for the algorithm.
Definition: SegmModule.cpp:65
virtual double get_minRegion()
Get minRegion parameter for the algorithm, i.e., the minimum size of any segmented component.
Definition: SegmModule.cpp:69
virtual void set_sigma(const double newValue)
Set sigma (smoothing) parameter for the algorithm.
Definition: SegmModule.cpp:64
virtual double get_k()
Get k (scale factor for boundary-detection threshold function) parameter for the algorithm.
Definition: SegmModule.cpp:68
virtual int32_t get_num_components()
Get the number of segmented components that have been detected in the last provided image.
Definition: SegmModule.cpp:70
virtual void set_minRegion(const double newValue)
Set minRegion parameter for the algorithm, i.e., the minimum size of any segmented component.
Definition: SegmModule.cpp:66
virtual double get_sigma()
Get sigma (smoothing) parameter for the algorithm.
Definition: SegmModule.cpp:67
Pixel position in the image frame.
Definition: Pixel.h:28
std::int32_t y
Index of pixel along vertical axis.
Definition: Pixel.h:38
std::int32_t x
Index of pixel along horizontal axis.
Definition: Pixel.h:34