Skip to content

Commit

Permalink
using push_back to avoid an Seg fault
Browse files Browse the repository at this point in the history
  • Loading branch information
kalwalt committed Nov 1, 2023
1 parent c22785c commit e91098e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
2 changes: 2 additions & 0 deletions WebARKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/
${PARENT_DIR}/include/WebARKitCamera.h
${PARENT_DIR}/include/WebARKitLog.h
${PARENT_DIR}/include/WebARKitManager.h
${PARENT_DIR}/include/WebARKitPattern.h
)

set(SOURCE
Expand All @@ -44,6 +45,7 @@ ${PARENT_DIR}/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp
${PARENT_DIR}/WebARKitCamera.cpp
${PARENT_DIR}/WebARKitLog.cpp
${PARENT_DIR}/WebARKitManager.cpp
${PARENT_DIR}/WebARKitPattern.cpp
)

add_library(
Expand Down
18 changes: 18 additions & 0 deletions WebARKit/WebARKitPattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <WebARKitPattern.h>
#include <opencv2/calib3d.hpp>

void WebARKitPatternTrackingInfo::computePose(const WebARKitPattern& pattern, cv::Mat caMatrix, cv::Mat distCoeffs)
{
cv::Mat Rvec;
cv::Mat_<float> Tvec;
cv::Mat raux, taux;

cv::solvePnPRansac(pattern.points3d, points2d, caMatrix, distCoeffs, raux, taux);
raux.convertTo(Rvec, CV_32F);
taux.convertTo(Tvec, CV_32F);

cv::Mat_<float> rotMat(3, 3);
cv::Rodrigues(Rvec, rotMat);

cv::hconcat(rotMat, Tvec, pose3d);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ class WebARKitTracker::WebARKitTrackerImpl {

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

_pattern.points2d[0] = cv::Point2f(0, 0);
_pattern.points2d[1] = cv::Point2f(refCols, 0);
_pattern.points2d[2] = cv::Point2f(refCols, refRows);
_pattern.points2d[3] = cv::Point2f(0, refRows);

_pattern.points3d[0] = cv::Point3f(-unitW, -unitH, 0);
_pattern.points3d[1] = cv::Point3f(unitW, -unitH, 0);
_pattern.points3d[2] = cv::Point3f(unitW, unitH, 0);
_pattern.points3d[3] = cv::Point3f(-unitW, unitH, 0);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "WebARKitEnums.h"
#include <WebARKitLog.h>
#include <WebARKitCamera.h>
#include <WebARKitPattern..h>
#include <WebARKitPattern.h>
#include <opencv2/xfeatures2d.hpp>

namespace webarkit {
Expand Down
18 changes: 0 additions & 18 deletions WebARKit/include/WebARKitPattern..h

This file was deleted.

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

#include <opencv2/core/core.hpp>

struct WebARKitPattern {
cv::Size size;

//cv::Mat grayImg;

//std::vector<cv::KeyPoint> keypoints;
//cv::Mat descriptors;

std::vector<cv::Point2f> points2d;
std::vector<cv::Point3f> points3d;
};

/**
* Intermediate pattern tracking info structure
*/
struct WebARKitPatternTrackingInfo
{
cv::Mat homography;
std::vector<cv::Point2f> points2d;
cv::Mat pose3d;

/**
* Compute pattern pose using PnP algorithm
*/
void computePose(const WebARKitPattern& pattern, cv::Mat caMatrix, cv::Mat distCoeffs);
};

#endif // WEBARKITPATTERN_H
2 changes: 1 addition & 1 deletion tests/webarkit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ TEST(WebARKitTest, InitTrackerTest) {
}

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

ASSERT_FALSE(image.empty());
Expand Down

0 comments on commit e91098e

Please sign in to comment.