8 extern bool CmCDisplayProgress;
13 #include "EdisonSegmModule.h"
15 #include <edge/BgImage.h>
16 #include <edge/BgEdge.h>
17 #include <edge/BgEdgeList.h>
18 #include <edge/BgEdgeDetect.h>
19 #include <edge/BgDefaults.h>
22 #include <yarp/os/Network.h>
23 #include <yarp/cv/Cv.h>
26 using namespace yarp::os;
27 using namespace yarp::sig;
28 using namespace yarp::cv;
30 void EdisonSegmModule::setSpeedUpValue(
int newSpeedUpValue)
32 switch(newSpeedUpValue) {
33 case(0): speedup = NO_SPEEDUP;
break;
34 case(1): speedup = MED_SPEEDUP;
break;
35 case(2): speedup = HIGH_SPEEDUP;
break;
36 default: speedup = NO_SPEEDUP;
40 EdisonSegmModule::EdisonSegmModule() : _stamp(0,0)
49 inputImage_ = (
unsigned char *) NULL;
50 filtImage_ = (
unsigned char *) NULL;
51 segmImage_ = (
unsigned char *) NULL;
52 gradMap_ = (
float *) NULL;
53 confMap_ = (
float *) NULL;
54 weightMap_ = (
float *) NULL;
55 edges_ = (
int *) NULL;
56 boundaries_ = (
int *) NULL;
65 speedup = MED_SPEEDUP;
68 EdisonSegmModule::~EdisonSegmModule()
70 if(edges_)
delete [] edges_;
71 if(boundaries_)
delete [] boundaries_;
75 bool EdisonSegmModule::configure (yarp::os::ResourceFinder &rf)
77 if (rf.check(
"help",
"if present, display usage message")) {
78 printf(
"Call with --from configfile.ini\n");
83 setName(rf.find(
"name").asString().c_str());
84 else setName(
"edisonSeg");
96 speedup = MED_SPEEDUP;
98 if(rf.check(
"height")) height_ = rf.find(
"height").asInt32();
99 if(rf.check(
"width")) width_ = rf.find(
"width").asInt32();
101 if(rf.check(
"sigmaS")) sigmaS = rf.find(
"sigmaS").asInt32();
102 if(rf.check(
"sigmaR")) sigmaR = (float)rf.find(
"sigmaR").asFloat64();
103 if(rf.check(
"minRegion")) minRegion = rf.find(
"minRegion").asInt32();
104 if(rf.check(
"gradWinRad")) gradWindRad = (
int)rf.find(
"gradWinRad").asFloat64();
105 if(rf.check(
"threshold")) threshold = (
float)rf.find(
"threshold").asFloat64();
106 if(rf.check(
"mixture")) mixture = (
float)rf.find(
"mixture").asFloat64();
107 if(rf.check(
"speedup"))
109 int spdp = rf.find(
"speedup").asInt32();
110 setSpeedUpValue(spdp);
113 std::string slash=
"/";
114 _imgPort.open(slash + getName(
"/rawImg:i"));
115 _configPort.open(slash + getName(
"/conf"));
116 _filtPort.open(slash + getName(
"/filtImg:o"));
117 _labelPort.open(slash +getName(
"/labeledImg:o"));
118 _viewPort.open(slash +getName(
"/viewImg:o"));
119 _rawPort.open(slash +getName(
"/rawImg:o"));
120 _labelViewPort.open(slash +getName(
"/debugImg:o"));
125 ImageOf<PixelRgb> *yrpImgIn;
126 yrpImgIn = _imgPort.read();
127 if (yrpImgIn == NULL)
129 orig_height_ = yrpImgIn->height();
130 orig_width_ = yrpImgIn->width();
133 if( width_ > orig_width_ )
134 width_ = orig_width_;
135 if( height_ > orig_height_ )
136 height_ = orig_height_;
140 inputImage.resize(width_, height_); inputImage_ = inputImage.getRawImage();
141 inputHsv.resize(width_, height_); inputHsv_ = inputHsv.getRawImage();
142 inputHue.resize(width_, height_); inputHue_ = inputHue.getRawImage();
143 filtImage.resize(width_, height_); filtImage_ = filtImage.getRawImage();
144 segmImage.resize(width_, height_); segmImage_ = segmImage.getRawImage();
145 gradMap.resize(width_, height_); gradMap_ = (
float*)gradMap.getRawImage();
146 confMap.resize(width_, height_); confMap_ = (
float*)confMap.getRawImage();
147 weightMap.resize(width_, height_); weightMap_ = (
float*)weightMap.getRawImage();
148 labelImage.resize(width_, height_);
149 labelView.resize(width_, height_);
154 bool EdisonSegmModule::close()
158 _labelViewPort.close();
168 bool EdisonSegmModule::interruptModule(){
170 _imgPort.interrupt();
171 _configPort.interrupt();
172 _labelPort.interrupt();
173 _filtPort.interrupt();
174 _labelViewPort.interrupt();
175 _viewPort.interrupt();
176 _rawPort.interrupt();
180 bool EdisonSegmModule::updateModule()
182 ImageOf<PixelRgb> *yrpImgIn;
183 static int cycles = 0;
185 yrpImgIn = _imgPort.read();
186 if (yrpImgIn == NULL)
189 bool use_private_stamp;
191 if(!_imgPort.getEnvelope(s))
193 cout <<
"No stamp found in input image. Will use private stamp" << endl;
194 use_private_stamp =
true;
198 cout <<
"Received image #" << s.getCount() <<
" generated at time " << s.getTime() << endl;
199 use_private_stamp =
false;
203 _timestart = yarp::os::Time::now();
206 cv::Mat imgMat = toCvMat(*yrpImgIn);
209 if( (width_ != orig_width_) || (height_ != orig_height_ ) )
210 cv::resize(imgMat,toCvMat(inputImage),cv::Size(),1.0,1.0);
212 imgMat.copyTo(toCvMat(inputImage));
214 double edgetime = yarp::os::Time::now();
216 BgEdgeDetect edgeDetector(gradWindRad);
218 bgImage.SetImage(inputImage_, width_, height_,
true);
219 edgeDetector.ComputeEdgeInfo(&bgImage, confMap_, gradMap_);
221 for(
int i = 0; i < width_*height_; i++) {
222 if(gradMap_[i] > 0.02) {
223 weightMap_[i] = mixture*gradMap_[i] + (1 - mixture)*confMap_[i];
229 cout <<
"Edge computation Time (ms): " << (yarp::os::Time::now() - edgetime)*1000.0 << endl;
231 msImageProcessor iProc;
233 iProc.DefineImage(inputImage_, COLOR, height_, width_);
236 vector<cv::Mat> channels;
237 cv::cvtColor(toCvMat(inputImage), toCvMat(inputHsv), CV_RGB2HSV);
238 cv::split(toCvMat(inputHsv),channels);
239 toCvMat(inputHue)=channels[0];
240 iProc.DefineImage(inputHue_, GRAYSCALE, height_, width_);
242 if(iProc.ErrorStatus) {
243 cout <<
"MeanShift Error" << endl;
246 iProc.SetWeightMap(weightMap_, threshold);
247 if(iProc.ErrorStatus) {
248 cout <<
"MeanShift Error" << endl;
252 double filtertime = yarp::os::Time::now();
253 iProc.Filter(sigmaS, sigmaR, speedup);
254 if(iProc.ErrorStatus) {
255 cout <<
"MeanShift Error" << endl;
258 cout <<
"Mean Shift Filter Computation Time (ms): " << (yarp::os::Time::now() - filtertime)*1000.0 << endl;
261 iProc.GetResults(filtImage_);
262 if(iProc.ErrorStatus) {
263 cout <<
"MeanShift Error" << endl;
268 double fusetime = yarp::os::Time::now();
269 iProc.FuseRegions(sigmaR, minRegion);
270 if(iProc.ErrorStatus) {
271 cout <<
"MeanShift Error" << endl;
274 cout <<
"Region Fusion Computation Time (ms): " << (yarp::os::Time::now() - fusetime)*1000.0 << endl;
277 iProc.GetResults(segmImage_);
278 if(iProc.ErrorStatus) {
279 cout <<
"MeanShift Error" << endl;
286 int *modePointCounts;
288 regionCount = iProc.GetRegions(&labels, &modes, &modePointCounts);
289 int *labelp = (
int*)labelImage.getRawImage();
290 for(
int i = 0; i < width_*height_; i++)
291 labelp[i] = labels[i];
293 cv::convertScaleAbs(toCvMat(labelImage),toCvMat(labelView));
296 if(use_private_stamp)
299 _labelPort.setEnvelope(_stamp);
300 _labelViewPort.setEnvelope(_stamp);
301 _viewPort.setEnvelope(_stamp);
302 _filtPort.setEnvelope(_stamp);
303 _rawPort.setEnvelope(_stamp);
307 _labelPort.setEnvelope(s);
308 _labelViewPort.setEnvelope(s);
309 _viewPort.setEnvelope(s);
310 _filtPort.setEnvelope(s);
311 _rawPort.setEnvelope(s);
314 ImageOf<PixelInt> &yrpImgLabel = _labelPort.prepare();
316 if( (width_ != orig_width_) || (height_ != orig_height_ ) )
318 yrpImgLabel.resize(orig_width_, orig_height_);
319 cv::resize(toCvMat(labelImage),toCvMat(yrpImgLabel),cv::Size(),1.0,1.0);
322 yrpImgLabel = labelImage;
325 ImageOf<PixelMono> &yrpImgDebug = _labelViewPort.prepare();
327 if( (width_ != orig_width_) || (height_ != orig_height_ ) )
329 yrpImgDebug.resize(orig_width_, orig_height_);
330 cv::resize(toCvMat(labelView),toCvMat(yrpImgDebug),cv::Size(),1.0,1.0);
333 yrpImgDebug = labelView;
334 _labelViewPort.write();
337 ImageOf<PixelRgb> &yrpFiltOut = _filtPort.prepare();
339 if( (width_ != orig_width_) || (height_ != orig_height_ ) )
341 yrpFiltOut.resize(orig_width_, orig_height_);
342 cv::resize(toCvMat(filtImage),toCvMat(yrpFiltOut),cv::Size(),1.0,1.0);
345 yrpFiltOut = filtImage;
348 ImageOf<PixelRgb> &yrpImgView = _viewPort.prepare();
350 if( (width_ != orig_width_) || (height_ != orig_height_ ) )
352 yrpImgView.resize(orig_width_, orig_height_);
353 cv::resize(toCvMat(segmImage),toCvMat(yrpImgView),cv::Size(),1.0,1.0);
356 yrpImgView = segmImage;
359 ImageOf<PixelRgb> &yrpImgOut = _rawPort.prepare();
360 yrpImgOut = *yrpImgIn;
364 if(cycles % 100 == 0)
366 double cps = ((double)cycles)/(yarp::os::Time::now() - _timestart);
367 printf(
"fps: %02.2f\n", cps);