00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef GHough_H_DEFINED
00039 #define GHough_H_DEFINED
00040
00041 #include "Util/Types.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Image/ImageSet.H"
00045 #include "Image/Point2D.H"
00046 #include "Image/Point3D.H"
00047 #include "Util/CpuTimer.H"
00048 #include "Util/JobServer.H"
00049 #include "Util/JobWithSemaphore.H"
00050 #include "Util/WorkThreadServer.H"
00051 #include "Learn/SOFM.H"
00052
00053 #include <vector>
00054 #include <map>
00055
00056 class GHough
00057 {
00058 public:
00059
00060 struct Feature
00061 {
00062 Point2D<float> loc;
00063 std::vector<float> values;
00064 };
00065
00066 struct RTable
00067 {
00068 std::map<int, std::vector<Point2D<float> > > entries;
00069 std::map<long, std::vector<Feature> > featureEntries;
00070 };
00071
00072 struct Model
00073 {
00074 int id;
00075 int type;
00076 Point2D<int> center;
00077 std::map<int,std::vector<double> > invRTable;
00078 std::vector<RTable> rTables;
00079 Point2D<float> imgPos;
00080 Point3D<float> pos;
00081 Point3D<float> rot;
00082 int numFeatures;
00083 };
00084
00085
00086
00087 struct Acc
00088 {
00089 int id;
00090 Point2D<int> pos;
00091 float votes;
00092 float prob;
00093
00094 Acc(int i, Point2D<int> p, float w) :
00095 id(i), pos(p), votes(w), prob(0)
00096 {}
00097 Acc(int i, int x, int y, float w) :
00098 id(i), pos(x,y), votes(w), prob(0)
00099 {}
00100
00101 Acc() {}
00102 };
00103
00104
00105 struct AccCmp
00106 {
00107 bool operator()(const Acc& c1, const Acc& c2)
00108 {
00109 return c1.prob > c2.prob;
00110 }
00111 };
00112
00113
00114 GHough();
00115
00116
00117 virtual ~GHough();
00118
00119
00120 void createInvRTable(const Image<byte>& img, const Image<float>& ang);
00121
00122
00123 RTable createRTable(const Image<byte>& img, const Image<float>& ang, Point2D<float>& imgPos, int& numFeatures,
00124 Point2D<float>& imgLoc);
00125
00126 RTable createRTable(const std::vector<Feature>& features, Point2D<float>& imgPos, Point2D<float>& imgLoc);
00127
00128 RTable createRTable(const Image<byte>& img, const Image<float>& ang,
00129 Point2D<int>& center, int& numFeatures);
00130
00131
00132
00133 Image<float> getInvVotes(const Image<byte>& img, const Image<float>& ang);
00134
00135
00136 long getIndex(const std::vector<float>& values);
00137
00138
00139 std::vector<Acc> getVotes(const Image<byte>& img, const Image<float>& ang);
00140
00141 std::vector<Acc> getVotes(const std::vector<Feature>& features);
00142
00143 std::vector<Acc> getVotes(int id, const RTable& rTable, const Image<byte>& img, const Image<float>& ang);
00144
00145 std::vector<Acc> getVotes(int id, const RTable& rTable, const std::vector<Feature>& features, float& maxVotes);
00146 std::vector<Acc> getVotes2(int id, const RTable& rTable, const std::vector<Feature>& features, float& maxVotes);
00147
00148 std::vector<Acc> getVotes(const Image<float>& img);
00149
00150 std::vector<Acc> getVotes(const Image<float>& img, const Image<float>& ori);
00151
00152
00153 Point2D<float> addModel(int& id, const Image<byte>& img, const Image<float>& ang,
00154 Point3D<float> pos, Point3D<float> rot);
00155
00156 Point2D<float> addModel(int& id, int type, const std::vector<Feature>& features,
00157 Point3D<float> pos, Point3D<float> rot);
00158
00159
00160 void addModel(int id, const Image<float>& img);
00161
00162 Point2D<int> addModel(int id, const std::vector<Point2D<int> >& polygon);
00163
00164 void setPosOffset(int id, Point3D<float> pos);
00165
00166 void writeTable(const char* filename);
00167
00168 void readTable(const char* filename);
00169
00170 Point2D<float> getModelImgPos(const int id);
00171
00172 Point3D<float> getModelRot(const int id);
00173
00174 Point3D<float> getModelPosOffset(const int id);
00175
00176 int getModelType(const int id);
00177
00178 Image<PixRGB<byte> > getRTableImg(const int id);
00179
00180 uint getNumFeatures(const int id);
00181
00182 Image<float> getAccImg(std::vector<GHough::Acc>& acc);
00183
00184 Point2D<int> findInvFeature(const int x, const int y,
00185 const Image<float>& img, const Image<float>& ang);
00186
00187 void trainSOFM();
00188
00189 private:
00190 std::vector<Model> itsModels;
00191 int itsNumEntries;
00192 rutz::shared_ptr<WorkThreadServer> itsThreadServer;
00193
00194 SOFM* itsSOFM;
00195
00196 };
00197
00198
00199
00200
00201
00202
00203
00204
00205 #endif //