-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibCompute.js
149 lines (110 loc) · 4.19 KB
/
calibCompute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { auth, db, signInWithGoogle, signOutGoogle, onAuthStateChanged, setDoc, getDoc, doc } from './src/firebase';
// <script async src="https://docs.opencv.org/4.x/opencv.js"></script>
// import { cv } from 'opencv.js';
import { estimatePose3D, estimatePose3DFromMultipleMarkers,
serializeTransformationMatrix, deserializeTransformationMatrix,
projectPointToPlane, applyHomography,
applyTransformInCSS, calculatePerspectiveWrap,
drawCVImage, detectArucoMarkers } from './src/poseEstimation';
// TODO:
// Nouvelle page avec :
// Centres + FFT calculés
// Tracking Aruco, et projection des coins sur la feuille.
let logged_user;
onAuthStateChanged(auth, (user) => {
if (user) {
logged_user = user;
loadDocs();
} else {
logged_user = null;
}
});
async function loadDocs(){
const cameraConfigDoc = await getDoc(doc(db, 'users', logged_user.uid));
const data = cameraConfigDoc.data();
console.log(data);
const matches = [];
const blinkingCircles = data.blinkingCircles;
blinkingCircles.forEach((circle, i) => {
const { x, y } = circle;
const gridId = `grid-${i}`;
const gridCircle = document.getElementById(gridId);
gridCircle.style.left = x + 'px';
gridCircle.style.top = y + 'px';
});
const fftCenters = data.fftCenters;
const pose = data.poseMatrix;
const cameraConfig = data.cameraConfig;
const resolution = cameraConfig.resolution.split('x');
const width = parseInt(resolution[0]);
const height = parseInt(resolution[1]);
cameraWidth = width;
cameraHeight = height;
let cx = width / 2;
let cy = height / 2;
let fx = parseFloat(cameraConfig.focalLength);
let fy = parseFloat(cameraConfig.focalLength);
let cam = { width, height, cx, cy, fx, fy };
console.log(pose)
console.log("cam... ", cam)
const transformationMatrix = deserializeTransformationMatrix(pose);
blinkingCircles.forEach(circle => {
const { x, y, frequency } = circle;
fftCenters.forEach(center => {
const freqDiff = Math.abs(frequency - center.freq);
if (freqDiff < 0.05) {
// TODO: Matching not working well.
// Projecting not working well either.
// Seen by the camera
let cameraPoint = {x: center.x, y: center.y};
const paperPoint = projectPointToPlane(cameraPoint, cam, transformationMatrix);
matches.push({
projectorPoint: { x, y, frequency },
cameraPoint: { x: center.x, y: center.y, freq: center.freq },
paperPoint,
freqDiff
});
}
});
});
console.log(matches);
findHomography(matches)
}
function findHomography(matches) {
let originPointsData = [];
let targetPointsData = [];
matches.forEach(match => {
originPointsData.push(match.paperPoint[0], match.paperPoint[1]);
targetPointsData.push(match.cameraPoint.x, match.cameraPoint.y);
});
// Creating the originPoints and targetPoints matrices
let originPoints = cv.matFromArray(matches.length, 1, cv.CV_32FC2, originPointsData);
let targetPoints = cv.matFromArray(matches.length, 1, cv.CV_32FC2, targetPointsData);
// Compute the homography matrix Paper -> Camera ?
let H = cv.findHomography(originPoints, targetPoints);
console.log("Homography Matrix:\n", H.data64F);
let originX = 0;
let originY = 0;
let w = 297 /2;
let h = 210 /2;
createCircleAtTransformedPoint(H, originX, originY);
createCircleAtTransformedPoint(H, originX + w, originY);
createCircleAtTransformedPoint(H, originX, originY + h);
createCircleAtTransformedPoint(H, originX + w, originY + h);
const pose = serializeTransformationMatrix(H);
setDoc(doc(db, 'users', logged_user.uid), { paperToProjection: pose }, { merge: true });
console.log('TableHomography saved successfully');
// Cleanup
targetPoints.delete();
originPoints.delete();
H.delete();
}
function createCircleAtTransformedPoint(H, originX, originY) {
const point3D = [originX, originY, 1];
const point2D = applyHomography(H, point3D);
const circle = document.createElement('div');
circle.className = 'circle';
circle.style.left = point2D[0] + 'px';
circle.style.top = point2D[1] + 'px';
document.getElementById('container').appendChild(circle);
}