Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature camera class #22

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WebARKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ ${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/
${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking//WebARKitEnums.h
${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h
${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitUtils.h
${PARENT_DIR}/include/WebARKitCamera.h
${PARENT_DIR}/include/WebARKitLog.h
${PARENT_DIR}/include/WebARKitManager.h
)

set(SOURCE
${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.cpp
${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp
${PARENT_DIR}/WebARKitCamera.cpp
${PARENT_DIR}/WebARKitLog.cpp
${PARENT_DIR}/WebARKitManager.cpp
)
Expand Down
52 changes: 52 additions & 0 deletions WebARKit/WebARKitCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <WebARKitCamera.h>
#include <WebARKitLog.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitConfig.h>

namespace webarkit {
WebARKitCamera::WebARKitCamera() : xsize(-1), ysize(-1), diagonal_fov_degrees(70.0) { cmat.fill(0.0); }

WebARKitCamera::~WebARKitCamera() {}

bool WebARKitCamera::setupCamera(int width, int height) {
if (width <= 0 || height <= 0) {
return false;
}
xsize = width;
ysize = height;

setFocalLength(xsize, ysize);

cmat.at(0) = focal_length;
cmat.at(2) = 0.5 * xsize;
cmat.at(4) = focal_length;
cmat.at(5) = 0.5 * ysize;
cmat.at(8) = 1.0;
kc.fill(0.0);
return true;
};

void WebARKitCamera::printSettings() {
WEBARKIT_LOGi("WebARKit: Camera Size %d , %d\n", xsize, ysize);
WEBARKIT_LOGi("WebARKit: camera matrix = [%.2f %.2f %.2f]\n", cmat[0], cmat[1], cmat[2]);
WEBARKIT_LOGi(" [%.2f %.2f %.2f]\n", cmat[3], cmat[4], cmat[5]);
WEBARKIT_LOGi(" [%.2f %.2f %.2f]\n", cmat[6], cmat[7], cmat[8]);
WEBARKIT_LOGi("WebARKit: kc = [%.4f %.4f %.4f %.4f %.4f %.4f]\n", kc[0], kc[1], kc[2], kc[3], kc[4], kc[5]);
};

std::array<double, 9> WebARKitCamera::getCameraData() const {
return cmat;
}

std::array<double, 6> WebARKitCamera::getDistortionCoefficients() const {
return kc;
}

void WebARKitCamera::setFocalLength(int width, int height) {
double diagonal_image_size;
double diagonal_fov_radians;
// simple routine to calculate focal length from diagonal field of view, and convert to camera matrix.
diagonal_image_size = std::pow(std::pow(width, 2.0) + std::pow(height, 2.0), 0.5);
diagonal_fov_radians = diagonal_fov_degrees * m_pi / 180.0;
focal_length = 0.5 * diagonal_image_size / std::tan(0.5 * diagonal_fov_radians);
}
} // namespace webarkit
4 changes: 2 additions & 2 deletions WebARKit/WebARKitManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string WebARKitManager::getWebARKitVersion() {
return versionString;
}

bool WebARKitManager::initialiseBase(webarkit::TRACKER_TYPE trackerType) {
bool WebARKitManager::initialiseBase(webarkit::TRACKER_TYPE trackerType, int frameWidth, int frameHeight) {
WEBARKIT_LOGd("WebARKItManager::initialiseBase(...)\n");
if (state != NOTHING_INITIALISED) {
WEBARKIT_LOGe("Initialise called while already initialised. Will finish first.\n");
Expand All @@ -33,7 +33,7 @@ bool WebARKitManager::initialiseBase(webarkit::TRACKER_TYPE trackerType) {
m_trackerType = trackerType;

m_tracker = std::make_shared<webarkit::WebARKitTracker>();
m_tracker->initialize(m_trackerType);
m_tracker->initialize(m_trackerType, frameWidth, frameHeight);

state = BASE_INITIALISED;

Expand Down
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 m_pi = 3.14159265358979323846;
extern const std::string WEBARKIT_HEADER_VERSION_STRING = "1.0.0";
/*@
The MAJOR version number defines non-backwards compatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ class WebARKitTracker::WebARKitTrackerImpl {
public:
WebARKitTrackerImpl()
: corners(4), initialized(false), output(17, 0.0), _valid(false), _isDetected(false), numMatches(0),
minNumMatches(MIN_NUM_MATCHES), _nn_match_ratio(0.7f){};
minNumMatches(MIN_NUM_MATCHES), _nn_match_ratio(0.7f){
m_camMatrix = cv::Mat();
m_distortionCoeff = cv::Mat();
};
~WebARKitTrackerImpl() = default;

void initialize(webarkit::TRACKER_TYPE trackerType) {
void initialize(webarkit::TRACKER_TYPE trackerType, int frameWidth, int frameHeight) {
setDetectorType(trackerType);
if (trackerType == webarkit::TEBLID_TRACKER) {
_nn_match_ratio = TEBLID_NN_MATCH_RATIO;
Expand All @@ -22,6 +25,10 @@ class WebARKitTracker::WebARKitTrackerImpl {
_nn_match_ratio = DEFAULT_NN_MATCH_RATIO;
minNumMatches = 15;
}
_camera->setupCamera(frameWidth, frameHeight);
_camera->printSettings();
m_camMatrix = cv::Mat(3,3, CV_64FC1, _camera->getCameraData().data());
m_distortionCoeff = cv::Mat(6, 1, CV_64FC1, _camera->getDistortionCoefficients().data());
}

void initTracker(uchar* refData, size_t refCols, size_t refRows) {
Expand Down Expand Up @@ -288,6 +295,11 @@ class WebARKitTracker::WebARKitTrackerImpl {

bool initialized;

WebARKitCamera* _camera = new WebARKitCamera();

cv::Mat m_camMatrix;
cv::Mat m_distortionCoeff;

private:
std::vector<double> output; // 9 from homography matrix, 8 from warped corners*/

Expand Down Expand Up @@ -339,7 +351,7 @@ WebARKitTracker::WebARKitTracker(WebARKitTracker&&) = default; // copy construct

WebARKitTracker& WebARKitTracker::operator=(WebARKitTracker&&) = default; // move assignment operator

void WebARKitTracker::initialize(webarkit::TRACKER_TYPE trackerType) { _trackerImpl->initialize(trackerType); }
void WebARKitTracker::initialize(webarkit::TRACKER_TYPE trackerType, int frameWidth, int frameHeight) { _trackerImpl->initialize(trackerType, frameWidth, frameHeight); }

void WebARKitTracker::initTracker(uchar* refData, size_t refCols, size_t refRows) {
_trackerImpl->initTracker(refData, refCols, refRows);
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 m_pi;
extern const std::string WEBARKIT_HEADER_VERSION_STRING;
extern const int WEBARKIT_HEADER_VERSION_MAJOR;
extern const int WEBARKIT_HEADER_VERSION_MINOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "WebARKitEnums.h"
#include <WebARKitLog.h>
#include <WebARKitCamera.h>
#include <opencv2/xfeatures2d.hpp>

namespace webarkit {
Expand All @@ -17,7 +18,7 @@ class WebARKitTracker {

WebARKitTracker& operator=(WebARKitTracker&&);

void initialize(webarkit::TRACKER_TYPE trackerType);
void initialize(webarkit::TRACKER_TYPE trackerType, int frameWidth, int frameHeight);

void initTracker(uchar* refData, size_t refCols, size_t refRows);

Expand Down
33 changes: 33 additions & 0 deletions WebARKit/include/WebARKitCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef WEBARKITCAMERA_H
#define WEBARKITCAMERA_H

#include <array>

namespace webarkit {
class WebARKitCamera {
public:
WebARKitCamera();
~WebARKitCamera();

bool setupCamera(int width, int height);

void printSettings();

std::array<double, 9> getCameraData() const;

std::array<double, 6> getDistortionCoefficients() const;

double getFocalLength() const { return focal_length; }

private:
int xsize, ysize;
std::array<double, 9> cmat;
std::array<double, 6> kc;
double focal_length;
double diagonal_fov_degrees;

void setFocalLength(int width, int height);
};
} // namespace webarkit

#endif // WEBARKITCAMERA_H
2 changes: 1 addition & 1 deletion WebARKit/include/WebARKitManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class WebARKitManager {
* Start trackable management so trackables can be added and removed.
* @return true if initialisation was OK, false if an error occured.
*/
bool initialiseBase(webarkit::TRACKER_TYPE trackerType);
bool initialiseBase(webarkit::TRACKER_TYPE trackerType, int frameWidth, int frameHeight);

/**
* Return the current tracker object.
Expand Down
35 changes: 28 additions & 7 deletions tests/webarkit_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <WebARKitManager.h>
#include <WebARKitTrackers/WebARKitOpticalTracking/WebARKitEnums.h>
#include <WebARKitCamera.h>
#include <opencv2/imgcodecs.hpp>

class WebARKitEnumTest : public testing::TestWithParam<std::tuple<webarkit::TRACKER_TYPE, webarkit::ColorSpace>> {};
Expand Down Expand Up @@ -61,44 +62,64 @@ TEST(WebARKitConfigTest, TestBlurSize) {
EXPECT_EQ(expected_blur_size.height, blurSize.height);
}

TEST(WebARKitConfigTest, TestPIConstant) {
double internal_m_pi = 3.14159265358979323846;
EXPECT_EQ(internal_m_pi, m_pi);
}

TEST(WebARKitCameraTest, TestCamera) {
int width = 640;
int height = 480;
webarkit::WebARKitCamera camera;
EXPECT_TRUE(camera.setupCamera(width, height));
std::array<double, 9> camera_mat = camera.getCameraData();
EXPECT_EQ(camera_mat[0], 571.25920269684582);
EXPECT_EQ(camera_mat[2], 320.0);
EXPECT_EQ(camera_mat[4], 571.25920269684582);
EXPECT_EQ(camera_mat[5], 240.0);
EXPECT_EQ(camera_mat[8], 1.0);
EXPECT_EQ(camera.getFocalLength(), 571.25920269684582);
camera.printSettings();
}

// Check WebARKitManager initialisation.
TEST(WebARKitTest, InitialiseBaseAkazeTest) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Check if the WebARKitManager initialisation is successful
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER));
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480));
}

// Check WebARKitManager initialisation.
TEST(WebARKitTest, InitialiseBaseFreakTest) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Check if the WebARKitManager initialisation is successful
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::FREAK_TRACKER));
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::FREAK_TRACKER, 640, 480));
}

// Check WebARKitManager initialisation.
TEST(WebARKitTest, InitialiseBaseOrbTest) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Check if the WebARKitManager initialisation is successful
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::ORB_TRACKER));
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::ORB_TRACKER, 640, 480));
}

// Check WebARKitManager initialisation.
TEST(WebARKitTest, InitialiseBaseTeblidTest) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Check if the WebARKitManager initialisation is successful
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::TEBLID_TRACKER));
EXPECT_TRUE(manager.initialiseBase(webarkit::TRACKER_TYPE::TEBLID_TRACKER, 640, 480));
}

// Check WebARKit version
TEST(WebARKitTest, CheckWebARKitVersion) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Init the manager with the Akaze tracker
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER);
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480);
// Check if the WebARKit version is correct
EXPECT_STREQ(manager.getWebARKitVersion().c_str(), "1.0.0");
}
Expand All @@ -107,7 +128,7 @@ TEST(WebARKitTest, InitTrackerTest) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Init the manager with the Akaze tracker
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER);
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480);
// Load the test image
cv::Mat image = cv::imread("pinball.jpg");

Expand Down Expand Up @@ -135,7 +156,7 @@ TEST(WebARKitTest, CheckShutDown) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Init the manager with the Akaze tracker
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER);
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480);
// Check if the WebARKit went down successfully
EXPECT_TRUE(manager.shutdown());
}