Skip to content

Commit

Permalink
simplified code: removing WebARKitAkazeTracker and
Browse files Browse the repository at this point in the history
WebARKitOrbTracker classes.
Now only WebARKitTracker is necessary.
- fix for issue webarkit/webarkit-testing#11
  • Loading branch information
kalwalt committed May 1, 2023
1 parent 5237407 commit 323b6df
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 272 deletions.

This file was deleted.

This file was deleted.

101 changes: 101 additions & 0 deletions WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,107 @@ namespace webarkit {
WebARKitTracker::WebARKitTracker()
: corners(4), initialized(false), output(17, 0.0), _valid(false), numMatches(0) {}

void WebARKitTracker::initialize(webarkit::TRACKER_TYPE trackerType) {
setDetectorType(trackerType);
}

void WebARKitTracker::setDetectorType(webarkit::TRACKER_TYPE trackerType) {
if(trackerType == webarkit::TRACKER_TYPE::AKAZE_TRACKER) {
this->_featureDetector = cv::AKAZE::create();
} else if (trackerType == webarkit::TRACKER_TYPE::ORB_TRACKER) {
this->_featureDetector = cv::ORB::create(MAX_FEATURES);
}
_matcher = cv::BFMatcher::create();
}

void WebARKitTracker::initTracker(uchar *refData, size_t refCols,
size_t refRows) {
std::cout << "Init Tracker!" << std::endl;
cv::Mat refGray(refRows, refCols, CV_8UC1, refData);

this->_featureDetector->detectAndCompute(refGray, cv::noArray(), refKeyPts, refDescr);

corners[0] = cvPoint(0, 0);
corners[1] = cvPoint(refCols, 0);
corners[2] = cvPoint(refCols, refRows);
corners[3] = cvPoint(0, refRows);

initialized = true;

std::cout << "Tracker ready!" << std::endl;
}

void WebARKitTracker::processFrameData(uchar *frameData, size_t frameCols,
size_t frameRows,
ColorSpace colorSpace) {
cv::Mat grayFrame;
if (colorSpace == ColorSpace::RGBA) {
cv::Mat colorFrame(frameRows, frameCols, CV_8UC4, frameData);
grayFrame.create(frameRows, frameCols, CV_8UC1);
cv::cvtColor(colorFrame, grayFrame, cv::COLOR_RGBA2GRAY);
} else if (colorSpace == ColorSpace::GRAY) {
grayFrame = cv::Mat(frameRows, frameCols, CV_8UC1, frameData);
}
processFrame(grayFrame);
grayFrame.release();
}

void WebARKitTracker::processFrame(cv::Mat &frame) {
if (!this->_valid) {
this->_valid = resetTracking(frame);
} else {
this->_valid = track(frame);
}
}

bool WebARKitTracker::resetTracking(cv::Mat &currIm) {
if (!initialized) {
std::cout << "Reference image not found. AR is unintialized!" << std::endl;
return NULL;
}

std::cout << "Reset Tracking!" << std::endl;

clear_output();

cv::Mat frameDescr;
std::vector<cv::KeyPoint> frameKeyPts;

this->_featureDetector->detectAndCompute(currIm, cv::noArray(), frameKeyPts, frameDescr);

std::vector<std::vector<cv::DMatch>> knnMatches;
_matcher->knnMatch(frameDescr, refDescr, knnMatches, 2);

framePts.clear();
std::vector<cv::Point2f> refPts;

// find the best matches
for (size_t i = 0; i < knnMatches.size(); ++i) {
if (knnMatches[i][0].distance <
GOOD_MATCH_RATIO * knnMatches[i][1].distance) {
framePts.push_back(frameKeyPts[knnMatches[i][0].queryIdx].pt);
refPts.push_back(refKeyPts[knnMatches[i][0].trainIdx].pt);
}
}

bool valid;

if (framePts.size() >= MIN_NUM_MATCHES) {
m_H = cv::findHomography(refPts, framePts, cv::RANSAC);
if ((valid = homographyValid(m_H))) {
numMatches = framePts.size();

if (currIm.empty()) {
std::cout << "prevIm is empty!" << std::endl;
return NULL;
}
currIm.copyTo(prevIm);
}
}

return valid;
}

bool WebARKitTracker::track(cv::Mat &currIm) {
if (!initialized) {
std::cout << "Reference image not found. AR is unintialized!" << std::endl;
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 323b6df

Please sign in to comment.