tracks.cpp
Go to the documentation of this file.
00001 
00005 #include "tracks.hpp"
00006 
00007 featureTrack::featureTrack() {
00008         isTriangulated = false;
00009 }
00010 
00011 void featureTrack::set3dLoc(const cv::Point3d& loc) {
00012         
00013         if (!isTriangulated) {
00014                 xyzEstimate.x = loc.x;
00015                 xyzEstimate.y = loc.y;
00016                 xyzEstimate.z = loc.z;
00017         
00018                 isTriangulated = true;
00019         }
00020         
00021 }
00022 
00023 void featureTrack::reset3dLoc(const cv::Point3d& loc) {
00024         
00025         xyzEstimate.x = loc.x;
00026         xyzEstimate.y = loc.y;
00027         xyzEstimate.z = loc.z;
00028 
00029         isTriangulated = true;
00030         
00031 }
00032 
00033 unsigned int getActiveTrackCount(const vector<featureTrack>& featureTrackVector, unsigned int previousIndex, unsigned int latestIndex) {
00034         
00035         unsigned int trackCount = 0;
00036         
00037         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00038                 
00039                 if (featureTrackVector.at(iii).locations.size() < 2) {
00040                         continue;
00041                 }
00042                 
00043                 if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).imageIndex == latestIndex) {
00044                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-2).imageIndex == previousIndex) {
00045                                 trackCount++;
00046                         }
00047                 }
00048                 
00049         }
00050         
00051         return trackCount;
00052                 
00053 }
00054 
00055 double obtainFeatureSpeeds(const vector<featureTrack>& featureTrackVector, unsigned int idx1, double time1, unsigned int idx2, double time2) {
00056         
00057         
00058         double cumulativeVelocity = 0.0;
00059         unsigned int cumulativeCount = 0;
00060         
00061         //printf("%s << Entered with (%d, %d)\n", __FUNCTION__, idx1, idx2);
00062         
00063         if ((time1 == time2) || (time1 == 0.0) || (time2 == 0.0)) {
00064                 return -1.0;
00065         }
00066         
00067         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00068                 
00069                 if (featureTrackVector.at(iii).locations.size() < 2) {
00070                         continue;
00071                 }
00072                 
00073                 // If the last two features match the frame indices you're interested in...
00074                 
00075                 if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).imageIndex == idx2) {
00076                         
00077                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-2).imageIndex == idx1) {
00078                                 
00079                                 //printf("%s << Found match at (%d)\n", __FUNCTION__, iii);
00080                                 cumulativeCount++;
00081                                 cv::Point2f p1, p2, v;
00082                                 
00083                                 p1 = featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-2).featureCoord;
00084                                 p2 = featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord;
00085                                 
00086                                 v.x = (p2.x - p1.x) / (time2 - time1);
00087                                 v.y = (p2.y - p1.y) / (time2 - time1);
00088                                 
00089                                 cumulativeVelocity += pow(pow(v.x, 2.0)+pow(v.y, 2.0), 0.5);
00090                                 
00091                         }
00092                         
00093                 }
00094                 
00095                 
00096         }
00097         
00098         
00099         if (cumulativeCount > 0) {
00100                 cumulativeVelocity /= double(cumulativeCount);
00101         } else {
00102                 cumulativeVelocity = 0.0;
00103         }
00104         
00105         return cumulativeVelocity;
00106 
00107 }
00108 
00109 double updateFeatureSpeeds(vector<featureTrack>& featureTrackVector, unsigned int idx1, double time1, unsigned int idx2, double time2, double maxVelocity) {
00110         
00111         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00112                 
00113                 featureTrackVector.at(iii).velocity_x = 0.0;
00114                 featureTrackVector.at(iii).velocity_y = 0.0;
00115                 
00116         }
00117         
00118         if ((time1 == time2) || (time1 == 0.0) || (time2 == 0.0)) {
00119                 return -1.0;
00120         } else if (abs(time1 - time2) > MAX_TIME_DIFF_FOR_PREDICTION) {
00121                 return -1.0;
00122         }
00123         
00124         
00125         double cumulativeVelocity = 0.0;
00126         unsigned int cumulativeCount = 0;
00127         
00128         //printf("%s << Entered with (%d, %d)\n", __FUNCTION__, idx1, idx2);
00129         
00130         
00131         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00132 
00133                 
00134                 if (featureTrackVector.at(iii).locations.size() < 2) {
00135                         continue;
00136                 }
00137                 
00138                 // If the last two features match the frame indices you're interested in...
00139                 
00140                 if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).imageIndex == idx2) {
00141                         
00142                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-2).imageIndex == idx1) {
00143                                 
00144                                 //printf("%s << Found match at (%d)\n", __FUNCTION__, iii);
00145                                 cumulativeCount++;
00146                                 cv::Point2f p1, p2, v;
00147                                 
00148                                 p1 = featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-2).featureCoord;
00149                                 p2 = featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord;
00150                                 
00151                                 v.x = (p2.x - p1.x) / (time2 - time1);
00152                                 v.y = (p2.y - p1.y) / (time2 - time1);
00153                                 
00154                                 double s = maxVelocity / pow(pow(v.x, 2.0)+pow(v.y,2.0),0.5);
00155                                 s = min(s,1.0);
00156                                 
00157                                 //printf("%s << entered here...\n", __FUNCTION__);
00158                                 
00159                                 featureTrackVector.at(iii).velocity_x = v.x * s;
00160                                 featureTrackVector.at(iii).velocity_y = v.y * s;
00161                                 
00162                                 cumulativeVelocity += pow(pow(featureTrackVector.at(iii).velocity_x, 2.0)+pow(featureTrackVector.at(iii).velocity_y, 2.0), 0.5);
00163                                 
00164                         }
00165                         
00166                 }
00167                 
00168                 
00169         }
00170         
00171         
00172         if (cumulativeCount > 0) {
00173                 cumulativeVelocity /= double(cumulativeCount);
00174         } else {
00175                 cumulativeVelocity = 0.0;
00176         }
00177         
00178         return cumulativeVelocity;
00179 
00180 }
00181 
00182 void removeObsoleteElements(vector<featureTrack>& featureTrackVector, const vector<unsigned int>& activeFrameIndices) {
00183         
00184         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00185                 
00186                 if (!featureTrackVector.at(iii).occursInSequence(activeFrameIndices)) {
00187                         featureTrackVector.erase(featureTrackVector.begin() + iii);
00188                         iii--;
00189                 } else {
00190                         // Need to go through individual indices and delete them
00191                         for (unsigned int jjj = 0; jjj < featureTrackVector.at(iii).locations.size(); jjj++) {
00192                                 
00193                                 bool isPresent = false;
00194                                 
00195                                 for (unsigned int kkk = 0; kkk < activeFrameIndices.size(); kkk++) {
00196                                         if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == activeFrameIndices.at(kkk)) {
00197                                                 isPresent = true;
00198                                                 break;
00199                                         }
00200                                 }
00201                                 
00202                                 if (!isPresent) {
00203                                         featureTrackVector.at(iii).locations.erase(featureTrackVector.at(iii).locations.begin() + jjj);
00204                                         jjj--;
00205                                 }
00206                                 
00207                         }
00208                         
00209                 }
00210         }
00211 }
00212 
00213 int findTrackPosition(const vector<featureTrack>& featureTrackVector, long int index) {
00214         
00215         if (featureTrackVector.size() == 0) {
00216                 return -1;
00217         }
00218         
00219         int retVal = -1;
00220         
00221         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00222                 if (featureTrackVector.at(iii).trackIndex == index) {
00223                         retVal = int(iii);
00224                         //printf("%s << index = (%d); featureTrackVector.at(%d).trackIndex = (%d), retVal = (%d)\n", __FUNCTION__, index, iii, featureTrackVector.at(iii).trackIndex, retVal);
00225                         break;
00226                 }
00227         }
00228         
00229         return retVal;
00230         
00231 }
00232 
00233 bool featureTrack::occursInSequence(const vector<unsigned int>& indices) {
00234         
00235         for (unsigned int iii = 0; iii < locations.size(); iii++) {
00236                 for (unsigned int jjj = 0; jjj < indices.size(); jjj++) {
00237                         
00238                         if (locations.at(iii).imageIndex == indices.at(jjj)) {
00239                                 return true;
00240                         }
00241                         
00242                 }
00243         }
00244         
00245         return false;
00246 }
00247 
00248 cv::Point2f featureTrack::getCoord(unsigned int cam_idx) {
00249         
00250         cv::Point2f blankPt(-1.0, -1.0);
00251         
00252         for (unsigned int iii = 0; iii < locations.size(); iii++) {
00253                 
00254                 if (((unsigned int) locations.at(iii).imageIndex) == cam_idx) {
00255                         blankPt.x = locations.at(iii).featureCoord.x;
00256                         blankPt.y = locations.at(iii).featureCoord.y;
00257                         
00258                         return blankPt;
00259                 }
00260                 
00261                 
00262         }
00263         
00264         return blankPt;
00265         
00266 }
00267 
00268 cv::Point3d featureTrack::get3dLoc() {
00269         
00270         cv::Point3d loc;
00271         
00272         loc.x = xyzEstimate.x;
00273         loc.y = xyzEstimate.y;
00274         loc.z = xyzEstimate.z;
00275         
00276         return loc;
00277 }
00278 
00279 void assignEstimatesBasedOnVelocities(const vector<featureTrack>& featureTrackVector, const vector<cv::Point2f>& pts1, vector<cv::Point2f>& pts2, unsigned int idx, double time1, double time2) {
00280         
00281         /*
00282         for (unsigned int jjj = 0; jjj < featureTrackVector.size(); jjj++) {
00283                 printf("%s << Track (%d) velocity = (%f, %f)\n", __FUNCTION__, jjj, featureTrackVector.at(jjj).velocity_x, featureTrackVector.at(jjj).velocity_y); 
00284         }
00285         */
00286         
00287         for (unsigned int iii = 0; iii < pts1.size(); iii++) {
00288                 
00289                 cv::Point2f velocity(0.0,0.0); // to find
00290                 
00291                 for (unsigned int jjj = 0; jjj < featureTrackVector.size(); jjj++) {
00292                         if (featureTrackVector.at(jjj).locations.at(featureTrackVector.at(jjj).locations.size()-1).imageIndex == idx) {
00293                                 
00294                                 if (featureTrackVector.at(jjj).locations.at(featureTrackVector.at(jjj).locations.size()-1).featureCoord.x == pts1.at(iii).x) {
00295                                         if (featureTrackVector.at(jjj).locations.at(featureTrackVector.at(jjj).locations.size()-1).featureCoord.y == pts1.at(iii).y) {
00296                                         
00297                                                 velocity.x = featureTrackVector.at(jjj).velocity_x;
00298                                                 velocity.y = featureTrackVector.at(jjj).velocity_y;
00299                                                 
00300                                                 //printf("%s << Found feature (%f, %f) (idx = %d) with velocity (%f, %f).\n", __FUNCTION__, pts1.at(iii).x, pts1.at(iii).y, idx, velocity.x, velocity.y);
00301                                                 // printf("%s << Found feature (%f, %f) (idx = %d) with velocity (%f, %f).\n", __FUNCTION__, pts1.at(iii).x, pts1.at(iii).y, idx, velocity.x, velocity.y);
00302                                                 break;
00303                                         
00304                                         }
00305                                 }
00306                                 
00307                                 
00308                         }
00309                 }
00310                 
00311                 if (abs(time1 - time2) < MAX_TIME_DIFF_FOR_PREDICTION) {
00312                         cv::Point2f newLoc;
00313                 
00314                         newLoc.x = pts1.at(iii).x + (time2-time1)*velocity.x;
00315                         newLoc.y = pts1.at(iii).y + (time2-time1)*velocity.y;
00316                         
00317                         pts2.push_back(newLoc);
00318                 } else {
00319                         pts2.push_back(pts1.at(iii));
00320                 }
00321                 
00322                 
00323                 
00324                 //printf("%s << Predicted location of (%f, %f) is (%f, %f)\n", __FUNCTION__, pts1.at(iii).x, pts1.at(iii).y, pts2.at(iii).x, pts2.at(iii).y);
00325         }
00326 }
00327 
00328 void addProjectionsToVector(vector<featureTrack>& featureTrackVector, unsigned int index, vector<cv::Point2f>& points, long int &starting_track_index, double minSeparation) {
00329 
00330         
00331 
00332         for (unsigned int jjj = 1; jjj < points.size(); jjj++) {
00333                                         
00334                 // Check violation:
00335                 for (unsigned int kkk = 0; kkk < jjj; kkk++) {
00336                         
00337                         // Distance between this point and higher-ranked ones
00338                         double dist = pow(pow(points.at(kkk).x - points.at(jjj).x, 2.0)+pow(points.at(kkk).x - points.at(jjj).x, 2.0),0.5);
00339                         
00340                         if (dist < minSeparation) {
00341                                 points.erase(points.begin()+jjj);
00342                                 jjj--;
00343                                 break;
00344                         }
00345                 }
00346                 
00347         }
00348         
00349         //printf("%s << Adding (%d) points with index (%d)...\n", __FUNCTION__, points.size(), index);
00350         
00351         
00352         for (unsigned int mmm = 0; mmm < points.size(); mmm++) {
00353                 
00354                 addProjectionToVector(featureTrackVector, index, points.at(mmm), starting_track_index);
00355                 
00356         }
00357         
00358         
00359 }
00360 
00361 void addMatchesToVector(vector<featureTrack>& featureTrackVector, unsigned int index1, vector<cv::Point2f>& points1, unsigned int index2, vector<cv::Point2f>& points2, long int &starting_track_index, double minSeparation, bool debug) {     
00362         
00363         if (points1.size() != points2.size()) {
00364                 printf("%s << ERROR! Vector lengths are not equal.\n", __FUNCTION__);
00365                 return;
00366         }
00367         
00368         // Can you even just assume that both are in order of age?? I think that might be how it works...
00369         
00370         if (debug) { printf("%s << Points to add before proximity trimming = (%d)\n", __FUNCTION__, points2.size()); }
00371         
00372         for (unsigned int jjj = 1; jjj < points2.size(); jjj++) {
00373                 
00374                 // So for point (jjj) you have found the 
00375                                                 
00376                 // Check violation:
00377                 for (unsigned int kkk = 0; kkk < jjj; kkk++) {
00378                         
00379                         // Distance between this point and higher-ranked ones
00380                         double dist = pow(pow(points2.at(kkk).x - points2.at(jjj).x, 2.0)+pow(points2.at(kkk).x - points2.at(jjj).x, 2.0),0.5);
00381                         
00382                         if (dist < minSeparation) {
00383                                 points1.erase(points1.begin()+jjj);
00384                                 points2.erase(points2.begin()+jjj);
00385                                 jjj--;
00386                                 break;
00387                         }
00388                 }
00389                 
00390                 /*
00391                 for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00392                         
00393                         
00394                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).imageIndex == index1) {
00395                                 if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord.x == points1.at(jjj).x) {
00396                                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord.y == points1.at(jjj).y) {
00397                                                 //printf("%s << Found feature (%d):(%f,%f) in position (%d) of vector.\n", __FUNCTION__, jjj, points1.at(jjj).x, points1.at(jjj).y, iii);
00398                                                 
00399                                                 
00400                                                 
00401                                         }
00402                                 }
00403                         }
00404                         
00405                 }
00406                 */
00407         }
00408         
00409         if (debug) { printf("%s << Points to add after proximity trimming = (%d)\n", __FUNCTION__, points2.size()); }
00410         
00411         // Determine priority order based on age...
00412         
00413         /*
00414         vector<mypair> pointFirstOccurrences;
00415         
00416         // featureTrackVector tracks are already in order from oldest to newest!!!
00417         
00418         for (unsigned int jjj = 0; jjj < points1.size(); jjj++) {
00419                 for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00420                         
00421                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).imageIndex == index1) {
00422                                 if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord.x == points1.at(jjj).x) {
00423                                         if (featureTrackVector.at(iii).locations.at(featureTrackVector.at(iii).locations.size()-1).featureCoord.y == points1.at(jjj).y) {
00424                                                 printf("%s << Found feature (%d):(%f,%f) in position (%d) of vector.\n", __FUNCTION__, jjj, points1.at(jjj).x, points1.at(jjj).y, iii);
00425                                                 
00426                                                 mypair pair;
00427                                                 pair.first = iii;
00428                                                 pair.second = jjj;
00429                                                 pointFirstOccurrences.push_back(pair);
00430                                                 
00431                                         }
00432                                 }
00433                         }
00434                 }
00435         }
00436         
00437         std::sort (pointFirstOccurrences.begin(), pointFirstOccurrences.end(), comparator);
00438         
00439         for (unsigned int iii = 0; iii < pointFirstOccurrences.size(); iii++) {
00440                 
00441                 
00442                 printf("%s << sorted(%d) = (%d, %d)\n", __FUNCTION__, iii, pointFirstOccurrences.at(iii).first, pointFirstOccurrences.at(iii).second);
00443                 
00444         }
00445         */
00446         
00447         for (unsigned int mmm = 0; mmm < points1.size(); mmm++) {
00448                 
00449                 if (debug) { printf("%s << Adding match from index (%d) to (%d)..\n", __FUNCTION__, index1, index2); }
00450                 
00451                 addMatchToVector(featureTrackVector, index1, points1.at(mmm), index2, points2.at(mmm), starting_track_index);
00452                 
00453         }
00454         
00455         if (debug) { printf("%s << Loop completed.\n", __FUNCTION__); }
00456         
00457         
00458 }
00459 
00460 void clearDangerFeatures(vector<featureTrack>& featureTrackVector, long int index) {
00461         
00462         //long int rev_index = max(((long int)100000000) - max(((long int)2147483648) - index, ((long int) 0)), ((long int) 0));
00463         
00464         long int buffer = 10000000;
00465         
00466         long int bottom_index;
00467         if (index < buffer) {
00468                 bottom_index = 0;
00469         } else {
00470                 bottom_index = index - buffer;
00471         }
00472         
00473         long int top_index;
00474         if ((2147483647 - index) < buffer) {
00475                 top_index = 2147483647;
00476         } else {
00477                 top_index = index + buffer;
00478         }
00479         
00480         //printf("%s << (%d) < (%d) < (%d)\n", __FUNCTION__, bottom_index, index, top_index);
00481         
00482         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00483                 if (featureTrackVector.at(iii).trackIndex > top_index) {
00484                         featureTrackVector.erase(featureTrackVector.begin() + iii);
00485                         iii--;
00486                 } else if (featureTrackVector.at(iii).trackIndex < bottom_index) {
00487                         featureTrackVector.erase(featureTrackVector.begin() + iii);
00488                         iii--;
00489                 }
00490         }
00491         
00492 }
00493 
00494 void addProjectionToVector(vector<featureTrack>& featureTrackVector, unsigned int index, const cv::Point2f& point, long int &starting_track_index) {
00495         
00496         // Go through all of the feature tracks to check if either of these features exist.
00497         
00498         bool foundFeature = false;
00499 
00500         // For each track
00501         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00502                 
00503                 // For each existing feature in the track
00504                 for (unsigned int jjj = 0; jjj < featureTrackVector.at(iii).locations.size(); jjj++) {
00505                         
00506                         // If you've found a feature from the correct image
00507                         if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == index) {
00508                                 if (featureTrackVector.at(iii).locations.at(jjj).featureCoord == point) {
00509                                         foundFeature = true;
00510                                         break;
00511                                 }
00512                         }
00513                         
00514                 }
00515                 
00516                 if (foundFeature) {
00517                         break;
00518                 }
00519                 
00520         }
00521         
00522         if (!foundFeature) {
00523                 
00524                 starting_track_index++; 
00525                 featureTrack newFeatureTrack;
00526                 newFeatureTrack.trackIndex = starting_track_index;
00527                 indexedFeature featToAdd(index, point);
00528                 newFeatureTrack.addFeature(featToAdd);
00529                 newFeatureTrack.velocity_x = 0.0;
00530                 newFeatureTrack.velocity_y = 0.0;
00531                 newFeatureTrack.firstOccurence = index;
00532                 
00533                 featureTrackVector.push_back(newFeatureTrack);
00534         }
00535         
00536 }
00537 
00538 int addMatchToVector(vector<featureTrack>& featureTrackVector, unsigned int index1, const cv::Point2f& point1, unsigned int index2, const cv::Point2f& point2, long int &starting_track_index, const cv::Point2f& velocity, bool debug) {
00539         
00540         int trackIndex = -1;
00541         
00542         // Go through all of the feature tracks to check if either of these features exist.
00543         
00544         bool everFoundFeature1or2 = false;
00545 
00546         // For each track
00547         for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) {
00548                 
00549                 bool foundFeature1 = false, foundFeature2 = false;
00550                 bool foundImage1 = false, foundImage2 = false;
00551                 
00552                 // int foundIndex1 = -1, foundIndex2 = -1;
00553                 
00554                 
00555                 
00556                 // For each existing feature in the track
00557                 for (unsigned int jjj = 0; jjj < featureTrackVector.at(iii).locations.size(); jjj++) {
00558                         
00559                         // If you've found a feature from the correct image
00560                         if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == index1) {
00561                                 foundImage1 = true;
00562                                 if (featureTrackVector.at(iii).locations.at(jjj).featureCoord == point1) {
00563                                         foundFeature1 = true;
00564                                         //foundIndex1 = jjj;
00565                                 }
00566                         }
00567                         
00568                         if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == index2) {
00569                                 foundImage2 = true;
00570                                 if (featureTrackVector.at(iii).locations.at(jjj).featureCoord == point2) {
00571                                         foundFeature2 = true;
00572                                         //foundIndex2 = jjj;
00573                                 }
00574                         }
00575                         
00576                 }
00577                 
00578                 if (foundFeature1 && foundFeature2) {
00579                         everFoundFeature1or2 = true;
00580                 } else if (foundFeature1) {
00581                         everFoundFeature1or2 = true;
00582                         // add feature 2 if no feature from this image in track
00583                         if (!foundImage2) {
00584                                 indexedFeature featToAdd(index2, point2);
00585                                 featureTrackVector.at(iii).addFeature(featToAdd);
00586                         }
00587                 } else if (foundFeature2) {
00588                         everFoundFeature1or2 = true;
00589                         // add feature 1 if no feature from this image in track
00590                         if (!foundImage1) {
00591                                 indexedFeature featToAdd(index1, point1);
00592                                 featureTrackVector.at(iii).addFeature(featToAdd);
00593                         }
00594                         
00595                 }
00596                 
00597                 if (everFoundFeature1or2) {
00598                         if (debug) { printf("%s << Setting track (%d) velocity to (%f, %f)\n", __FUNCTION__, iii, velocity.x, velocity.y); }
00599                         featureTrackVector.at(iii).velocity_x = velocity.x;
00600                         featureTrackVector.at(iii).velocity_y = velocity.y;
00601                         trackIndex = iii;
00602                         break;
00603                 }
00604                 
00605         }
00606         
00607         if (!everFoundFeature1or2) {
00608                 
00609                 
00610                 starting_track_index++;
00611                 // If neither does, create a new track
00612                 featureTrack newFeatureTrack;
00613                 newFeatureTrack.trackIndex = starting_track_index;
00614                 indexedFeature featToAdd1(index1, point1);
00615                 newFeatureTrack.addFeature(featToAdd1);
00616                 indexedFeature featToAdd2(index2, point2);
00617                 newFeatureTrack.addFeature(featToAdd2);
00618                 newFeatureTrack.velocity_x = velocity.x;
00619                 newFeatureTrack.velocity_y = velocity.y;
00620                 newFeatureTrack.firstOccurence = index1;
00621                 featureTrackVector.push_back(newFeatureTrack);
00622                 trackIndex = featureTrackVector.size()-1;
00623                 
00624         }
00625         
00626         return trackIndex;
00627 }
00628 
00629 void drawFeatureTracks(cv::Mat& src, cv::Mat& dst, vector<featureTrack>& tracks, const cv::Scalar& tColor, const cv::Scalar& kColor, unsigned int index, unsigned int history) {
00630         
00631         cv::Point p1, p2;
00632         
00633         src.copyTo(dst);
00634         
00635         for (unsigned int iii = 0; iii < tracks.size(); iii++) {
00636                 
00637                 //printf("%s << DEBUG [%d] : %d\n", __FUNCTION__, iii, 0);
00638                 
00639                 if (tracks.at(iii).locations.size() < 1) {
00640                         continue;
00641                 }
00642                 
00643                 if (tracks.at(iii).locations.at(tracks.at(iii).locations.size()-1).imageIndex == index) {
00644                         p1 = cv::Point(tracks.at(iii).locations.at(tracks.at(iii).locations.size()-1).featureCoord.x * 16.0, tracks.at(iii).locations.at(tracks.at(iii).locations.size()-1).featureCoord.y * 16.0);
00645                         circle(dst, p1, 1, kColor, 2, CV_AA, 4);
00646                 } else {
00647                         continue;
00648                 }
00649                 
00650                 if (tracks.at(iii).locations.size() < 2) {
00651                         continue;
00652                 }
00653                 
00654                 
00655                 for (int jjj = ((int)tracks.at(iii).locations.size())-2; jjj >= 0; jjj--) {
00656                         
00657                         
00658                         if (((int)tracks.at(iii).locations.at(((unsigned int) jjj)).imageIndex) < std::max(((int)index)-((int) history), 0)) {
00659                                 break;
00660                         }
00661                         
00662                         //printf("%s << DEBUG [%d][%d] : [%d][%d]\n", __FUNCTION__, iii, jjj, tracks.at(iii).locations.at(((unsigned int) jjj)).imageIndex, index);
00663                         
00664                         p1 = cv::Point(tracks.at(iii).locations.at(((unsigned int) jjj)+1).featureCoord.x * 16.0, tracks.at(iii).locations.at(((unsigned int) jjj)+1).featureCoord.y * 16.0);
00665                         p2 = cv::Point(tracks.at(iii).locations.at(((unsigned int) jjj)).featureCoord.x * 16.0, tracks.at(iii).locations.at(((unsigned int) jjj)).featureCoord.y * 16.0);
00666         
00667                         cv::line(dst, p1, p2, tColor, 0.5, CV_AA, 4);
00668                 
00669                 }
00670                 
00671         }
00672         
00673         
00674 }
00675 
00676 void redistortTracks(const vector<featureTrack>& src, vector<featureTrack>& dst, const cv::Mat& cameraMatrix, const cv::Mat& distCoeffs, unsigned int latest, const cv::Mat& newCamMat, unsigned int history) {
00677         
00678         dst.clear();
00679         dst.insert(dst.end(), src.begin(), src.end());
00680         
00681         vector<cv::Point2f> tempPoints, newPoints;
00682         
00683         
00684         for (unsigned int iii = 0; iii < src.size(); iii++) {
00685                 
00686                 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex != ((int) latest)) {
00687                         continue;
00688                 }
00689                 
00690                 //printf("%s << continuing...\n", __FUNCTION__);
00691                 
00692                 newPoints.clear();
00693                 tempPoints.clear();
00694                 
00695                 for (int jjj = src.at(iii).locations.size()-1; jjj >= ((int) std::max(((int) src.at(iii).locations.size()) - ((int) history), ((int) 0))); jjj--) {
00696                         
00697                         //printf("%s << A [%d] (%d / %d)\n", __FUNCTION__, iii, jjj, src.at(iii).locations.size());
00698                         
00699                         tempPoints.push_back(src.at(iii).locations.at(jjj).featureCoord);
00700                 } 
00701                 
00702                 redistortPoints(tempPoints, newPoints, cameraMatrix, distCoeffs, newCamMat);
00703                 
00704                 unsigned int currIndex = 0;
00705                 
00706                 for (int jjj = src.at(iii).locations.size()-1; jjj >= ((int) std::max(((int) src.at(iii).locations.size()) - ((int) history), ((int) 0))); jjj--) {
00707                         //printf("%s << B [%d] (%d / %d) [%d]\n", __FUNCTION__, iii, jjj, src.at(iii).locations.size(), history - (src.at(iii).locations.size()-1) + jjj - 1);
00708                         dst.at(iii).locations.at(jjj).featureCoord = newPoints.at(currIndex);
00709                         //printf("%s << changed from (%f, %f) to (%f, %f)\n", __FUNCTION__, tempPoints.at(currIndex).x, tempPoints.at(currIndex).y, newPoints.at(currIndex).x, newPoints.at(currIndex).y);
00710                         currIndex++;
00711                 }
00712                 
00713         }
00714 }
00715 
00716 bool createTrackMatrix(const vector<featureTrack>& src, cv::Mat& dst, int latest) {
00717         
00718         unsigned int rows = 240, cols = 640;
00719         dst = cv::Mat(rows, cols, CV_8UC3);
00720         
00721         for (int iii = 0; iii < dst.rows; iii++) {
00722                 for (int jjj = 0; jjj < dst.cols; jjj++) {
00723                         dst.at<cv::Vec3b>(iii,jjj) = cv::Vec3b(255,255,255);
00724                         // dst.at<cv::Vec3b>(iii,jjj)[1] = 255;
00725                         //dst.at<cv::Vec3b>(iii,jjj)[2] = 255;
00726                 }
00727         }
00728         
00729         // printf("%s << A.\n", __FUNCTION__);
00730         int earliest = -1;
00731         // Determine the latest frame index
00732         if (latest == -1) {
00733                 
00734                 
00735                 
00736                 for (unsigned int iii = 0; iii < src.size(); iii++) {
00737                         
00738                         // printf("%s << iii = (%d / %d) : (%d)\n", __FUNCTION__, iii, src.size(), src.at(iii).locations.size());
00739                         
00740                         if (src.at(iii).locations.size() > 0) {
00741                                 if (((int) src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex) > latest) {
00742                                         // printf("%s << imageIndex = (%d)\n", __FUNCTION__, src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex);
00743                                         latest = src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex;
00744                                 }
00745                                 
00746                                 if ( (earliest == -1) || ((int) src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex) < earliest) {
00747                                         
00748                                         earliest = src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex;
00749                                 }
00750                         }
00751                         
00752                         
00753                 }
00754         }
00755         
00756         printf("%s << earliest/latest = (%d/%d)\n", __FUNCTION__, earliest, latest);
00757         
00758         vector<int> activeTrackIndices;
00759         
00760         // Determine which tracks in the feature track structure were identified in latest frame
00761         for (unsigned int iii = 0; iii < src.size(); iii++) {
00762                 
00763                 if (src.at(iii).locations.size() > 0) {
00764                         if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex == ((int) latest)) {
00765                                 activeTrackIndices.push_back(iii);
00766 
00767                         }
00768                 }
00769                 
00770                 
00771         }
00772 
00773         // printf("%s << activeTrackIndices.size() = (%d)\n", __FUNCTION__, activeTrackIndices.size());
00774 
00775         
00776         // Colorize the pixels in the debug image
00777         for (unsigned int iii = 0; iii < std::min((unsigned int)rows, ((unsigned int) activeTrackIndices.size())); iii++) {
00778                 for (unsigned int jjj = 0; jjj < std::min((unsigned int)cols, ((unsigned int)src.at(activeTrackIndices.at(iii)).locations.size())); jjj++) {
00779                         
00780                         unsigned int column = latest - src.at(activeTrackIndices.at(iii)).locations.at(jjj).imageIndex;
00781                         
00782                         if (column < cols) {
00783                                 dst.at<cv::Vec3b>(iii,column)[1] = 0;
00784                                 dst.at<cv::Vec3b>(iii,column)[2] = 0;                           
00785                         }
00786 
00787                 }
00788                 
00789         }
00790         
00791         return true;
00792         
00793         
00794 }
00795 
00796 void assignHistoricalPoints(const vector<featureTrack>& src, unsigned int idx_1, unsigned int idx_2, vector<cv::Point2f>& dst) {
00797         
00798         //if ((src.size() > 0) && (src.at(0).locations.size() > 0)) { printf("%s << First occurence of oldest track = (%d)\n", __FUNCTION__, src.at(0).locations.at(0).imageIndex); }
00799         
00800         for (unsigned int iii = 0; iii < src.size(); iii++) {
00801                 
00802                 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex == idx_2) {
00803                         continue;
00804                 }
00805                 
00806                 for (unsigned int jjj = 0; jjj < src.at(iii).locations.size(); jjj++) {
00807                         
00808                         //printf("%s << (%d : %d) vs (%d : %d)\n", __FUNCTION__, src.at(iii).locations.at(jjj).imageIndex, idx_1, src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex, idx_2);
00809                         
00810                         if (src.at(iii).locations.at(jjj).imageIndex == idx_1) {
00811                                 dst.push_back(src.at(iii).locations.at(jjj).featureCoord);
00812                         }
00813                 }
00814         }
00815         
00816 }


thermalvis
Author(s): Stephen Vidas
autogenerated on Sun Jan 5 2014 11:38:45