Skip to content

Commit

Permalink
fix for template matching
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Jun 4, 2024
1 parent 393533e commit 64ff718
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern const int markerTemplateWidth = 15; ///< Width in pixels of image patches
extern const int maxLevel = 3; ///< Maximum number of levels in optical flow image pyramid.
extern const cv::Size winSize(31, 31);
extern const cv::TermCriteria termcrit(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 20, 0.03);
extern const int searchRadius = 15;
extern const int match_method = cv::TM_SQDIFF_NORMED;
extern const double featureDetectPyramidLevel =
1.05f; ///> Scale factor applied to image pyramid to determine image to perform feature matching upon.
extern const int featureBorder = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
TrackerVisualization _trackViz;

WebARKitTrackerImpl()
: corners(4), initialized(false), output(17, 0.0), _valid(false), _isDetected(false), _isTracking(false),
: corners(4), initialized(false), output(17, 0.0), _valid(false), _currentlyTrackedMarkers(0), _frameCount(0), _isDetected(false), _isTracking(false),
numMatches(0), minNumMatches(MIN_NUM_MATCHES), _nn_match_ratio(0.7f),_trackVizActive(false), _trackViz(TrackerVisualization()) {
m_camMatrix = cv::Matx33d::zeros();
m_distortionCoeff = cv::Mat::zeros(4, 1, cv::DataType<double>::type);
Expand Down Expand Up @@ -56,6 +56,7 @@ class WebARKitTracker::WebARKitTrackerImpl {

_pyramid.clear();
_prevPyramid.clear();
_currentlyTrackedMarkers = 0;
}

template <typename T> void initTracker(T refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
Expand Down Expand Up @@ -165,8 +166,6 @@ class WebARKitTracker::WebARKitTrackerImpl {

_isDetected = false;

cv::Mat _image;

cv::Mat frameDescr;
std::vector<cv::KeyPoint> frameKeyPts;
bool valid;
Expand Down Expand Up @@ -412,9 +411,9 @@ class WebARKitTracker::WebARKitTrackerImpl {
if (_trackVizActive) _trackViz.templateMatching.failedBoundsTestCount++;
}
}
bool gotHomography = UpdateTrackableHomography(trackableId, finalTemplatePoints, finalTemplateMatchPoints);
bool gotHomography = updateTrackableHomography(trackableId, finalTemplatePoints, finalTemplateMatchPoints);
if (!gotHomography) {
//_trackables[trackableId]._isTracking = false;
// _trackables[trackableId]._isTracking = false;
// _trackables[trackableId]._isDetected = false;
_isTracking = false;
_isDetected = false;
Expand Down Expand Up @@ -488,7 +487,8 @@ class WebARKitTracker::WebARKitTrackerImpl {
int i = 0;
if (_isDetected) {
WEBARKIT_LOGi("Start tracking!\n");
if (_prevPyramid.size() > 0) {
if (_frameCount > 0 && _prevPyramid.size() > 0) {
//if (_prevPyramid.size() > 0) {
// std::cout << "Starting Optical Flow" << std::endl;
//std::vector<cv::Point2f> warpedPoints;
// perspectiveTransform(framePts, warpedPoints, m_H);
Expand Down Expand Up @@ -523,6 +523,7 @@ class WebARKitTracker::WebARKitTrackerImpl {

WEBARKIT_LOG("Marker detected : %s\n", _isDetected ? "true" : "false");
swapImagePyramid();
_frameCount++;
}

bool homographyValid(cv::Mat& H) {
Expand Down Expand Up @@ -653,7 +654,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
_trackViz.bounds[i][1] = _bBoxTransformed[i].y;
}
}
//_currentlyTrackedMarkers++;
_currentlyTrackedMarkers++;
}
}
}
Expand Down Expand Up @@ -692,7 +693,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
if (!updateTrackableHomography(trackableId, filteredTrackablePoints, filteredTrackedPoints)) {
_isDetected = false;
_isTracking = false;
//_currentlyTrackedMarkers--;
_currentlyTrackedMarkers--;
return false;
}

Expand Down Expand Up @@ -871,6 +872,12 @@ class WebARKitTracker::WebARKitTrackerImpl {
return warpedPoints;
}

cv::Mat _image;

int _currentlyTrackedMarkers;

int _frameCount;

bool _valid;

bool _isDetected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern const int markerTemplateWidth; ///< Width in pixels of image patches used
extern const int maxLevel; ///< Maximum number of levels in optical flow image pyramid.
extern const cv::Size winSize;
extern const cv::TermCriteria termcrit;
extern const int searchRadius;
extern const int match_method;
extern const double featureDetectPyramidLevel; ///> Scale factor applied to image pyramid to determine image to perform feature matching upon.
extern const int featureBorder;
extern const cv::Size blurSize;
Expand Down

0 comments on commit 64ff718

Please sign in to comment.