segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
lbpExtract.h
1 /*
2  * Copyright (C) 2015 Department of iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Vadim Tikhanoff
4  * email: vadim.tikhanoff@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16  */
17 
18 #ifndef __ICUB_LBPEXTRACT_MOD_H__
19 #define __ICUB_LBPEXTRACT_MOD_H__
20 
21 #include <yarp/os/BufferedPort.h>
22 #include <yarp/os/RFModule.h>
23 #include <yarp/os/Network.h>
24 #include <yarp/os/Thread.h>
25 #include <yarp/os/PeriodicThread.h>
26 #include <yarp/os/Time.h>
27 #include <yarp/os/Stamp.h>
28 #include <yarp/os/RpcClient.h>
29 #include <yarp/sig/Vector.h>
30 #include <yarp/sig/Image.h>
31 
32 #include <ctime>
33 #include <string>
34 #include <iostream>
35 #include <iomanip>
36 #include <map>
37 #include <mutex>
38 
39 #include <cstdio>
40 #include <cstdlib>
41 
42 #include <opencv2/opencv.hpp>
43 
44 #include "lbp.h"
45 #include "histogram.h"
46 
47 #include <lbpExtract_IDLServer.h>
48 
49 
50 class SEGMENTManager : public yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> >
51 {
52 private:
53 
54  std::string moduleName; //string containing module name
55  std::string inPortName; //string containing image input port name
56 
57  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > outPortPropagate;
58  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > outPortBlobs;
59  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > outPortSegmented;
60  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelMono> > outPortLbp;
61  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelMono> > outPortLbpContours;
62 
63  yarp::os::BufferedPort<yarp::os::Bottle> outTargetPort;
64 
65  cv::Mat imgMat;
66  int radius, defaultRadius;
67  int neighbours, defaultNeighbours;
68  int topBound, defaultTopBound;
69  int minArcLength, defaultMinArcLength;
70  int maxArcLength, defaultMaxArcLength;
71  int numIteration, defaultNumIteration;
72  int minArea, defaultMinArea;
73  int maxArea, defaultMaxArea;
74  int minWidth, defaultMinWidth;
75  int bbOffset;
76  bool verbose;
77 
78  cv::Mat segmented;
79  yarp::os::Bottle allPoints;
80  yarp::os::Bottle& getComponents(cv::Mat &img, int x, int y);
81  std::mutex semComp;
82 
83 public:
84 
90  SEGMENTManager( const std::string &moduleName );
91  ~SEGMENTManager();
92 
93  std::mutex mtx;
94 
95  bool open();
96  void close();
97  void onRead( yarp::sig::ImageOf<yarp::sig::PixelRgb> &img );
98  void interrupt();
99 
100  bool setDefaultValues(const int32_t radius, const int32_t neighbours, const int32_t topBound, const int32_t minArcLength, const int32_t maxArcLength, const int32_t numIteration, const int32_t minArea, const int32_t maxArea, const int32_t minWidth );
101 
102  bool setRadius(const int32_t radius);
103  bool setNeighbours(const int32_t neighbours);
104  bool setTopBound(const int32_t topBound);
105  bool setMinArcLength(const int32_t minArcLength);
106  bool setMaxArcLength(const int32_t maxArcLength);
107  bool setNumIteration(const int32_t numIteration);
108  bool setMinArea(const int32_t minArea);
109  bool setMaxArea(const int32_t maxArea);
110  bool setMinWidth(const int32_t minWidth);
111  bool setbbOffset(const int32_t offset);
112  bool resetAllValues();
113  bool verbosity(const int32_t boolVerbosity);
114 
115  int32_t getRadius();
116  int32_t getNeighbours();
117  int32_t getTopBound();
118  int32_t getMinArcLength();
119  int32_t getMaxArcLength();
120  int32_t getMinArea();
121  int32_t getMaxArea();
122  int32_t getMinWidth();
123  int32_t getNumIteration();
124  int32_t getbbOffset();
125  yarp::os::Bottle get_component_around(const int32_t x, const int32_t y);
126 };
127 
128 class SEGMENTModule:public yarp::os::RFModule, public lbpExtract_IDLServer
129 {
130  /* module parameters */
131  std::string moduleName;
132  std::string handlerPortName;
133  yarp::os::RpcServer rpcPort;
134 
135  /* pointer to a new thread */
136  SEGMENTManager *segmentManager;
137  bool closing;
138 
139  friend class segmentManager;
140 public:
141 
142 
143  bool configure(yarp::os::ResourceFinder &rf); // configure all the module parameters and return true if successful
144  bool interruptModule(); // interrupt, e.g., the ports
145  bool close(); // close and shut down the module
146 
147  bool attach(yarp::os::RpcServer &source);
148  bool reset();
149  bool quit();
150 
151  bool setRadius(const int32_t radius);
152  bool setNeighbours(const int32_t neighbours);
153  bool setTopBound(const int32_t topBound);
154  bool setMinArcLength(const int32_t minArcLength);
155  bool setMaxArcLength(const int32_t maxArcLength);
156  bool setNumIteration(const int32_t numIteration);
157  bool setMinArea(const int32_t minArea);
158  bool setMaxArea(const int32_t maxArea);
159  bool setMinWidth(const int32_t minWidth);
160  bool resetAllValues();
161  bool verbosity(const int32_t boolVerbosity);
162  bool setbbOffset(const int32_t offset);
163 
164  int32_t getRadius();
165  int32_t getNeighbours();
166  int32_t getTopBound();
167  int32_t getMinArcLength();
168  int32_t getMaxArcLength();
169  int32_t getMinArea();
170  int32_t getMaxArea();
171  int32_t getMinWidth();
172  int32_t getNumIteration();
173  int32_t getbbOffset();
174  yarp::os::Bottle get_component_around(const int32_t x, const int32_t y);
175 
176  double getPeriod();
177  bool updateModule();
178 };
179 
180 
181 #endif
182 //empty line to make gcc happy
lbpExtract_IDLServer Interface.
virtual bool setMinArea(const int32_t minArea)
Sets the minimum area of the allowed blobs.
virtual bool verbosity(const int32_t boolVerbosity)
Sets the verbosity of the algorithm.
virtual int32_t getbbOffset()
Gets the current offset of the bounding box.
virtual bool setRadius(const int32_t radius)
Sets the radius of the lbp operators.
virtual bool setNumIteration(const int32_t numIteration)
Sets the number of iteration for the grabCut segmentation algorithm.
virtual int32_t getMinArea()
Gets the minimum area of the allowed blobs.
virtual bool quit()
Quit the module.
virtual int32_t getMaxArea()
Gets the maximum area of the allowed blobs.
virtual int32_t getNumIteration()
Gets the number of iteration for the grabCut segmentation algorithm.
virtual bool setTopBound(const int32_t topBound)
Sets the top bound (Y) limit for the blobs.
virtual int32_t getTopBound()
Gets the top bound (Y) limit for the blobs.
virtual bool setMaxArea(const int32_t maxArea)
Sets the maximum area of the allowed blobs.
virtual bool resetAllValues()
resets all values to the default ones.
virtual int32_t getRadius()
Gets the radius of the lbp operators.
virtual yarp::os::Bottle get_component_around(const int32_t x, const int32_t y)
Gets all the components (points) that belong to any of the segmented blobs.
virtual bool reset()
Resets all the histograms.
virtual bool setNeighbours(const int32_t neighbours)
Sets the neighbours value of the lbp operators.
virtual bool setMaxArcLength(const int32_t maxArcLength)
Sets the maximum arc length of the allowed blobs.
virtual bool setMinArcLength(const int32_t minArcLength)
Sets the minimum arc length of the allowed blobs.
virtual int32_t getNeighbours()
Gets the neighbours of the lbp operators.
virtual int32_t getMaxArcLength()
Gets the maximum arc length of the allowed blobs.
virtual int32_t getMinArcLength()
Gets the minimum arc length of the allowed blobs.
virtual bool setbbOffset(const int32_t offset)
Sets the offset of the bounding box.