$search
00001 00005 #include "extrinsics.hpp" 00006 00007 double calculateExtrinsicERE(int nCams, 00008 cv::vector<Point3f>& physicalPoints, 00009 cv::vector< cv::vector< cv::vector<Point2f> > > corners, 00010 Mat *cameraMatrix, 00011 Mat *distCoeffs, 00012 Mat *R, 00013 Mat *T) 00014 { 00015 00016 int ptsPerSet = physicalPoints.size(); 00017 int numFrames = corners.at(0).size(); 00018 00019 Mat *fsRvec, *fsTvec, *esRvec, *esTvec; 00020 fsRvec = new Mat[nCams]; 00021 fsTvec = new Mat[nCams]; 00022 esRvec = new Mat[nCams]; 00023 esTvec = new Mat[nCams]; 00024 00025 cv::vector<Point2f> *estimatedPattern, *projectedPattern; 00026 estimatedPattern = new cv::vector<Point2f>[nCams]; 00027 projectedPattern = new cv::vector<Point2f>[nCams]; 00028 00029 for (int k = 0; k < nCams; k++) 00030 { 00031 estimatedPattern[k].resize(physicalPoints.size()); 00032 projectedPattern[k].resize(physicalPoints.size()); 00033 } 00034 00035 double *errors; 00036 00037 double tSum = 0; //, tMean = 0, tDev = 0; 00038 00039 double xError, yError; 00040 00041 int maxCapacity = numFrames*ptsPerSet*nCams; 00042 00043 errors = new double[maxCapacity]; 00044 00045 Mat physPtsMat = Mat(physicalPoints); 00046 Mat cornersMat; 00047 00048 int index = 0; 00049 00050 for (int i = 0; i < numFrames; i++) 00051 { 00052 00053 for (int k = 0; k < nCams; k++) 00054 { 00055 00056 cornersMat = Mat(corners.at(k).at(i)); 00057 00058 solvePnP(physPtsMat, cornersMat, cameraMatrix[k], distCoeffs[k], fsRvec[k], fsTvec[k], false); 00059 00060 projectPoints(Mat(physicalPoints), fsRvec[k], fsTvec[k], cameraMatrix[k], distCoeffs[k], estimatedPattern[k]); 00061 00062 esRvec[k] = R[k] * fsRvec[0]; 00063 00064 esTvec[k] = fsTvec[0] + T[k]; 00065 00066 projectPoints(Mat(physicalPoints), esRvec[k], esTvec[k], cameraMatrix[k], distCoeffs[k], projectedPattern[k]); 00067 00068 for (unsigned int j = 0; j < estimatedPattern[k].size(); j++) 00069 { 00070 00071 xError = abs(projectedPattern[k].at(j).x - estimatedPattern[k].at(j).x); 00072 yError = abs(projectedPattern[k].at(j).y - estimatedPattern[k].at(j).y); 00073 00074 errors[index] = pow(pow(xError, 2)+pow(yError, 2), 0.5); 00075 00076 tSum += (errors[index] / (double(ptsPerSet) * double(nCams))); 00077 00078 index++; 00079 } 00080 00081 } 00082 } 00083 00084 double err = tSum / double(numFrames); 00085 00086 delete[] fsRvec; 00087 delete[] fsTvec; 00088 delete[] esRvec; 00089 delete[] esTvec; 00090 00091 delete[] estimatedPattern; 00092 delete[] projectedPattern; 00093 00094 delete[] errors; 00095 00096 //printf("%s << Current EMRE = %f\n", __FUNCTION__, err); 00097 00098 return err; 00099 00100 } 00101 00102 double obtainMultisetScore(int nCams, vector<Mat>& distributionMap, vector<Mat>& binMap, vector<vector<double> >& distances, cv::vector<cv::vector<cv::vector<Point2f> > >& corners, int index) 00103 { 00104 double score = 0.0; 00105 double *viewScore; 00106 viewScore = new double[nCams]; 00107 00108 cv::vector<Point> hull; 00109 cv::vector<Point2f> hull2; 00110 double area; 00111 cv::vector<cv::vector<double> > distancesCpy; 00112 00113 //distances.copyTo(distancesCpy); // why the fuck won't this work?? 00114 00115 Point centroid, center; 00116 00117 double *distFromCenter, *proportionOfView; 00118 distFromCenter = new double[nCams]; // delete 00119 proportionOfView = new double[nCams]; 00120 00121 printf("KHAAAN!!! 2\n"); 00122 00123 // for each view 00124 for (int k = 0; k < nCams; k++) 00125 { 00126 printf("K00\n"); 00127 center = Point((distributionMap.at(k).size().width-1)/2, (distributionMap.at(k).size().height-1)/2); 00128 00129 printf("K01\n"); 00130 // obtain a convex hull around the points 00131 convexHull(Mat(corners.at(k).at(index)), hull2); 00132 00133 convertVectorToPoint(hull2, hull); 00134 00135 printf("K02\n"); 00136 // measure the convex hull's distance from the centre of the image 00137 centroid = findCentroid(hull); 00138 distFromCenter[k] = distBetweenPts(centroid, center); 00139 00140 printf("K03\n"); 00141 // measure the convex hull's proportional coverage from the centre of the image 00142 area = contourArea(Mat(hull)); 00143 proportionOfView[k] = area; 00144 00145 // this shit won't work either :P 00146 //distancesCpy.push_back((distributionMap.at(k).size().width*distributionMap.at(k).size().height)/area); 00147 00148 // it hardly adds any points when the pattern is far from the center of a view 00149 viewScore[k] = 1/(distBetweenPts(centroid, center)); 00150 //viewScore[k] = distBetweenPts(centroid, center); 00151 00152 score += viewScore[k]; 00153 } 00154 00155 delete[] viewScore; 00156 00157 delete[] distFromCenter; 00158 delete[] proportionOfView; 00159 00160 // Measure get mean and variance of the distances 00161 // if distances are scored rather than areas, further away points can be emphasized 00162 // figure out a better cost function 00163 00164 // Current configuration works OK, but doesn't utilize distances 00165 00166 return score; 00167 } 00168 00169 void optimizeCalibrationSets(cv::vector<Size> imSize, 00170 int nCams, 00171 Mat *cameraMatrix, 00172 Mat *distCoeffs, 00173 cv::vector<Mat>& distributionMap, 00174 cv::vector< cv::vector< cv::vector<Point2f> > >& candidateCorners, 00175 cv::vector< cv::vector< cv::vector<Point2f> > >& testCorners, 00176 cv::vector<Point3f> row, 00177 int selection, int num, 00178 cv::vector<cv::vector<int> >& tagNames, 00179 cv::vector<cv::vector<int> >& selectedTags) 00180 { 00181 00182 srand ( time(NULL) ); 00183 00184 //printf("%s << Entered.\n", __FUNCTION__); 00185 00186 if (selection == 0) 00187 { 00188 return; 00189 } 00190 00191 char windowName[20]; 00192 00193 int randomNum = 0; 00194 cv::vector<Mat> distributionDisplay(nCams); 00195 cv::vector<Mat> binMap(nCams); 00196 cv::vector<Mat> binTemp(nCams); 00197 cv::vector<cv::vector<double> > distances(nCams); 00198 00199 // Scoring Variables 00200 double score, maxScore = 0.0; 00201 int maxIndex = 0; 00202 //int rankedFrames[MAX_FRAMES_TO_LOAD]; 00203 //double rankedScoreVector[MAX_FRAMES_TO_LOAD]; 00204 double *unrankedScores; //[MAX_FRAMES_TO_LOAD]; 00205 unrankedScores = new double[candidateCorners.at(0).size()]; 00206 00207 vector<int> addedIndices;//[MAX_FRAMES_TO_LOAD]; 00208 double bestScore = 0.0; 00209 int bestIndex; 00210 double err; 00211 00212 // For optimum number of frames 00213 double prevBestScore = 9e99; 00214 int optimumNum = 0; 00215 00216 double testingProbability = 1.00; 00217 00218 cv::vector< cv::vector<Point3f> > objectPoints; 00219 00220 cv::vector< cv::vector< cv::vector<Point2f> > > originalFramesCpy; 00221 cv::vector< cv::vector< cv::vector<Point2f> > > selectedFrames; 00222 cv::vector< cv::vector< cv::vector<Point2f> > > tempFrameTester; 00223 cv::vector< cv::vector< cv::vector<Point2f> > > newCorners; 00224 00225 originalFramesCpy.resize(nCams); 00226 selectedFrames.resize(nCams); 00227 tempFrameTester.resize(nCams); 00228 newCorners.resize(nCams); 00229 00230 //printf("%s << Half variables initialized.\n", __FUNCTION__); 00231 00232 for (int i = 0; i < nCams; i++) 00233 { 00234 00235 imSize.at(i) = distributionMap.at(i).size(); 00236 00237 distributionDisplay.at(i) = Mat(distributionMap.at(i).size(), CV_8UC1); 00238 binMap.at(i) = Mat(30, 40, CV_32SC1); 00239 binTemp.at(i) = Mat(binMap.at(i).size(), CV_8UC1); 00240 binMap.at(i).setTo(0); 00241 00242 00243 originalFramesCpy.at(i).assign(candidateCorners.at(i).begin(), candidateCorners.at(i).end()); // So all corners are preserved for ERE calculation/s 00244 //fullSetCorners.at(i).assign(testCorners.at(i).begin(), testCorners.at(i).end()); // So all corners are preserved for ERE calculation/s 00245 00246 } 00247 00248 Mat E[MAX_CAMS], F[MAX_CAMS], Q; // Between first camera and all other cameras 00249 Mat R[MAX_CAMS], T[MAX_CAMS]; // Rotations/translations between first camera and all other cameras 00250 // Mat R2[MAX_CAMS], T2[MAX_CAMS]; // Rotations/translations between all other cameras 00251 // Mat R_[MAX_CAMS], P_[MAX_CAMS]; 00252 00253 TermCriteria term_crit; 00254 term_crit = TermCriteria(TermCriteria::COUNT+ TermCriteria::EPS, 30, 1e-6); 00255 00256 bool alreadyAdded = false; 00257 00258 //printf("%s << Al variables initialized.\n", __FUNCTION__); 00259 00260 // SEED VARIABLES 00261 int nSeeds = 3; 00262 int nSeedTrials = 50; 00263 00264 int *bestSeedSet, *currentSeedSet; 00265 00266 double bestSeedScore = 9e50, currentSeedScore; 00267 00268 bool alreadyUsed; 00269 00270 //int newRandomNum; 00271 00272 switch (selection) 00273 { 00274 // ================================================== 00275 case SCORE_BASED_OPTIMIZATION_CODE: // SCORE-BASED OPTIMAL FRAME SELECTION 00276 // ================================================== 00277 // Until you've sufficiently filled the newCorners vector 00278 while (newCorners.at(0).size() < (unsigned int)(num)) 00279 { 00280 00281 printf("%s << newCorners.at(0).size() = %d; num = %d\n", __FUNCTION__, newCorners.at(0).size(), num); 00282 00283 maxIndex = 0; 00284 maxScore = 0; 00285 00286 // For each corner set-set 00287 for (unsigned int i = 0; i < candidateCorners.at(0).size(); i++) 00288 { 00289 score = obtainMultisetScore(nCams, distributionMap, binMap, distances, candidateCorners, i); 00290 printf("%s << Frame [%d] score %f\n", __FUNCTION__, i, score); 00291 if (score > maxScore) 00292 { 00293 maxScore = score; 00294 maxIndex = i; 00295 } 00296 else if (score < 0) 00297 { 00298 printf("%s << ERROR. Negative score. Returning.\n", __FUNCTION__); 00299 delete[] unrankedScores; 00300 return; 00301 } 00302 } 00303 00304 printf("%s << Top scoring frame-set #%d gets %f\n", __FUNCTION__, maxIndex, maxScore); 00305 00306 for (int k = 0; k < nCams; k++) 00307 { 00308 00309 //printf("DEBUG Q_001\n"); 00310 newCorners.at(k).push_back(candidateCorners.at(k).at(maxIndex)); // Push highest scorer onto new vector 00311 00312 //printf("DEBUG Q_002\n"); 00313 addToDistributionMap(distributionMap.at(k), newCorners.at(k).at(newCorners.at(k).size()-1)); // update distribution 00314 00315 //printf("DEBUG Q_003\n"); 00316 equalizeHist(distributionMap.at(k), distributionDisplay.at(k)); 00317 00318 //printf("DEBUG Q_004\n"); 00319 sprintf(windowName, "distributionMap-%d", k); 00320 00321 //printf("DEBUG Q_005\n"); 00322 //imshow(windowName, distributionDisplay.at(k)); 00323 00324 //printf("DEBUG Q_006\n"); 00325 //waitKey(5); 00326 00327 //printf("DEBUG Q_007\n"); 00328 addToBinMap(binMap.at(k), newCorners.at(k).at(newCorners.at(k).size()-1), distributionMap.at(k).size()); // update binned mat 00329 00330 //printf("DEBUG Q_008\n"); 00331 convertScaleAbs(binMap.at(k), binTemp.at(k)); 00332 00333 //printf("DEBUG Q_009\n"); 00334 00335 00336 simpleResize(binTemp.at(k), distributionDisplay.at(k), Size(480, 640)); 00337 00338 //printf("DEBUG Q_010\n"); 00339 equalizeHist(distributionDisplay.at(k), distributionDisplay.at(k)); 00340 00341 // why isn't this displaying??? 00342 //sprintf(windowName, "binMap-%d", k); 00343 00344 //printf("DEBUG Q_011\n"); 00345 //imshow(windowName, distributionDisplay.at(k)); 00346 00347 //printf("DEBUG Q_012\n"); 00348 //waitKey(5); 00349 00350 //printf("DEBUG Q_013\n"); 00351 00352 candidateCorners.at(k).erase(candidateCorners.at(k).begin()+maxIndex); // Erase it from original vector 00353 00354 //printf("DEBUG Q_014\n"); 00355 } 00356 00357 //printf("DEBUG Q_999\n"); 00358 //waitKey(40); 00359 00360 } 00361 00362 candidateCorners.clear(); 00363 newCorners.swap(candidateCorners); 00364 break; 00365 // ================================================== 00366 case RANDOM_SET_OPTIMIZATION_CODE: // RANDOM FRAME SELECTION 00367 // ================================================== 00368 00369 for (int i = 0; i < num; i++) 00370 { 00371 randomNum = rand() % candidateCorners.at(0).size(); 00372 00373 for (int k = 0; k < nCams; k++) 00374 { 00375 newCorners.at(k).push_back(candidateCorners.at(k).at(randomNum)); 00376 00377 addToDistributionMap(distributionMap.at(k), newCorners.at(k).at(i)); 00378 equalizeHist(distributionMap.at(k), distributionDisplay.at(k)); 00379 sprintf(windowName, "distributionMap-%d", k); 00380 //imshow(windowName, distributionDisplay.at(k)); 00381 00382 } 00383 00384 //waitKey(40); 00385 00386 } 00387 00388 candidateCorners.clear(); 00389 newCorners.swap(candidateCorners); 00390 00391 break; 00392 // ================================================== 00393 case FIRST_N_PATTERNS_OPTIMIZATION_CODE: // FIRST N FRAMES SELECTION 00394 // ================================================== 00395 while (candidateCorners.at(0).size() > (unsigned int)(num)) 00396 { 00397 for (int k = 0; k < nCams; k++) 00398 { 00399 candidateCorners.at(k).pop_back(); 00400 } 00401 } 00402 00403 for (int i = 0; i < num; i++) 00404 { 00405 00406 for (int k = 0; k < nCams; k++) 00407 { 00408 00409 addToDistributionMap(distributionMap.at(k), candidateCorners.at(k).at(i)); 00410 equalizeHist(distributionMap.at(k), distributionDisplay.at(k)); 00411 sprintf(windowName, "distributionMap-%d", k); 00412 imshow(windowName, distributionDisplay.at(k)); 00413 00414 } 00415 00416 //waitKey(40); 00417 00418 } 00419 00420 delete[] unrankedScores; 00421 00422 return; 00423 // ================================================== 00424 case ENHANCED_MCM_OPTIMIZATION_CODE: // MULTIPLE-TRIAL OPTIMAL FRAME SELECTION 00425 // ================================================== 00426 00427 //printf("%s << Multiple-trial mode\n", __FUNCTION__); 00428 00429 for (int k = 0; k < nCams; k++) 00430 { 00431 selectedFrames.at(k).clear(); 00432 } 00433 00434 prevBestScore = 9e50; 00435 00436 for (int N = 0; N < num; N++) 00437 { 00438 00439 //printf("%s << DEBUG N = %d\n", __FUNCTION__, N); 00440 00441 objectPoints.push_back(row); 00442 00443 for (unsigned int i = 0; i < originalFramesCpy.at(0).size(); i++) 00444 { 00445 00446 //printf("%s << DEBUG i = %d\n", __FUNCTION__, i); 00447 00448 for (int k = 0; k < nCams; k++) 00449 { 00450 //printf("%s << DEBUG tempFrameTester.size() = %d\n", __FUNCTION__, tempFrameTester.size()); 00451 //printf("%s << DEBUG tempFrameTester.at(%d).size() = %d\n", __FUNCTION__, k, tempFrameTester.at(k).size()); 00452 //printf("%s << DEBUG selectedFrames.size() = %d\n", __FUNCTION__, selectedFrames.size()); 00453 //printf("%s << DEBUG selectedFrames.at(%d).size() = %d\n", __FUNCTION__, k, selectedFrames.at(k).size()); 00454 tempFrameTester.at(k).assign(selectedFrames.at(k).begin(), selectedFrames.at(k).end()); 00455 //printf("%s << DEBUG X%d\n", __FUNCTION__, 1); 00456 tempFrameTester.at(k).push_back(originalFramesCpy.at(k).at(i)); 00457 //printf("%s << DEBUG X%d\n", __FUNCTION__, 2); 00458 } 00459 00460 //printf("%s << DEBUG %d\n", __FUNCTION__, 1); 00461 00462 alreadyAdded = false; 00463 00464 // Check if index has already been added 00465 for (unsigned int k = 0; k < addedIndices.size(); k++) 00466 { 00467 if (i == int(addedIndices.at(k))) 00468 { 00469 00470 alreadyAdded = true; 00471 } 00472 } 00473 00474 //printf("%s << DEBUG %d\n", __FUNCTION__, 2); 00475 00476 //printf("%s << DEBUG[i = %d] %d\n", __FUNCTION__, i, 1); 00477 00478 // This is a better way of corrupting scores for previously added points 00479 if (alreadyAdded == true) 00480 { 00481 err = -1.0; 00482 } 00483 else 00484 { 00485 00486 randomNum = rand() % 1000 + 1; // random number between 1 and 1000 (inclusive) 00487 00488 00489 if (randomNum > (1.0 - testingProbability)*1000.0) 00490 { 00491 00492 //printf("%s << randomNum = %d\n", __FUNCTION__, randomNum); 00493 00494 R[0] = Mat::eye(3, 3, CV_64FC1); 00495 T[0] = Mat::zeros(3, 1, CV_64FC1); 00496 00497 for (int k = 0; k < nCams-1; k++) 00498 { 00499 00500 //printf("%s << op.size() = %d, tFT.at(0).size() = %d; tFT.at(%d).size() = %d\n", __FUNCTION__, objectPoints.size(), tempFrameTester.at(0).size(), k+1, tempFrameTester.at(k+1).size()); 00501 //printf("%s << op.at(0).size() = %d, tFT.at(0).at(0).size() = %d; tFT.at(%d).at(0).size() = %d\n", __FUNCTION__, objectPoints.at(0).size(), tempFrameTester.at(0).at(0).size(), k+1, tempFrameTester.at(k+1).at(0).size()); 00502 00503 stereoCalibrate(objectPoints, 00504 tempFrameTester.at(0), tempFrameTester.at(k+1), 00505 cameraMatrix[0], distCoeffs[0], 00506 cameraMatrix[k+1], distCoeffs[k+1], 00507 imSize[0], // hopefully multiple cameras allow multiple image sizes 00508 R[k+1], T[k+1], E[k+1], F[k+1], 00509 term_crit, 00510 EXTRINSICS_FLAGS); // 00511 00512 //printf("%s << Stereo Calibration complete.\n", __FUNCTION__); 00513 00514 00515 00516 00517 } 00518 00519 // Calculate ERE 00520 err = calculateExtrinsicERE(nCams, objectPoints.at(0), testCorners, cameraMatrix, distCoeffs, R, T); 00521 00522 } 00523 else 00524 { 00525 err = -1.0; 00526 } 00527 00528 } 00529 00530 unrankedScores[i] = err; 00531 00532 //printf("%s << DEBUG %d\n", __FUNCTION__, 3); 00533 00534 } 00535 00536 //printf("%s << DEBUG %d\n", __FUNCTION__, 4); 00537 00538 bestScore = 9e50; 00539 bestIndex = 0; 00540 00541 for (unsigned int j = 0; j < originalFramesCpy.at(0).size(); j++) 00542 { 00543 //printf("%s << unrankedScores[%d] = %f\n", __FUNCTION__, j, unrankedScores[j]); 00544 00545 if ((unrankedScores[j] < bestScore) && (unrankedScores[j] > 0.0)) 00546 { 00547 bestScore = unrankedScores[j]; 00548 bestIndex = j; 00549 } 00550 } 00551 00552 //printf("%s << DEBUG %d\n", __FUNCTION__, 5); 00553 00554 unrankedScores[bestIndex] = 9e50; 00555 00556 printf("%s << Best score for %d frameset calibration: %f\n", __FUNCTION__, N+1, bestScore); 00557 00558 for (int k = 0; k < nCams; k++) 00559 { 00560 selectedFrames.at(k).push_back(originalFramesCpy.at(k).at(bestIndex)); 00561 00562 } 00563 00564 //printf("%s << DEBUG %d\n", __FUNCTION__, 6); 00565 00566 addedIndices.push_back(bestIndex); 00567 00568 if (bestScore < prevBestScore) 00569 { 00570 prevBestScore = bestScore; 00571 optimumNum = N; 00572 } 00573 00574 //printf("%s << DEBUG %d\n", __FUNCTION__, 7); 00575 00576 } 00577 00578 //printf("%s << Attempting to delete unrankedScores...\n", __FUNCTION__); 00579 00580 //printf("%s << unrankedScores deleted!.\n", __FUNCTION__); 00581 00582 printf("%s << Optimum number of framesets for calibration = %d\n", __FUNCTION__, optimumNum+1); 00583 00584 for (int k = 0; k < nCams; k++) 00585 { 00586 candidateCorners.at(k).clear(); 00587 candidateCorners.at(k).assign(selectedFrames.at(k).begin(), selectedFrames.at(k).begin() + optimumNum+1); 00588 } 00589 00590 break; 00591 // ================================================== 00592 case RANDOM_SEED_OPTIMIZATION_CODE: // Random N-seed accumulative search 00593 // ================================================== 00594 00595 printf("%s << Initiating Random N-seed accumulative search [seeds = %d; seed trials = %d]\n", __FUNCTION__, nSeeds, nSeedTrials); 00596 00597 for (int k = 0; k < nCams; k++) 00598 { 00599 selectedFrames.at(k).clear(); 00600 } 00601 00602 00603 unrankedScores = new double[originalFramesCpy.at(0).size()]; 00604 00605 prevBestScore = 9e50; 00606 00607 //printf("%s << num = %d\n", __FUNCTION__, num); 00608 00609 00610 00611 bestSeedSet = new int[nSeeds]; 00612 currentSeedSet = new int[nSeeds]; 00613 00614 R[0] = Mat::eye(3, 3, CV_64FC1); 00615 T[0] = Mat::zeros(3, 1, CV_64FC1); 00616 00617 //printf("%s << DEBUG %d\n", __FUNCTION__, 0); 00618 00619 for (int iii = 0; iii < nSeedTrials; iii++) 00620 { 00621 00622 //printf("%s << DEBUG [%d] %d\n", __FUNCTION__, iii, 0); 00623 00624 objectPoints.clear(); 00625 00626 randomNum = rand() % originalFramesCpy.at(0).size(); 00627 00628 currentSeedSet[0] = randomNum; 00629 objectPoints.push_back(row); 00630 00631 //printf("%s << DEBUG [%d] %d\n", __FUNCTION__, iii, 1); 00632 00633 for (int k = 0; k < nCams; k++) 00634 { 00635 tempFrameTester.at(k).clear(); 00636 tempFrameTester.at(k).push_back(originalFramesCpy.at(k).at(currentSeedSet[0])); 00637 } 00638 00639 //printf("%s << DEBUG [%d] %d\n", __FUNCTION__, iii, 2); 00640 00641 for (int jjj = 1; jjj < nSeeds; jjj++) 00642 { 00643 do 00644 { 00645 alreadyUsed = false; 00646 00647 randomNum = rand() % originalFramesCpy.at(0).size(); 00648 00649 for (int kkk = 0; kkk < jjj; kkk++) 00650 { 00651 if (randomNum == currentSeedSet[kkk]) 00652 { 00653 alreadyUsed = true; 00654 } 00655 } 00656 } 00657 while (alreadyUsed); 00658 00659 currentSeedSet[jjj] = randomNum; 00660 00661 objectPoints.push_back(row); 00662 00663 for (int k = 0; k < nCams; k++) 00664 { 00665 tempFrameTester.at(k).push_back(originalFramesCpy.at(k).at(currentSeedSet[jjj])); 00666 } 00667 00668 } 00669 00670 //printf("%s << seed[0] = %d; seed[1] = %d\n", __FUNCTION__, currentSeedSet[0], currentSeedSet[1]); 00671 00672 //printf("%s << DEBUG [%d] %d\n", __FUNCTION__, iii, 3); 00673 00674 //printf("%s << objectPoints.size() = %d\n", __FUNCTION__, objectPoints.size()); 00675 00676 //printf("%s << tempFrameTester.at(%d).size() = %d\n", __FUNCTION__, 0, tempFrameTester.at(0).size()); 00677 00678 for (int k = 0; k < nCams-1; k++) 00679 { 00680 00681 00682 //printf("%s << tempFrameTester.at(%d).size() = %d\n", __FUNCTION__, k+1, tempFrameTester.at(k+1).size()); 00683 00684 stereoCalibrate(objectPoints, 00685 tempFrameTester.at(0), tempFrameTester.at(k+1), 00686 cameraMatrix[0], distCoeffs[0], 00687 cameraMatrix[k+1], distCoeffs[k+1], 00688 imSize[0], // hopefully multiple cameras allow multiple image sizes 00689 R[k+1], T[k+1], E[k+1], F[k+1], 00690 term_crit, 00691 EXTRINSICS_FLAGS); 00692 } 00693 00694 //printf("%s << DEBUG [%d] %d\n", __FUNCTION__, iii, 4); 00695 00696 currentSeedScore = calculateExtrinsicERE(nCams, objectPoints.at(0), testCorners, cameraMatrix, distCoeffs, R, T); 00697 00698 if (currentSeedScore < bestSeedScore) 00699 { 00700 bestSeedScore = currentSeedScore; 00701 00702 printf("%s << Best seed score [trial = %d]: %f\n", __FUNCTION__, iii, bestSeedScore); 00703 00704 for (int jjj = 0; jjj < nSeeds; jjj++) 00705 { 00706 bestSeedSet[jjj] = currentSeedSet[jjj]; 00707 } 00708 00709 } 00710 else 00711 { 00712 //printf("%s << Current seed score [trial = %d]: %f\n", __FUNCTION__, iii, currentSeedScore); 00713 } 00714 00715 } 00716 00717 for (int jjj = 0; jjj < nSeeds; jjj++) 00718 { 00719 00720 for (int k = 0; k < nCams; k++) 00721 { 00722 selectedFrames.at(k).push_back(originalFramesCpy.at(k).at(bestSeedSet[jjj])); 00723 } 00724 00725 unrankedScores[bestSeedSet[jjj]] = 9e50; 00726 00727 // Corrupt seed frames 00728 // Don't think it's necessary - removed. 00729 00730 addedIndices.push_back(bestSeedSet[jjj]); 00731 } 00732 00733 bestScore = bestSeedScore; 00734 00735 // Subtract 1 because later code is dodgy... :P 00736 optimumNum = nSeeds-1; 00737 00738 00739 prevBestScore = 9e50; 00740 00741 for (int N = nSeeds; N < num; N++) 00742 { 00743 00744 //printf("%s << DEBUG N = %d\n", __FUNCTION__, N); 00745 00746 objectPoints.push_back(row); 00747 00748 for (unsigned int i = 0; i < originalFramesCpy.at(0).size(); i++) 00749 { 00750 00751 //printf("%s << DEBUG i = %d\n", __FUNCTION__, i); 00752 00753 for (int k = 0; k < nCams; k++) 00754 { 00755 //printf("%s << DEBUG tempFrameTester.size() = %d\n", __FUNCTION__, tempFrameTester.size()); 00756 //printf("%s << DEBUG tempFrameTester.at(%d).size() = %d\n", __FUNCTION__, k, tempFrameTester.at(k).size()); 00757 //printf("%s << DEBUG selectedFrames.size() = %d\n", __FUNCTION__, selectedFrames.size()); 00758 //printf("%s << DEBUG selectedFrames.at(%d).size() = %d\n", __FUNCTION__, k, selectedFrames.at(k).size()); 00759 tempFrameTester.at(k).assign(selectedFrames.at(k).begin(), selectedFrames.at(k).end()); 00760 //printf("%s << DEBUG X%d\n", __FUNCTION__, 1); 00761 tempFrameTester.at(k).push_back(originalFramesCpy.at(k).at(i)); 00762 //printf("%s << DEBUG X%d\n", __FUNCTION__, 2); 00763 } 00764 00765 //printf("%s << DEBUG %d\n", __FUNCTION__, 1); 00766 00767 alreadyAdded = false; 00768 00769 // Check if index has already been added 00770 for (unsigned int k = 0; k < addedIndices.size(); k++) 00771 { 00772 if (i == int(addedIndices.at(k))) 00773 { 00774 00775 alreadyAdded = true; 00776 } 00777 } 00778 00779 //printf("%s << DEBUG %d\n", __FUNCTION__, 2); 00780 00781 //printf("%s << DEBUG[i = %d] %d\n", __FUNCTION__, i, 1); 00782 00783 // This is a better way of corrupting scores for previously added points 00784 if (alreadyAdded == true) 00785 { 00786 err = -1.0; 00787 } 00788 else 00789 { 00790 00791 randomNum = rand() % 1000 + 1; // random number between 1 and 1000 (inclusive) 00792 00793 00794 if (randomNum > (1.0 - testingProbability)*1000.0) 00795 { 00796 00797 //printf("%s << randomNum = %d\n", __FUNCTION__, randomNum); 00798 00799 00800 00801 for (int k = 0; k < nCams-1; k++) 00802 { 00803 00804 //printf("%s << op.size() = %d, tFT.at(0).size() = %d; tFT.at(%d).size() = %d\n", __FUNCTION__, objectPoints.size(), tempFrameTester.at(0).size(), k+1, tempFrameTester.at(k+1).size()); 00805 //printf("%s << op.at(0).size() = %d, tFT.at(0).at(0).size() = %d; tFT.at(%d).at(0).size() = %d\n", __FUNCTION__, objectPoints.at(0).size(), tempFrameTester.at(0).at(0).size(), k+1, tempFrameTester.at(k+1).at(0).size()); 00806 00807 stereoCalibrate(objectPoints, 00808 tempFrameTester.at(0), tempFrameTester.at(k+1), 00809 cameraMatrix[0], distCoeffs[0], 00810 cameraMatrix[k+1], distCoeffs[k+1], 00811 imSize[0], // hopefully multiple cameras allow multiple image sizes 00812 R[k+1], T[k+1], E[k+1], F[k+1], 00813 term_crit, 00814 EXTRINSICS_FLAGS); // 00815 00816 //printf("%s << Stereo Calibration complete.\n", __FUNCTION__); 00817 00818 00819 00820 00821 } 00822 00823 // Calculate ERE 00824 err = calculateExtrinsicERE(nCams, objectPoints.at(0), testCorners, cameraMatrix, distCoeffs, R, T); 00825 00826 } 00827 else 00828 { 00829 err = -1.0; 00830 } 00831 00832 } 00833 00834 unrankedScores[i] = err; 00835 00836 //printf("%s << DEBUG %d\n", __FUNCTION__, 3); 00837 00838 } 00839 00840 //printf("%s << DEBUG %d\n", __FUNCTION__, 4); 00841 00842 bestScore = 9e50; 00843 bestIndex = 0; 00844 00845 for (unsigned int j = 0; j < originalFramesCpy.at(0).size(); j++) 00846 { 00847 //printf("%s << unrankedScores[%d] = %f\n", __FUNCTION__, j, unrankedScores[j]); 00848 00849 if ((unrankedScores[j] < bestScore) && (unrankedScores[j] > 0.0)) 00850 { 00851 bestScore = unrankedScores[j]; 00852 bestIndex = j; 00853 } 00854 } 00855 00856 //printf("%s << DEBUG %d\n", __FUNCTION__, 5); 00857 00858 unrankedScores[bestIndex] = 9e50; 00859 00860 printf("%s << Best score for %d frameset calibration: %f\n", __FUNCTION__, N+1, bestScore); 00861 00862 for (int k = 0; k < nCams; k++) 00863 { 00864 selectedFrames.at(k).push_back(originalFramesCpy.at(k).at(bestIndex)); 00865 00866 } 00867 00868 //printf("%s << DEBUG %d\n", __FUNCTION__, 6); 00869 00870 addedIndices.push_back(bestIndex); 00871 00872 if (bestScore < prevBestScore) 00873 { 00874 prevBestScore = bestScore; 00875 optimumNum = N; 00876 } 00877 00878 //printf("%s << DEBUG %d\n", __FUNCTION__, 7); 00879 00880 } 00881 00882 //printf("%s << Attempting to delete unrankedScores...\n", __FUNCTION__); 00883 00884 //printf("%s << unrankedScores deleted!.\n", __FUNCTION__); 00885 00886 printf("%s << Optimum number of framesets for calibration = %d\n", __FUNCTION__, optimumNum+1); 00887 00888 for (int k = 0; k < nCams; k++) 00889 { 00890 candidateCorners.at(k).clear(); 00891 candidateCorners.at(k).assign(selectedFrames.at(k).begin(), selectedFrames.at(k).begin() + optimumNum+1); 00892 } 00893 00894 break; 00895 default: 00896 delete[] unrankedScores; 00897 return; 00898 } 00899 00900 //printf("%s << Trying to delete unrankedScores x3...\n", __FUNCTION__); 00901 delete[] unrankedScores; 00902 //printf("%s << Deleted!\n", __FUNCTION__); 00903 }