Skip to content

Commit 0269850

Browse files
author
Max Krogius
committed
Merge branch 's-trinh-fix_mem_leaks'
2 parents f428ced + 285cb24 commit 0269850

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

apriltag.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ zarray_t *apriltag_detector_detect(apriltag_detector_t *td, image_u8_t *im_orig)
10451045
image_u8_write_pnm(quad_im, "debug_preprocess.pnm");
10461046

10471047
zarray_t *quads = apriltag_quad_thresh(td, quad_im);
1048-
1048+
10491049
// adjust centers of pixels so that they correspond to the
10501050
// original full-resolution image.
10511051
if (td->quad_decimate > 1) {
@@ -1286,6 +1286,8 @@ zarray_t *apriltag_detector_detect(apriltag_detector_t *td, image_u8_t *im_orig)
12861286
}
12871287
}
12881288

1289+
image_u8_destroy(darker);
1290+
12891291
for (int i = 0; i < zarray_size(detections); i++) {
12901292
apriltag_detection_t *det;
12911293
zarray_get(detections, i, &det);

apriltag_pose.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ double orthogonal_iteration(matd_t** v, matd_t** p, matd_t** t, matd_t** R, int
124124
matd_destroy(p_res[i]);
125125
matd_destroy(F[i]);
126126
}
127+
matd_destroy(p_mean);
127128
return prev_error;
128129
}
129130

