static void ShowEigs(const MAT& eigvals) { double min_eig = eigvals(0) / 1000; double var_all = SumElems(eigvals); double var10 = 0; int neigs = NSIZE(eigvals); for (int i = 0; i < MIN(10, neigs); i++) var10 += eigvals(i); int iprint = 0; while (iprint < neigs && eigvals(iprint) > min_eig && iprint < 9) iprint++; if (iprint < neigs + 1) // show first small eig value, for context iprint++; lprintf("%.0f%% percent variance is explained by %s%d shape eigs:", 100 * var10 / var_all, (iprint < neigs? "the first ": ""), iprint); const MAT eigvals_t(eigvals.t()); for (int i = 0; i < iprint; i++) lprintf(" %.0f", 100 * eigvals_t(i) / var_all); lprintf("%%\n"); }
static void GenShapeMod( Shape& meanshape, // out: n x 2 VEC& eigvals, // out: 2n x 1 MAT& eigvecs, // out: 2n x 2n, inverse of eigs of cov mat vec_Shape& aligned_shapes, // out: shapes aligned to refshape const ShapeFile& sh, // in int irefshape, // in: index of shape used to align other shapes const vec_int& nused_vec, // in: vec [nshapes] of int const char* outdir, // in: output directory (-d flag) const char* modname, // in: e.g. "yaw00" const char* cmdline, // in: command line used to invoke tasm bool force_facedet, // in: facedet the images themselves (ignore shapefile facedets) bool write_detpars) // in: write the image detpars to facedet.shape { CopyShapeVec(aligned_shapes, sh.shapes_); // after this, mean shape and aligned_shapes will be aligned to ref shape AlignTrainingShapes(meanshape, aligned_shapes, aligned_shapes[irefshape]); MAT cov(CalcShapeCov(aligned_shapes, meanshape)); EigsOfSymMat(eigvecs, eigvals, cov); if (eigvals(0) < 0.1) // number is arbitrary lprintf("eigen data invalid " "(not enough variation in shapes, max eigenvalue is %g)", eigvals(0)); eigvals *= 1000 / eigvals(0); // normalize so biggest eigval is 1000 (to match old Stasm) ZapSmallEigs(eigvals, eigvecs); eigvecs = eigvecs.t(); // invert (eigvecs is orthog so transpose is inverse) ShowEigs(eigvals); // now align mean shape to the face detector frame MeanShapeAlignedToFaceDets(meanshape, sh, nused_vec, outdir, modname, cmdline, force_facedet, write_detpars); }