$search
00001 00005 #include "tracks.hpp" 00006 00007 featureTrack::featureTrack() { 00008 isTriangulated = false; 00009 } 00010 00011 void featureTrack::set3dLoc(const 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 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 Point2f featureTrack::getCoord(unsigned int cam_idx) { 00034 00035 Point2f blankPt; 00036 00037 for (unsigned int iii = 0; iii < locations.size(); iii++) { 00038 00039 if (((unsigned int) locations.at(iii).imageIndex) == cam_idx) { 00040 blankPt.x = locations.at(iii).featureCoord.x; 00041 blankPt.y = locations.at(iii).featureCoord.y; 00042 00043 return blankPt; 00044 } 00045 00046 00047 } 00048 00049 } 00050 00051 Point3d featureTrack::get3dLoc() { 00052 00053 Point3d loc; 00054 00055 loc.x = xyzEstimate.x; 00056 loc.y = xyzEstimate.y; 00057 loc.z = xyzEstimate.z; 00058 00059 return loc; 00060 } 00061 00062 void addMatchToVector(vector<featureTrack>& featureTrackVector, int index1, const Point2f& point1, int index2, const Point2f& point2) { 00063 00064 // Go through all of the feature tracks to check if either of these features exist. 00065 00066 bool everFoundFeature1or2 = false; 00067 00068 // For each track 00069 for (unsigned int iii = 0; iii < featureTrackVector.size(); iii++) { 00070 00071 bool foundFeature1 = false, foundFeature2 = false; 00072 bool foundImage1 = false, foundImage2 = false; 00073 00074 // int foundIndex1 = -1, foundIndex2 = -1; 00075 00076 00077 00078 // For each existing feature in the track 00079 for (unsigned int jjj = 0; jjj < featureTrackVector.at(iii).locations.size(); jjj++) { 00080 00081 // If you've found a feature from the correct image 00082 if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == index1) { 00083 foundImage1 = true; 00084 if (featureTrackVector.at(iii).locations.at(jjj).featureCoord == point1) { 00085 foundFeature1 = true; 00086 //foundIndex1 = jjj; 00087 } 00088 } 00089 00090 if (featureTrackVector.at(iii).locations.at(jjj).imageIndex == index2) { 00091 foundImage2 = true; 00092 if (featureTrackVector.at(iii).locations.at(jjj).featureCoord == point2) { 00093 foundFeature2 = true; 00094 //foundIndex2 = jjj; 00095 } 00096 } 00097 00098 } 00099 00100 if (foundFeature1 && foundFeature2) { 00101 everFoundFeature1or2 = true; 00102 } else if (foundFeature1) { 00103 everFoundFeature1or2 = true; 00104 // add feature 2 if no feature from this image in track 00105 if (!foundImage2) { 00106 indexedFeature featToAdd(index2, point2); 00107 featureTrackVector.at(iii).addFeature(featToAdd); 00108 } 00109 } else if (foundFeature2) { 00110 everFoundFeature1or2 = true; 00111 // add feature 1 if no feature from this image in track 00112 if (!foundImage1) { 00113 indexedFeature featToAdd(index1, point1); 00114 featureTrackVector.at(iii).addFeature(featToAdd); 00115 } 00116 00117 } 00118 00119 } 00120 00121 if (!everFoundFeature1or2) { 00122 // If neither does, create a new track 00123 featureTrack newFeatureTrack; 00124 indexedFeature featToAdd1(index1, point1); 00125 newFeatureTrack.addFeature(featToAdd1); 00126 indexedFeature featToAdd2(index2, point2); 00127 newFeatureTrack.addFeature(featToAdd2); 00128 featureTrackVector.push_back(newFeatureTrack); 00129 } 00130 00131 00132 } 00133 00134 void drawFeatureTracks(Mat& src, Mat& dst, vector<featureTrack>& tracks, const Scalar& tColor, const Scalar& kColor, int index, unsigned int history) { 00135 00136 cv::Point p1, p2; 00137 00138 src.copyTo(dst); 00139 00140 00141 00142 for (unsigned int iii = 0; iii < tracks.size(); iii++) { 00143 00144 //printf("%s << DEBUG [%d] : %d\n", __FUNCTION__, iii, 0); 00145 00146 bool trackFound = false; 00147 int latestIndex = 0; 00148 00149 for (unsigned int jjj = tracks.at(iii).locations.size()-1; jjj > ((unsigned int) std::max((latestIndex-((int) history)), 0)); jjj--) { 00150 00151 //printf("%s << DEBUG [%d][%d] : %d\n", __FUNCTION__, iii, jjj, 0); 00152 00153 if (trackFound) { 00154 // Draw line between points... 00155 00156 p1 = cv::Point(tracks.at(iii).locations.at(jjj+1).featureCoord.x * 16.0, tracks.at(iii).locations.at(jjj+1).featureCoord.y * 16.0); 00157 p2 = cv::Point(tracks.at(iii).locations.at(jjj).featureCoord.x * 16.0, tracks.at(iii).locations.at(jjj).featureCoord.y * 16.0); 00158 00159 cv::line(dst, p1, p2, tColor, 0.5, CV_AA, 4); 00160 00161 } else { 00162 latestIndex = jjj; 00163 } 00164 00165 //printf("%s << DEBUG [%d][%d] : %d\n", __FUNCTION__, iii, jjj, 1); 00166 00167 if (tracks.at(iii).locations.at(jjj).imageIndex == index) { 00168 trackFound = true; 00169 p1 = cv::Point(tracks.at(iii).locations.at(jjj).featureCoord.x * 16.0, tracks.at(iii).locations.at(jjj).featureCoord.y * 16.0); 00170 circle(dst, p1, 1, kColor, 2, CV_AA, 4); 00171 } 00172 00173 //printf("%s << DEBUG [%d][%d] : %d\n", __FUNCTION__, iii, jjj, 2); 00174 00175 } 00176 00177 } 00178 00179 00180 } 00181 00182 void redistortTracks(const vector<featureTrack>& src, vector<featureTrack>& dst, const Mat& cameraMatrix, const Mat& distCoeffs, unsigned int latest, const Mat& newCamMat, unsigned int history) { 00183 00184 dst.clear(); 00185 dst.insert(dst.end(), src.begin(), src.end()); 00186 00187 vector<Point2f> tempPoints, newPoints; 00188 00189 00190 for (unsigned int iii = 0; iii < src.size(); iii++) { 00191 00192 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex != ((int) latest)) { 00193 continue; 00194 } 00195 00196 //printf("%s << continuing...\n", __FUNCTION__); 00197 00198 newPoints.clear(); 00199 tempPoints.clear(); 00200 00201 for (int jjj = src.at(iii).locations.size()-1; jjj >= ((int) std::max(((int) src.at(iii).locations.size()) - ((int) history), ((int) 0))); jjj--) { 00202 00203 //printf("%s << A [%d] (%d / %d)\n", __FUNCTION__, iii, jjj, src.at(iii).locations.size()); 00204 00205 tempPoints.push_back(src.at(iii).locations.at(jjj).featureCoord); 00206 } 00207 00208 redistortPoints(tempPoints, newPoints, cameraMatrix, distCoeffs, newCamMat); 00209 00210 unsigned int currIndex = 0; 00211 00212 for (int jjj = src.at(iii).locations.size()-1; jjj >= ((int) std::max(((int) src.at(iii).locations.size()) - ((int) history), ((int) 0))); jjj--) { 00213 //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); 00214 dst.at(iii).locations.at(jjj).featureCoord = newPoints.at(currIndex); 00215 //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); 00216 currIndex++; 00217 } 00218 00219 } 00220 } 00221 00222 bool createTrackMatrix(const vector<featureTrack>& src, Mat& dst, int latest) { 00223 00224 unsigned int rows = 240, cols = 640; 00225 dst = Mat(rows, cols, CV_8UC3); 00226 00227 // printf("%s << A.\n", __FUNCTION__); 00228 00229 if (latest == -1) { 00230 for (unsigned int iii = 0; iii < src.size(); iii++) { 00231 00232 if (src.at(iii).locations.size() > 0) { 00233 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex > latest) { 00234 latest = src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex; 00235 } 00236 } 00237 00238 00239 } 00240 } 00241 00242 // printf("%s << B.\n", __FUNCTION__); 00243 00244 vector<int> activeTrackIndices; 00245 00246 for (unsigned int iii = 0; iii < src.size(); iii++) { 00247 00248 if (src.at(iii).locations.size() > 0) { 00249 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex == ((int) latest)) { 00250 activeTrackIndices.push_back(iii); 00251 00252 } 00253 } 00254 00255 00256 } 00257 00258 for (unsigned int iii = 0; iii < dst.rows; iii++) { 00259 for (unsigned int jjj = 0; jjj < dst.cols; jjj++) { 00260 dst.at<Vec3b>(iii,jjj)[0] = 255; 00261 dst.at<Vec3b>(iii,jjj)[1] = 255; 00262 dst.at<Vec3b>(iii,jjj)[2] = 255; 00263 } 00264 } 00265 00266 for (unsigned int iii = 0; iii < std::min((int)rows, ((int) activeTrackIndices.size())); iii++) { 00267 00268 for (unsigned int jjj = 0; jjj < std::min((int)cols, ((int)src.at(activeTrackIndices.at(iii)).locations.size())); jjj++) { 00269 00270 unsigned int column = latest - src.at(activeTrackIndices.at(iii)).locations.at(jjj).imageIndex; 00271 00272 if (column < cols) { 00273 dst.at<Vec3b>(iii,column)[1] = 0; 00274 dst.at<Vec3b>(iii,column)[2] = 0; 00275 } 00276 00277 } 00278 00279 } 00280 00281 return true; 00282 00283 00284 } 00285 00286 void assignHistoricalPoints(const vector<featureTrack>& src, unsigned int idx_1, unsigned int idx_2, vector<Point2f>& dst) { 00287 00288 for (unsigned int iii = 0; iii < src.size(); iii++) { 00289 00290 bool oldFound = false; 00291 00292 for (unsigned int jjj = 0; jjj < src.at(iii).locations.size(); jjj++) { 00293 if (src.at(iii).locations.at(jjj).imageIndex == idx_1) { 00294 if (src.at(iii).locations.at(src.at(iii).locations.size()-1).imageIndex != idx_2) { 00295 dst.push_back(src.at(iii).locations.at(jjj).featureCoord); 00296 } 00297 } 00298 } 00299 } 00300 00301 }