@@ -280,7 +281,7 @@ matd_t* fix_pose_ambiguities(matd_t** v, matd_t** p, matd_t* t, matd_t* R, int n
280281
r31/hypotenuse, -r32/hypotenuse, 0,
281282
r32/hypotenuse, r31/hypotenuse, 0,
282283
0, 0, 1});
283-
284+
284285
// 3. Calculate parameters of Eos
285286
matd_t* R_trans = matd_multiply(R_1_prime, R_z);
286287
double sin_gamma = -MATD_EL(R_trans, 0, 1);
@@ -323,23 +324,31 @@ matd_t* fix_pose_ambiguities(matd_t** v, matd_t** p, matd_t* t, matd_t* R, int n
323324
matd_t* b1 = matd_create(3, 1);
324325
matd_t* b2 = matd_create(3, 1);
325326
for (int i = 0; i < n_points; i++) {
326-
matd_add_inplace(b0, matd_op("(M-M)MM", F_trans[i], I3, R_gamma, p_trans[i]));
327-
matd_add_inplace(b1, matd_op("(M-M)MMM", F_trans[i], I3, R_gamma, M1, p_trans[i]));
328-
matd_add_inplace(b2, matd_op("(M-M)MMM", F_trans[i], I3, R_gamma, M2, p_trans[i]));
327+
matd_t* op_tmp1 = matd_op("(M-M)MM", F_trans[i], I3, R_gamma, p_trans[i]);
328+
matd_t* op_tmp2 = matd_op("(M-M)MMM", F_trans[i], I3, R_gamma, M1, p_trans[i]);
329+
matd_t* op_tmp3 = matd_op("(M-M)MMM", F_trans[i], I3, R_gamma, M2, p_trans[i]);
330+
331+
matd_add_inplace(b0, op_tmp1);
332+
matd_add_inplace(b1, op_tmp2);
333+
matd_add_inplace(b2, op_tmp3);
334+
335+
matd_destroy(op_tmp1);
336+
matd_destroy(op_tmp2);
337+
matd_destroy(op_tmp3);
329338
}
330-
b0 = matd_multiply(G, b0);
331-
b1 = matd_multiply(G, b1);
332-
b2 = matd_multiply(G, b2);
339+
matd_t* b0_ = matd_multiply(G, b0);
340+
matd_t* b1_ = matd_multiply(G, b1);
341+
matd_t* b2_ = matd_multiply(G, b2);
333342

334343
double a0 = 0;
335344
double a1 = 0;
336345
double a2 = 0;
337346
double a3 = 0;
338347
double a4 = 0;
339348
for (int i = 0; i < n_points; i++) {
340-
matd_t* c0 = matd_op("(M-M)(MM+M)", I3, F_trans[i], R_gamma, p_trans[i], b0);
341-
matd_t* c1 = matd_op("(M-M)(MMM+M)", I3, F_trans[i], R_gamma, M1, p_trans[i], b1);
342-
matd_t* c2 = matd_op("(M-M)(MMM+M)", I3, F_trans[i], R_gamma, M2, p_trans[i], b2);
349+
matd_t* c0 = matd_op("(M-M)(MM+M)", I3, F_trans[i], R_gamma, p_trans[i], b0_);
350+
matd_t* c1 = matd_op("(M-M)(MMM+M)", I3, F_trans[i], R_gamma, M1, p_trans[i], b1_);
351+
matd_t* c2 = matd_op("(M-M)(MMM+M)", I3, F_trans[i], R_gamma, M2, p_trans[i], b2_);
343352

344353
a0 += matd_to_double(matd_op("M'M", c0, c0));
345354
a1 += matd_to_double(matd_op("2M'M", c0, c1));
@@ -352,14 +361,21 @@ matd_t* fix_pose_ambiguities(matd_t** v, matd_t** p, matd_t* t, matd_t* R, int n
352361
matd_destroy(c2);
353362
}
354363

364+
matd_destroy(b0);
365+
matd_destroy(b1);
366+
matd_destroy(b2);
367+
matd_destroy(b0_);
368+
matd_destroy(b1_);
369+
matd_destroy(b2_);
370+
355371
for (int i = 0; i < n_points; i++) {
356372
matd_destroy(p_trans[i]);
357373
matd_destroy(v_trans[i]);
358374
matd_destroy(F_trans[i]);
359375
}
360376
matd_destroy(avg_F_trans);
361377
matd_destroy(G);
362-
378+
363379

364380
// 4. Solve for minima of Eos.
365381
double p0 = a1;
@@ -384,7 +400,7 @@ matd_t* fix_pose_ambiguities(matd_t** v, matd_t** p, matd_t* t, matd_t* R, int n
384400
if (a2 - 2*a0 + (3*a3 - 6*a1)*t1 + (6*a4 - 8*a2 + 10*a0)*t2 + (-8*a3 + 6*a1)*t3 + (-6*a4 + 3*a2)*t4 + a3*t5 >= 0) {
385401
// And that it corresponds to an angle different than the known minimum.
386402
double t = 2*atan(roots[i]);
387-
// We only care about finding a second local minima which is qualitatively
403+
// We only care about finding a second local minima which is qualitatively
388404
// different than the first.
389405
if (fabs(t - t_initial) > 0.1) {
390406
minima[n_minima++] = roots[i];
@@ -484,6 +500,11 @@ void estimate_tag_pose_orthogonal_iteration(
484500
} else {
485501
*err2 = HUGE_VAL;
486502
}
503+
504+
for (int i = 0; i < 4; i++) {
505+
matd_destroy(p[i]);
506+
matd_destroy(v[i]);
507+
}
487508
}
488509

489510
/**
@@ -496,10 +517,14 @@ double estimate_tag_pose(apriltag_detection_info_t* info, apriltag_pose_t* pose)
496517
if (err1 <= err2) {
497518
pose->R = pose1.R;
498519
pose->t = pose1.t;
520+
matd_destroy(pose2.R);
521+
matd_destroy(pose2.t);
499522
return err1;
500523
} else {
501524
pose->R = pose2.R;
502525
pose->t = pose2.t;
526+
matd_destroy(pose1.R);
527+
matd_destroy(pose1.t);
503528
return err2;
504529
}
505530
}

apriltag_quad_thresh.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void fit_line(struct line_fit_pt *lfps, int sz, int i0, int i1, double *lineparm
208208
// nx_old = cosf(normal_theta);
209209
// ny_old = sinf(normal_theta);
210210
//}
211-
211+
212212
// Instead of using the above cos/sin method, pose it as an eigenvalue problem.
213213
double eig_small = 0.5*(Cxx + Cyy - sqrtf((Cxx - Cyy)*(Cxx - Cyy) + 4*Cxy*Cxy));
214214

@@ -568,7 +568,7 @@ int quad_segment_agg(apriltag_detector_t *td, zarray_t *cluster, struct line_fit
568568

569569
return 1;
570570
}
571-
571+
572572
/**
573573
* Compute statistics that allow line fit queries to be
574574
* efficiently computed for any contiguous range of indices.
@@ -1882,7 +1882,9 @@ zarray_t *apriltag_quad_thresh(apriltag_detector_t *td, image_u8_t *im)
18821882
fprintf(f, "0 %d translate\n", im2->height);
18831883
fprintf(f, "1 -1 scale\n");
18841884

1885-
postscript_image(f, im);
1885+
postscript_image(f, im2);
1886+
1887+
image_u8_destroy(im2);
18861888

18871889
for (int i = 0; i < zarray_size(quads); i++) {
18881890
struct quad *q;

0 commit comments

Comments
 (0)