00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "HoughClusterer.h"
00012
00013
00014
00015
00016 #include "Workers/Puma2/GrayLevelImage8.h"
00017
00018 #include "ImageProperties.h"
00019 #include "HoughIndexCalculator.h"
00020
00021 #include "Workers/Math/vec2.h"
00022
00023 #include <algorithm>
00024 #include <assert.h>
00025 #include <map>
00026 #include <list>
00027 #include <math.h>
00028 #include <cstring>
00029
00030 #include <fstream>
00031
00032
00033 #define THIS HoughClusterer
00034
00035 THIS::THIS( std::vector< KeyPoint >* sceneKeyPoints, std::vector< KeyPoint >* objectImageKeyPoints, Point2D center, int imageWidth, int imageHeight)
00036 {
00037 m_Log << std::endl << "-------- Hough Transform Clustering -----------\n\n";
00038
00039 m_SceneKeyPoints = sceneKeyPoints;
00040
00041 m_ObjectImageKeyPoints = objectImageKeyPoints;
00042 m_Center = center;
00043
00044 m_ImageWidth = imageWidth;
00045 m_ImageHeight = imageHeight;
00046
00047
00048 m_ScaleBins = 10;
00049 m_OrientationBins = 10;
00050 m_XLocationBins = 10;
00051 m_YLocationBins = 10;
00052
00053 m_Log << "HoughClusterer [" << m_ScaleBins<< "*"<< m_OrientationBins << "*"<< m_XLocationBins <<"*"<<m_YLocationBins<< "]"<< " created -> ";
00054
00055
00056 m_HoughAccumulator = new HoughAccumulator();
00057
00058 }
00059
00060
00061 void THIS::setNNMatches(std::list< KeyPointMatch > nnrMatches)
00062 {
00063 m_Log << "Filling accumulator with " << nnrMatches.size() << " matches \n";
00064
00065
00066
00067 std::list< KeyPointMatch >::iterator it;
00068 it = nnrMatches.begin();
00069 while(it!=nnrMatches.end())
00070 {
00071 incrAccumulatorValue(m_SceneKeyPoints->at(it->index1), m_ObjectImageKeyPoints->at(it->index2),*it);
00072 ++it;
00073 }
00074
00075
00076 }
00077
00078 std::vector< std::list< KeyPointMatch> > THIS::clusterAccumulator()
00079 {
00080 m_Log << "Clustering Accumulator = " << "( + -> keep, - -> delete, x -> out of bounds -> index/value )\n\n";
00081
00082
00083
00084 std::vector< std::list< KeyPointMatch> > newMatches;
00085
00086 if(1)
00087 {
00088 newMatches = m_HoughAccumulator->getClusteredMatches();
00089 }
00090 else
00091 {
00092
00093 newMatches = m_HoughAccumulator->getMaximumMatches();
00094 }
00095
00096 m_Log << m_HoughAccumulator->getLog();
00097
00098 m_Log << "Number of clusters after hough clustering: " << newMatches.size() << std::endl;
00099
00100
00101
00102
00103
00104 return newMatches;
00105 }
00106
00107 void THIS::incrAccumulatorValue(KeyPoint scenePoint, KeyPoint objectPoint, KeyPointMatch match)
00108 {
00109
00110
00111 int scaleIndexFloor;
00112 int scaleIndexCeil;
00113
00114 double scaleQuotient = scenePoint.scale/objectPoint.scale;
00115 HoughIndexCalculator::calculateScaleIndex(scaleQuotient, scaleIndexFloor, scaleIndexCeil);
00116
00117
00118
00119 int orientationFloor;
00120 int orientationCeil;
00121
00122 double orientationQuotient = Math::minTurnAngle(scenePoint.orientation, objectPoint.orientation);
00123 HoughIndexCalculator::calculateOrientationIndex(orientationQuotient, orientationFloor, orientationCeil);
00124
00125
00126
00127 int xDistanceFloor;
00128 int xDistanceCeil;
00129
00130 int yDistanceFloor;
00131 int yDistanceCeil;
00132
00133 HoughIndexCalculator::calculatePositionIndex(scenePoint, objectPoint, m_Center, m_ImageWidth, m_ImageHeight, xDistanceFloor, xDistanceCeil, yDistanceFloor, yDistanceCeil);
00134
00135
00136
00137
00138
00139 int successCounter = 0;
00140
00141 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceFloor,yDistanceFloor, match))
00142 successCounter++;
00143 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceFloor,yDistanceCeil, match))
00144 successCounter++;
00145 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceCeil,yDistanceFloor, match))
00146 successCounter++;
00147 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceCeil,yDistanceCeil, match))
00148 successCounter++;
00149 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceFloor,yDistanceFloor, match))
00150 successCounter++;
00151 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceFloor,yDistanceCeil, match))
00152 successCounter++;
00153 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceCeil,yDistanceFloor, match))
00154 successCounter++;
00155 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceCeil,yDistanceCeil, match))
00156 successCounter++;
00157 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceFloor,yDistanceFloor, match))
00158 successCounter++;
00159 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceFloor,yDistanceCeil, match))
00160 successCounter++;
00161 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceCeil,yDistanceFloor, match))
00162 successCounter++;
00163 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceCeil,yDistanceCeil, match))
00164 successCounter++;
00165 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceFloor,yDistanceFloor, match))
00166 successCounter++;
00167 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceFloor,yDistanceCeil, match))
00168 successCounter++;
00169 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceCeil,yDistanceFloor, match))
00170 successCounter++;
00171 if(m_HoughAccumulator->incrAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceCeil,yDistanceCeil, match))
00172 successCounter++;
00173
00174 if(successCounter==0)
00175 {
00176 m_Log << "Incrementing match at scale "<< scaleQuotient << " orientation " << orientationQuotient << " posX "<< scenePoint.x << " posY "<< scenePoint.y << " failed." << std::endl;
00177 }
00178 }
00179
00180 bool THIS::getAccumulatorValue(KeyPoint scenePoint, KeyPoint objectPoint, unsigned int& value)
00181 {
00182
00183
00184 int scaleIndexFloor;
00185 int scaleIndexCeil;
00186
00187 double scaleQuotient = scenePoint.scale/objectPoint.scale;
00188 HoughIndexCalculator::calculateScaleIndex(scaleQuotient, scaleIndexFloor, scaleIndexCeil);
00189
00190
00191
00192 int orientationFloor;
00193 int orientationCeil;
00194
00195 double orientationQuotient = Math::minTurnAngle(scenePoint.orientation, objectPoint.orientation);
00196 HoughIndexCalculator::calculateOrientationIndex(orientationQuotient, orientationFloor, orientationCeil);
00197
00198
00199
00200 int xDistanceFloor;
00201 int xDistanceCeil;
00202
00203 int yDistanceFloor;
00204 int yDistanceCeil;
00205
00206 HoughIndexCalculator::calculatePositionIndex(scenePoint, objectPoint, m_Center, m_ImageWidth, m_ImageHeight, xDistanceFloor, xDistanceCeil, yDistanceFloor, yDistanceCeil);
00207
00208 std::vector<int> accumulatorValues;
00209
00210 unsigned int val;
00211
00212 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceFloor,yDistanceFloor,val))
00213 accumulatorValues.push_back(val);
00214
00215 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceFloor,yDistanceFloor,val))
00216 accumulatorValues.push_back(val);
00217
00218 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceFloor,yDistanceCeil,val))
00219 accumulatorValues.push_back(val);
00220
00221 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceCeil,yDistanceFloor,val))
00222 accumulatorValues.push_back(val);
00223
00224 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationFloor,xDistanceCeil,yDistanceCeil,val))
00225 accumulatorValues.push_back(val);
00226
00227 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceFloor,yDistanceFloor,val))
00228 accumulatorValues.push_back(val);
00229
00230 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceFloor,yDistanceCeil,val))
00231 accumulatorValues.push_back(val);
00232
00233 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceCeil,yDistanceFloor,val))
00234 accumulatorValues.push_back(val);
00235
00236 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexFloor,orientationCeil,xDistanceCeil,yDistanceCeil,val))
00237 accumulatorValues.push_back(val);
00238
00239 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceFloor,yDistanceFloor,val))
00240 accumulatorValues.push_back(val);
00241
00242 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceFloor,yDistanceCeil,val))
00243 accumulatorValues.push_back(val);
00244
00245 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceCeil,yDistanceFloor,val))
00246 accumulatorValues.push_back(val);
00247
00248 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationFloor,xDistanceCeil,yDistanceCeil,val))
00249 accumulatorValues.push_back(val);
00250
00251 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceFloor,yDistanceFloor,val))
00252 accumulatorValues.push_back(val);
00253
00254 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceFloor,yDistanceCeil,val))
00255 accumulatorValues.push_back(val);
00256
00257 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceCeil,yDistanceFloor,val))
00258 accumulatorValues.push_back(val);
00259
00260 if(m_HoughAccumulator->getAccumulatorValue(scaleIndexCeil,orientationCeil,xDistanceCeil,yDistanceCeil,val))
00261 accumulatorValues.push_back(val);
00262
00263 if(accumulatorValues.size()==0)
00264 {
00265 value = 0;
00266 m_Log << "Reading match "<< scaleQuotient << " " << orientationQuotient << " "<< scenePoint.x << " "<< scenePoint.y << " failed. \n";
00267 return false;
00268 }
00269 else
00270 {
00271 value = *std::max_element(accumulatorValues.begin(), accumulatorValues.end());
00272 return true;
00273 }
00274 }
00275
00276 float THIS::getVariance()
00277 {
00278 return m_HoughAccumulator->getVariance();
00279 }
00280
00281 void THIS::getImage( puma2::ColorImageRGB8& target )
00282 {
00283 m_HoughAccumulator->getImage(target);
00284 }
00285
00286
00287 std::string THIS::getLog()
00288 {
00289 return m_Log.str();
00290 }
00291
00292
00293 THIS::~THIS()
00294 {
00295 delete m_HoughAccumulator;
00296 }
00297
00298 #undef THIS