Skip to content

Commit

Permalink
new getHomograpyInliers function and WebARKitHomographyInfo class
Browse files Browse the repository at this point in the history
- issue at line 246 in the track function
- debug compilation to get more infos
  • Loading branch information
kalwalt committed May 28, 2024
1 parent 0531dca commit f5a0e89
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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;
extern const cv::Size blurSize(3, 3);
extern const double ransac_thresh = 2.5f;
extern const double m_pi = 3.14159265358979323846;
extern const std::string WEBARKIT_HEADER_VERSION_STRING = "1.0.0";
/*@
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitHomographyInfo.h>

namespace webarkit {
namespace homography {

WebARKitHomographyInfo::WebARKitHomographyInfo() { validHomography = false; }

WebARKitHomographyInfo::WebARKitHomographyInfo(cv::Mat hom, std::vector<uchar> newStatus,
std::vector<cv::DMatch> matches) {
homography = hom;
status = newStatus;
inlier_matches = matches;
if (matches.size() > 4) {
validHomography = true;
}
}

} // namespace homography

} // namespace webarkit
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitHomographyInfo.h>

namespace webarkit {

Expand Down Expand Up @@ -170,10 +171,15 @@ class WebARKitTracker::WebARKitTrackerImpl {
WEBARKIT_LOG("Num Matches: %d\n", numMatches);

if (numMatches >= minNumMatches) {
m_H = cv::findHomography(refPts, framePts, cv::RANSAC);
if ((valid = homographyValid(m_H))) {
//m_H = cv::findHomography(refPts, framePts, cv::RANSAC);
webarkit::homography::WebARKitHomographyInfo homographyInfo = getHomographyInliers(refPts, framePts);
valid = homographyInfo.validHomography;

//if ((valid = homographyValid(m_H))) {
if (valid) {
m_H = homographyInfo.homography;
_isDetected = true;
perspectiveTransform(_bBox, _bBoxTransformed, m_H);
perspectiveTransform(_bBox, _bBoxTransformed, homographyInfo.homography);
}
}
}
Expand Down Expand Up @@ -237,7 +243,10 @@ class WebARKitTracker::WebARKitTrackerImpl {
transform.push_back(row);

// update homography matrix
m_H = transform * m_H;
if(!m_H.empty()) {
m_H = transform * m_H;
}
//m_H = transform * m_H;

// set old points to new points
framePts = goodPtsCurr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern const cv::TermCriteria termcrit;
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;
extern const double ransac_thresh;
extern const double m_pi;
extern const std::string WEBARKIT_HEADER_VERSION_STRING;
extern const int WEBARKIT_HEADER_VERSION_MAJOR;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef WEBARKIT_HOMOGRAPHY_INFO_H
#define WEBARKIT_HOMOGRAPHY_INFO_H
#include "WebARKitConfig.h"

namespace webarkit {
namespace homography {

class WebARKitHomographyInfo {
public:
WebARKitHomographyInfo();

WebARKitHomographyInfo(cv::Mat hom, std::vector<uchar> newStatus, std::vector<cv::DMatch> matches);

bool validHomography;
cv::Mat homography;
std::vector<uchar> status;
std::vector<cv::DMatch> inlier_matches;
};

} // namespace homography

} // namespace webarkit

#endif
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
#ifndef WEBARKIT_UTILS_H
#define WEBARKIT_UTILS_H

#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.h>
//#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitEnums.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitHomographyInfo.h>
#include <iostream>

namespace webarkit {

/// Method for calculating and validating a homography matrix from a set of corresponding points.
/// pts1 and pts must have the same dimensionality.
/// @returns An WebARKitHomographyInfo instance, with its status vector of the same dimensionality as the pts1 and pts2 vectors.
static homography::WebARKitHomographyInfo getHomographyInliers(std::vector<cv::Point2f> pts1, std::vector<cv::Point2f> pts2)
{
if (pts1.size() < 4) {
return homography::WebARKitHomographyInfo();
}

cv::Mat inlier_mask, homography;
homography = findHomography(pts1, pts2, cv::RANSAC, ransac_thresh, inlier_mask);
if (homography.empty()) {
// Failed to find a homography.
return homography::WebARKitHomographyInfo();
}

const double det = homography.at<double>(0, 0) * homography.at<double>(1, 1) - homography.at<double>(1, 0) * homography.at<double>(0, 1);
if (det < 0) {
return homography::WebARKitHomographyInfo();
}

const double N1 = sqrt(homography.at<double>(0, 0) * homography.at<double>(0, 0) + homography.at<double>(1, 0) * homography.at<double>(1, 0));
if (N1 > 4 || N1 < 0.1) {
return homography::WebARKitHomographyInfo();
}

const double N2 = sqrt(homography.at<double>(0, 1) * homography.at<double>(0, 1) + homography.at<double>(1, 1) * homography.at<double>(1, 1));
if (N2 > 4 || N2 < 0.1) {
return homography::WebARKitHomographyInfo();
}

const double N3 = sqrt(homography.at<double>(2, 0) * homography.at<double>(2, 0) + homography.at<double>(2, 1) * homography.at<double>(2, 1));
if (N3 > 0.002) {
return homography::WebARKitHomographyInfo();
}

std::vector<uchar> status;
std::vector<cv::DMatch> inlier_matches;
int linliers = 0;
for (int i = 0; i < pts1.size(); i++) {
if ((int)inlier_mask.at<uchar>(i,0) == 1) {
status.push_back((uchar)1);
inlier_matches.push_back(cv::DMatch(i, i, 0));
linliers++;
} else {
status.push_back((uchar)0);
}
}
//Return homography and corresponding inlier point sets
return homography::WebARKitHomographyInfo(homography, status, inlier_matches);
}

/*static auto im_gray(uchar* data, size_t cols, size_t rows) {
uint32_t idx;
uchar gray[rows][cols];
Expand Down

0 comments on commit f5a0e89

Please sign in to comment.