# HG changeset patch # User Nicolas Saunier # Date 1499724281 14400 # Node ID dbd81710d515cf2137cb55bf79a515030fd8b56c # Parent 974077e23804636ab90801a51526efaffa944da4 new feature tracking in image space with point undistortion diff -r 974077e23804 -r dbd81710d515 c/feature-based-tracking.cpp --- a/c/feature-based-tracking.cpp Mon Jul 10 01:38:12 2017 -0400 +++ b/c/feature-based-tracking.cpp Mon Jul 10 18:04:41 2017 -0400 @@ -114,10 +114,6 @@ Mat intrinsicCameraMatrix, newIntrinsicCameraMatrix; if (params.undistort) { intrinsicCameraMatrix = ::loadMat(params.intrinsicCameraFilename, " "); - //videoSize = Size(static_cast(round(videoSize.width*params.undistortedImageMultiplication)), static_cast(round(videoSize.height*params.undistortedImageMultiplication))); - // newIntrinsicCameraMatrix = intrinsicCameraMatrix.clone(); - // newIntrinsicCameraMatrix.at(0,2) = undistortedVideoSize.width/2.; - // newIntrinsicCameraMatrix.at(1,2) = undistortedVideoSize.height/2.; Size undistortedVideoSize = Size(static_cast(round(videoSize.width*params.undistortedImageMultiplication)), static_cast(round(videoSize.height*params.undistortedImageMultiplication))); newIntrinsicCameraMatrix = getDefaultNewCameraMatrix(intrinsicCameraMatrix, undistortedVideoSize, true);//getOptimalNewCameraMatrix(intrinsicCameraMatrix, params.distortionCoefficients, videoSize, 1, undistortedVideoSize);//, 0, true); initUndistortRectifyMap(intrinsicCameraMatrix, params.distortionCoefficients, Mat::eye(3,3, CV_32FC1) /* 0 ?*/, newIntrinsicCameraMatrix, undistortedVideoSize, CV_32FC1, map1, map2); @@ -133,14 +129,12 @@ } std::shared_ptr > trajectoryDB = std::shared_ptr >(new TrajectoryDBAccessList()); - //TrajectoryDBAccess* trajectoryDB = new TrajectoryDBAccessBlob(); trajectoryDB->connect(params.databaseFilename.c_str()); trajectoryDB->createTable("positions"); trajectoryDB->createTable("velocities"); trajectoryDB->beginTransaction(); - std::vector prevKpts, currKpts; - std::vector prevPts, currPts, newPts, undistortedPts; + std::vector prevPts, currPts, newPts, undistortedPts; // all points but undistortedPts are in image space std::vector status; std::vector errors; Mat prevDesc, currDesc; @@ -164,16 +158,6 @@ break; } else if (frameNum%50 ==0) cout << "frame " << frameNum << endl; - - // if (params.undistort) { - // remap(frame, undistortedFrame, map1, map2, interpolationMethod, BORDER_CONSTANT, 0.); - // frame = undistortedFrame; - - // if (frame.size() != videoSize) { - // cout << "Different frame size " << frameNum << ", breaking ([" << frame.size().width << "x" << frame.size().height << "])" << endl; - // break; - // } - // } cvtColor(frame, currentFrameBW, CV_RGB2GRAY); @@ -182,10 +166,10 @@ calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(static_cast(TermCriteria::COUNT)+static_cast(TermCriteria::EPS) /* = 3 */, params.maxNumberTrackingIterations, params.minTrackingError), /* int flags = */ 0, params.minFeatureEigThreshold); /// \todo try calcOpticalFlowFarneback - if (params.undistort) { + if (params.undistort) undistortPoints(currPts, undistortedPts, intrinsicCameraMatrix, params.distortionCoefficients, noArray(), newIntrinsicCameraMatrix); - //currPts = undistortedPts; - } + else + undistortedPts =currPts; std::vector trackedPts; std::vector::iterator iter = featurePointMatches.begin(); @@ -197,11 +181,8 @@ if ((status[iter->pointNum] =! 0) && (currPtX >= 0) && (currPtX < videoSize.width) && (currPtY >= 0) && (currPtY < videoSize.height) && - (mask.at(currPtY, currPtX) != 0)) { // todo check point in mask in image space - if (params.undistort) - iter->feature->addPoint(frameNum, undistortedPts[iter->pointNum], homography); - else - iter->feature->addPoint(frameNum, currPts[iter->pointNum], homography); + (mask.at(currPtY, currPtX) != 0)) { + iter->feature->addPoint(frameNum, undistortedPts[iter->pointNum], homography); deleteFeature = iter->feature->isDisplacementSmall(params.nDisplacements, minTotalFeatureDisplacement) || !iter->feature->isMotionSmooth(params.accelerationBound, params.deviationBound); @@ -246,21 +227,17 @@ for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i(i,j)=0; goodFeaturesToTrack(currentFrameBW, newPts, params.maxNFeatures, params.featureQuality, params.minFeatureDistanceKLT, featureMask, params.blockSize, params.useHarrisDetector, params.k); - if (params.undistort) { + if (params.undistort) undistortPoints(newPts, undistortedPts, intrinsicCameraMatrix, params.distortionCoefficients, noArray(), newIntrinsicCameraMatrix); - //newPts = undistortedPts; - } - //BOOST_FOREACH(Point2f p, newPts) { + else + undistortedPts = newPts; + for (unsigned int i=0; i