BinocularPolyFit.cpp 5.1 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
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
#include "BinocularPolyFit.h"

using namespace std;
using namespace cv;

BinocularPolyFit::BinocularPolyFit(const PlType& plType)
    : PolyFit(plType)
    , binocularUnknowns(0)
    , binocularCalibrated(false)
{
    binocularUnknowns = 2 * (PolyFit::unknowns - 1) + 1;
}

string BinocularPolyFit::description() const
{
    return "BI" + PolyFit::description();
}

bool BinocularPolyFit::calibrate(vector<CollectionTuple>& calibrationTuples, QString& errorMsg)
{
    PointVector leftPupil;
    PointVector rightPupil;
    PointVector gaze;

    for (const auto& tuple : calibrationTuples) {
        leftPupil.insert(PolyFit::normalize(tuple.lEye.pupil.center, tuple.lEye.input));
        rightPupil.insert(PolyFit::normalize(tuple.rEye.pupil.center, tuple.rEye.input));
        gaze.insert(PolyFit::normalize(tuple.field.collectionMarker.center, tuple.field.input));
    }

    if (gaze.size() < binocularUnknowns) {
        errorMsg = QString("Not enough calibration points (%1/%2).").arg(gaze.size()).arg(binocularUnknowns);
        return false;
    }

    binocularCalibrated = calibrate(plType, leftPupil.x, leftPupil.y, rightPupil.x, rightPupil.y, gaze.x, bcx, errorMsg)
        && calibrate(plType, leftPupil.x, leftPupil.y, rightPupil.x, rightPupil.y, gaze.y, bcy, errorMsg);

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    if (!binocularCalibrated)
        return binocularCalibrated;

    const auto& ref = calibrationTuples[0];
    float threshold = 0.01f * hypot(ref.field.width, ref.field.height);
    vector<CollectionTuple> inliers;
    for (const auto& tuple : calibrationTuples) {
        GazeEstimate l, r, b;
        l.pupilConfidence = tuple.lEye.pupil.confidence;
        r.pupilConfidence = tuple.rEye.pupil.confidence;
        estimateBinocular2d(tuple, l, r, b);
        Point2f gt = { tuple.field.collectionMarker.center.x, tuple.field.collectionMarker.center.y };
        if (cv::norm(gt - b.gp) < threshold)
            inliers.push_back(tuple);
    }

    binocularCalibrated = calibrateWithInliers(inliers, errorMsg);
Thiago Santini's avatar
Thiago Santini committed
56 57 58
    return binocularCalibrated;
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
bool BinocularPolyFit::calibrateWithInliers(std::vector<CollectionTuple>& calibrationTuples, QString& errorMsg)
{
    if (!PolyFit::calibrate(calibrationTuples, errorMsg))
        return false;

    PointVector leftPupil;
    PointVector rightPupil;
    PointVector gaze;

    for (const auto& tuple : calibrationTuples) {
        leftPupil.insert(PolyFit::normalize(tuple.lEye.pupil.center, tuple.lEye.input));
        rightPupil.insert(PolyFit::normalize(tuple.rEye.pupil.center, tuple.rEye.input));
        gaze.insert(PolyFit::normalize(tuple.field.collectionMarker.center, tuple.field.input));
    }

    if (gaze.size() < binocularUnknowns) {
        errorMsg = QString("Not enough calibration points (%1/%2).").arg(gaze.size()).arg(binocularUnknowns);
        return false;
    }

    return calibrate(plType, leftPupil.x, leftPupil.y, rightPupil.x, rightPupil.y, gaze.x, bcx, errorMsg)
        && calibrate(plType, leftPupil.x, leftPupil.y, rightPupil.x, rightPupil.y, gaze.y, bcy, errorMsg);
}

Thiago Santini's avatar
Thiago Santini committed
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
bool BinocularPolyFit::calibrate(const PlType& type, const Mat& lx, const Mat& ly, const Mat& rx, const Mat& ry, const Mat& z, Mat1d& c, QString& errorMsg)
{

    Mat ones = Mat::ones(lx.rows, 1, DataType<double>::type);
    Mat lpl = buildMat(type, lx, ly);
    Mat rpl = buildMat(type, rx, ry);
    Mat tmp;
    Mat A;
    hconcat(ones, lpl, tmp);
    hconcat(tmp, rpl, A);

    z.convertTo(z, A.type());

    bool result = true;
    try {
        result &= solve(A, z, c, DECOMP_SVD);
    } catch (...) {
        errorMsg = "cv::solve exception";
        result = false;
    }

    if (result) {
        // TODO: is this still necessary?
        for (int i = 0; i < c.rows; i++)
            if (std::isnan(c(i))) {
                errorMsg = "found NaN coefficients.";
                result = false;
            }
    }

    return result;
}

void BinocularPolyFit::estimateBinocular2d(const DataTuple& tuple, GazeEstimate& left, GazeEstimate& right, GazeEstimate& binocular)
{
    (void)tuple;
    bool leftValid = left.pupilConfidence >= minPupilConfidence;
    bool rightValid = right.pupilConfidence >= minPupilConfidence;

    auto evaluate = [&](DataTuple tuple, const Mat1d& c) {
        Point2d nlp = PolyFit::normalize(tuple.lEye.pupil.center, tuple.lEye.input);
        Point2d nrp = PolyFit::normalize(tuple.rEye.pupil.center, tuple.rEye.input);
        double res = c.at<double>(0); // constant contribution
        res += PolyFit::evaluateVariable(plType, nlp.x, nlp.y, c.rowRange(1, PolyFit::unknowns)); // left eye contribution
        res += PolyFit::evaluateVariable(plType, nrp.x, nrp.y, c.rowRange(PolyFit::unknowns, binocularUnknowns)); // right eye contribution
        return res;
    };

    if (leftValid && rightValid && binocularCalibrated) {
        binocular.gp = { static_cast<float>(tuple.field.width * evaluate(tuple, bcx)), static_cast<float>(tuple.field.height * evaluate(tuple, bcy)) };
        binocular.valid = true;
        binocular.pupilConfidence = 0.5 * (left.pupilConfidence + right.pupilConfidence);
    } else {
        if (leftValid)
            binocular = left;
        if (rightValid)
            binocular = right;
    }
}