14 #include "SegmModule.h"
15 #include "segment-image.h"
19 #include <yarp/os/Network.h>
20 using namespace yarp::os;
21 using namespace yarp::sig;
23 void YarpImageToRGBImage(image<rgb> * im, ImageOf<PixelRgb> * yarpIm) {
25 for (
int y = 0; y < im->height(); y++) {
27 for (
int x = 0; x < im->width(); x++) {
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;
44 void RGBImageToYarpImage(image<rgb> * im, ImageOf<PixelRgb> * yarpIm) {
46 for (
int y = 0; y < im->height(); y++) {
48 for (
int x = 0; x < im->width(); x++) {
50 yarpIm->pixel(x,y).r = (uchar) imRef(im, x, y).r;
52 yarpIm->pixel(x,y).g = (uchar) imRef(im, x, y).g;
54 yarpIm->pixel(x,y).b = (uchar) imRef(im, x, y).b;
75 lock_guard<mutex> lg(segMutex);
76 rgb componentColor = imRef(seg, objCenter.
x, objCenter.
y);
77 for (
int y = 0; y < seg->height(); y++) {
79 for (
int x = 0; x < seg->width(); x++) {
81 if (imRef(seg, x, y) == componentColor)
82 result.push_back(
Pixel(x, y));
93 GBSegmModule::GBSegmModule() : _stamp(0,0)
103 GBSegmModule::~GBSegmModule()
112 bool GBSegmModule::configure (yarp::os::ResourceFinder &rf)
114 if (rf.check(
"help",
"if present, display usage message")) {
115 printf(
"Call with --from configfile.ini\n");
119 if (rf.check(
"name"))
120 setName(rf.find(
"name").asString().c_str());
121 else setName(
"GBSeg");
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();
129 std::string slash=
"/";
130 _imgPort.open(slash + getName(
"/rawImg:i"));
131 _configPort.open(slash + getName(
"/conf"));
132 _viewPort.open(slash +getName(
"/viewImg:o"));
136 ImageOf<PixelRgb> *yrpImgIn;
137 yrpImgIn = _imgPort.read();
138 if (yrpImgIn == NULL)
140 input=
new image<rgb>(yrpImgIn->width(), yrpImgIn->height(),
true);
176 bool GBSegmModule::close()
186 bool GBSegmModule::interruptModule(){
188 _imgPort.interrupt();
189 _configPort.interrupt();
190 _viewPort.interrupt();
195 bool GBSegmModule::updateModule()
197 ImageOf<PixelRgb> *yrpImgIn;
198 static int cycles = 0;
200 yrpImgIn = _imgPort.read();
201 if (yrpImgIn == NULL)
204 bool use_private_stamp;
206 if(!_imgPort.getEnvelope(s))
208 cout <<
"No stamp found in input image. Will use private stamp" << endl;
209 use_private_stamp =
true;
213 cout <<
"Received image #" << s.getCount() <<
" generated at time " << s.getTime() << endl;
214 use_private_stamp =
false;
218 _timestart = yarp::os::Time::now();
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;
227 seg=segment_image(input, sigma, k, min_size, &num_components);
230 cout <<
"processed" << endl;
232 if(use_private_stamp)
235 _viewPort.setEnvelope(_stamp);
239 _viewPort.setEnvelope(s);
244 ImageOf<PixelRgb> &yrpImgView = _viewPort.prepare();
247 yrpImgView.resize(seg->width(), seg->height());
248 RGBImageToYarpImage(seg, &yrpImgView);
253 if(cycles % 100 == 0)
255 double cps = ((double)cycles)/(yarp::os::Time::now() - _timestart);
256 printf(
"fps: %02.2f\n", cps);
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.
virtual void set_k(const double newValue)
Set k (scale factor for boundary-detection threshold function) parameter for the algorithm.
virtual double get_minRegion()
Get minRegion parameter for the algorithm, i.e., the minimum size of any segmented component.
virtual void set_sigma(const double newValue)
Set sigma (smoothing) parameter for the algorithm.
virtual double get_k()
Get k (scale factor for boundary-detection threshold function) parameter for the algorithm.
virtual int32_t get_num_components()
Get the number of segmented components that have been detected in the last provided image.
virtual void set_minRegion(const double newValue)
Set minRegion parameter for the algorithm, i.e., the minimum size of any segmented component.
virtual double get_sigma()
Get sigma (smoothing) parameter for the algorithm.
Pixel position in the image frame.
std::int32_t y
Index of pixel along vertical axis.
std::int32_t x
Index of pixel along horizontal axis.