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

Filtering feature #36

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
5,192 changes: 2,739 additions & 2,453 deletions build/artoolkit.debug.js

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions build/artoolkit.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/artoolkit_wasm.js

Large diffs are not rendered by default.

Binary file modified build/artoolkit_wasm.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions emscripten/ARBindEM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,9 @@ EMSCRIPTEN_BINDINGS(constant_bindings) {
constant("AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR", AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR + 0);
constant("AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR_MULTI", AR_MARKER_INFO_CUTOFF_PHASE_POSE_ERROR_MULTI + 0);
constant("AR_MARKER_INFO_CUTOFF_PHASE_HEURISTIC_TROUBLESOME_MATRIX_CODES", AR_MARKER_INFO_CUTOFF_PHASE_HEURISTIC_TROUBLESOME_MATRIX_CODES + 0);

/* Filter Trans Mat defaults for arFilterTransMatInit */
constant("AR_FILTER_TRANS_MAT_SAMPLE_RATE_DEFAULT", AR_FILTER_TRANS_MAT_SAMPLE_RATE_DEFAULT);
constant("AR_FILTER_TRANS_MAT_CUTOFF_FREQ_DEFAULT", AR_FILTER_TRANS_MAT_CUTOFF_FREQ_DEFAULT);

}
62 changes: 50 additions & 12 deletions emscripten/ARToolKitJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct arController {

KpmHandle* kpmHandle;
AR2HandleT* ar2Handle;
ARFilterTransMatInfo *ftmi;
ARdouble filterCutoffFrequency = 25.0;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to increase filterCutoffFrequency

ARdouble filterSampleRate = 50.0;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also filterSampleRate


int detectedPage = -2; // -2 Tracking not inited, -1 tracking inited OK, >= 0 tracking online on page.

Expand Down Expand Up @@ -96,6 +99,14 @@ extern "C" {
NFT API bindings
*/

void matrixLerp(ARdouble src[3][4], ARdouble dst[3][4], float interpolationFactor) {
kalwalt marked this conversation as resolved.
Show resolved Hide resolved
for (int i=0; i<3; i++) {
for (int j=0; j<4; j++) {
dst[i][j] = dst[i][j] + (src[i][j] - dst[i][j]) / interpolationFactor;
}
}
}

int getNFTMarkerInfo(int id, int markerIndex) {
if (arControllers.find(id) == arControllers.end()) { return ARCONTROLLER_NOT_FOUND; }
arController *arc = &(arControllers[id]);
Expand All @@ -108,10 +119,14 @@ extern "C" {
int kpmResultNum = -1;

float trans[3][4];
ARdouble transF[3][4];
ARdouble transFLerp[3][4];
memset( transFLerp, 0, 3 * 4 * sizeof(ARdouble) );
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this is not needed but not sure of this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed, without it the interpolation give weird results.

float err = -1;
if (arc->detectedPage == -2) {
kpmMatching( arc->kpmHandle, arc->videoLuma );
kpmGetResult( arc->kpmHandle, &kpmResult, &kpmResultNum );
arc->ftmi = arFilterTransMatInit(arc->filterSampleRate, arc->filterCutoffFrequency);
int i, j, k;
int flag = -1;
for( i = 0; i < kpmResultNum; i++ ) {
Expand Down Expand Up @@ -139,6 +154,25 @@ extern "C" {

if (arc->detectedPage >= 0) {
int trackResult = ar2TrackingMod(arc->ar2Handle, arc->surfaceSet[arc->detectedPage], arc->videoFrame, trans, &err);
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
transF[j][k] = trans[j][k];
}
}

bool reset;
if (trackResult < 0) {
reset = 1;
} else {
reset = 0;
}

if (arFilterTransMat(arc->ftmi, transF, reset) < 0) {
ARLOGe("arFilterTransMat error with marker %d.\n", markerIndex);
}

matrixLerp(transF, transFLerp, 24.0);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try with another interpolationFactor, maybe 12.0 instead of 24.0?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we have to try different values


if( trackResult < 0 ) {
ARLOGi("Tracking lost. %d\n", trackResult);
arc->detectedPage = -2;
Expand Down Expand Up @@ -179,20 +213,20 @@ extern "C" {
markerIndex,
err,

trans[0][0],
trans[0][1],
trans[0][2],
trans[0][3],
transFLerp[0][0],
transFLerp[0][1],
transFLerp[0][2],
transFLerp[0][3],

trans[1][0],
trans[1][1],
trans[1][2],
trans[1][3],
transFLerp[1][0],
transFLerp[1][1],
transFLerp[1][2],
transFLerp[1][3],

trans[2][0],
trans[2][1],
trans[2][2],
trans[2][3]
transFLerp[2][0],
transFLerp[2][1],
transFLerp[2][2],
transFLerp[2][3]
);
} else {
EM_ASM_({
Expand Down Expand Up @@ -347,6 +381,10 @@ extern "C" {
arPattDetach(arc->arhandle);
arDeleteHandle(arc->arhandle);
arc->arhandle = NULL;
if (arc->ftmi) {
arFilterTransMatFinal(arc->ftmi);
arc->ftmi = NULL;
}
}
if (arc->ar3DHandle != NULL) {
ar3DDeleteHandle(&(arc->ar3DHandle));
Expand Down
14 changes: 14 additions & 0 deletions examples/js/third_party/gpu.js/gpu-browser.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/nft_improved_worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ <h3>jsartoolkit5 demos with ar2Tracking</h3>
<ul>
<li><a href="threejs_worker_gltf.html">Show a 3D, animated model. Uses web worker. Has loader. Has interpolation.</a></li>
<li><a href="main_threejs_worker.html">Show a 3D sphere. Uses web worker. Has loader. Has interpolation. Asm version. (high framerate)</a></li>
<li><a href="main_threejs_filter_worker.html">Show a 3D sphere. Uses web worker. Has loader. No js interpolation with filtering and matrixLerp. Asm version. (high framerate)</a></li>
<li><a href="main_threejs_filter_gpujs_worker.html">Show a 3D sphere. Uses web worker. Has loader. No interpolation with filtering and gpujs. Asm version. (high framerate)</a></li>
<li><a href="main_threejs_wasm_worker.html">Show a 3D sphere. Uses web worker. Has loader. Has interpolation. Uses web assembly</a></li>
<li><a href="main.html">Draw a red rectangle around the image. Single-threaded. No loader</a></li>
<li><a href="main_worker.html">Draw a red rectangle around the image. Uses web worker. No loader</a></li>
Expand Down
142 changes: 142 additions & 0 deletions examples/nft_improved_worker/main_threejs_filter_gpujs_worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>NFT marker example with a WebWorker and Three.js</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=1">
<link rel="stylesheet" href="../css/video-style.css">
</head>
<body class="loading">
<div id="loading" >
<img src="../Data/logo.gif"/>
<span class="loading-text">Loading, please wait</span>
</div>
<!--
==================
STATS
==================
-->

<div id="stats" class="ui stats">

<div id="stats1" class="stats-item">
<p class="stats-item-title">
Main
</p>
</div>

<div id="stats2" class="stats-item">
<p class="stats-item-title">
Worker
</p>
</div>

</div>

<!--
==================
CAMERA VIDEO & CANVAS
==================
-->

<div id="app">
<video
loop
autoplay
muted
playsinline
id="video">
</video>

<canvas id="canvas"></canvas>
</div>

<a
href="https://raw.githubusercontent.com/artoolkit/artoolkit5/master/doc/Marker%20images/pinball.jpg"
class="ui marker"
target="_blank">
🖼 Marker Image
</a>

<script src="../js/third_party/three.js/stats.min.js"></script>
<script src="../js/third_party/three.js/three.min.js"></script>
<script src="../js/third_party/gpu.js/gpu-browser.min.js"></script>
<script src="threejs_filter_gpujs_worker.js"></script>

<script>

/**
* STATS
*/
var statsMain = new Stats();
statsMain.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById( 'stats1' ).appendChild( statsMain.dom );

var statsWorker = new Stats();
statsWorker.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById( 'stats2' ).appendChild( statsWorker.dom );

/**
* APP / ELEMENTS
*/
var container = document.getElementById( 'app' );
var video = document.getElementById( 'video' );
var canvas = document.getElementById( 'canvas' );

/**
* APP / VIDEO STREAM
*/

if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
var hint = {
audio: false,
video: true
};
if( window.innerWidth < 800 ) {
var width = ( window.innerWidth < window.innerHeight ) ? 240 : 360;
var height = ( window.innerWidth < window.innerHeight ) ? 360 : 240;

var aspectRatio = window.innerWidth / window.innerHeight;

console.log( width, height );

hint = {
audio: false,
video: {
facingMode: 'environment',
width: { min: width, max: width }
},
};

console.log( hint );
}

navigator.mediaDevices.getUserMedia( hint ).then( function( stream ) {
video.srcObject = stream;
video.addEventListener( 'loadedmetadata', function() {
video.play();

console.log( 'video', video, video.videoWidth, video.videoHeight );

start(
container,
markers['pinball'],
video,
video.videoWidth,
video.videoHeight,
canvas,
function() {
statsMain.update()
},
function() {
statsWorker.update();
},
null
);
} );
} );
}
</script>
</body>

</html>
Loading