Skip to content

Commit

Permalink
oveloaded convert2Grayscale function and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Nov 13, 2023
1 parent 022b559 commit 47e102f
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 2 deletions.
12 changes: 12 additions & 0 deletions WebARKit/WebARKitManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ bool WebARKitManager::initialiseBase(webarkit::TRACKER_TYPE trackerType, int fra
return true;
}

bool WebARKitManager::initTracker(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
WEBARKIT_LOGd("WebARKitManager::initTracker(...)\n");
if (refData.empty() || refCols <= 0 || refRows <= 0) {
WEBARKIT_LOGe("Error initialising tracker.\n");
return false;
}
m_tracker->initTracker(refData, refCols, refRows, colorSpace);
state = WAITING_FOR_VIDEO;
WEBARKIT_LOGd("WebARKitManager::initTracker() done.\n");
return true;
}

bool WebARKitManager::initTracker(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
WEBARKIT_LOGd("WebARKitManager::initTracker(...)\n");
if (!refData || refCols <= 0 || refRows <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,54 @@ class WebARKitTracker::WebARKitTrackerImpl {
webarkit::cameraProjectionMatrix(camData, 0.1, 1000.0, frameWidth, frameHeight, m_cameraProjectionMatrix);
}

void initTracker(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
WEBARKIT_LOGi("Init Tracker!\n");

cv::Mat refGray = convert2Grayscale(refData, refCols, refRows, colorSpace);

cv::Mat trackerFeatureMask = createTrackerFeatureMask(refGray);

this->_featureDetector->detect(refGray, refKeyPts, trackerFeatureMask);
this->_featureDescriptor->compute(refGray, refKeyPts, refDescr);

// Normalized dimensions :
const float maxSize = std::max(refCols, refRows);
const float unitW = refCols / maxSize;
const float unitH = refRows / maxSize;

_pattern.size = cv::Size(refCols, refRows);

WEBARKIT_LOGd("WebARKitPattern size ready!\n");

_pattern.points2d.push_back(cv::Point2f(0, 0));
_pattern.points2d.push_back(cv::Point2f(refCols, 0));
_pattern.points2d.push_back(cv::Point2f(refCols, refRows));
_pattern.points2d.push_back(cv::Point2f(0, refRows));

WEBARKIT_LOGd("WebARKitPattern points2d ready!\n");

_pattern.points3d.push_back(cv::Point3f(-unitW, -unitH, 0));
_pattern.points3d.push_back(cv::Point3f(unitW, -unitH, 0));
_pattern.points3d.push_back(cv::Point3f(unitW, unitH, 0));
_pattern.points3d.push_back(cv::Point3f(-unitW, unitH, 0));

WEBARKIT_LOGd("WebARKitPattern points3d ready!\n");

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

_bBox.push_back(cv::Point2f(0, 0));
_bBox.push_back(cv::Point2f(refCols, 0));
_bBox.push_back(cv::Point2f(refCols, refRows));
_bBox.push_back(cv::Point2f(0, refRows));

initialized = true;

WEBARKIT_LOGi("Tracker ready!\n");
}

void initTracker(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
WEBARKIT_LOGi("Init Tracker!\n");

Expand Down Expand Up @@ -413,6 +461,10 @@ void WebARKitTracker::initialize(webarkit::TRACKER_TYPE trackerType, int frameWi
_trackerImpl->initialize(trackerType, frameWidth, frameHeight);
}

void WebARKitTracker::initTracker(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
_trackerImpl->initTracker(refData, refCols, refRows, colorSpace);
}

void WebARKitTracker::initTracker(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
_trackerImpl->initTracker(refData, refCols, refRows, colorSpace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class WebARKitTracker {

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

void initTracker(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace);

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

void processFrameData(uchar* frameData, size_t frameCols, size_t frameRows, ColorSpace colorSpace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ namespace webarkit {
return cv::Mat(rows, cols, CV_8UC1, gray);
}*/

static auto convert2Grayscale(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
cv::Mat refGray;

switch (colorSpace) {
case ColorSpace::RGBA: {
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(refData, refGray, cv::COLOR_RGBA2GRAY);
} break;
case ColorSpace::RGB: {
refGray.create(refRows, refCols, CV_8UC1);
cv::cvtColor(refData, refGray, cv::COLOR_RGB2GRAY);
} break;
case ColorSpace::GRAY: {
refGray = refData;
} break;
default: {
refGray = refData;
}
}

return refGray;
}

static auto convert2Grayscale(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace) {
cv::Mat refGray;

Expand Down
2 changes: 2 additions & 0 deletions WebARKit/include/WebARKitManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class WebARKitManager {
*/
std::shared_ptr<webarkit::WebARKitTracker> getTracker() { return m_tracker; };

bool initTracker(cv::Mat refData, size_t refCols, size_t refRows, ColorSpace colorSpace);

bool initTracker(uchar* refData, size_t refCols, size_t refRows, ColorSpace colorSpace);

bool update();
Expand Down
32 changes: 30 additions & 2 deletions tests/webarkit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ TEST(WebARKitTest, InitTrackerTest) {
// Init the manager with the Akaze tracker
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480);
// Load the test image
cv::Mat image = cv::imread("../pinball.jpg", cv::IMREAD_UNCHANGED);
cv::Mat image = cv::imread("../pinball.jpg", cv::IMREAD_GRAYSCALE);

if(image.data == NULL) {
std::cout << "Something wrong while reading the image!" << std::endl;
Expand All @@ -178,9 +178,37 @@ TEST(WebARKitTest, InitTrackerTest) {
EXPECT_EQ(image.cols, 1637);
EXPECT_EQ(image.rows, 2048);
// Check if initTracker returns sucessfully
EXPECT_TRUE(manager.initTracker(data, width, height, webarkit::ColorSpace::RGB));
EXPECT_TRUE(manager.initTracker(data, width, height, webarkit::ColorSpace::GRAY));
}

TEST(WebARKitTest, InitTrackerTest2) {
// Create a WebARKitManager object
webarkit::WebARKitManager manager;
// Init the manager with the Akaze tracker
manager.initialiseBase(webarkit::TRACKER_TYPE::AKAZE_TRACKER, 640, 480);
// Load the test image
cv::Mat image = cv::imread("../pinball.jpg", cv::IMREAD_GRAYSCALE);

if(image.data == NULL) {
std::cout << "Something wrong while reading the image!" << std::endl;
}

if(image.empty()) {
image = cv::Mat::zeros(2048, 1637, CV_8UC4);
}

ASSERT_FALSE(image.empty());

int width = image.cols;
int height = image.rows;
//unsigned char* data = image.data;
EXPECT_EQ(image.cols, 1637);
EXPECT_EQ(image.rows, 2048);
// Check if initTracker returns sucessfully
EXPECT_TRUE(manager.initTracker(image, width, height, webarkit::ColorSpace::GRAY));
}


// Check WebARKit version
TEST(WebARKitTest, CheckShutDown) {
// Create a WebARKitManager object
Expand Down

0 comments on commit 47e102f

Please sign in to comment.