Commit 88745d57 authored by Thiago Santini's avatar Thiago Santini

MOdernizing a bit the pupil detection and tracking interfaces

parent 26dc3a6c
......@@ -16,15 +16,15 @@ Rect PupilDetectionMethod::coarsePupilDetection(const Mat& frame, const float& m
Mat downscaled;
resize(frame, downscaled, Size(), 1 / r, 1 / r, CV_INTER_LINEAR);
int ystep = static_cast<int>(ceil(max(0.01f * downscaled.rows, 1.0f)));
int xstep = static_cast<int>(ceil(max(0.01f * downscaled.cols, 1.0f)));
auto ystep = static_cast<int>(ceil(max(0.01f * downscaled.rows, 1.0f)));
auto xstep = static_cast<int>(ceil(max(0.01f * downscaled.cols, 1.0f)));
float d = static_cast<float>(sqrt(pow(downscaled.rows, 2) + pow(downscaled.cols, 2)));
auto d = static_cast<float>(sqrt(pow(downscaled.rows, 2) + pow(downscaled.cols, 2)));
// Pupil radii is based on PuRe assumptions
int min_r = static_cast<int>((0.5 * 0.07 * d));
int max_r = static_cast<int>((0.5 * 0.29 * d));
int r_step = static_cast<int>(max(0.2f * (max_r + min_r), 1.0f));
auto min_r = static_cast<int>((0.5 * 0.07 * d));
auto max_r = static_cast<int>((0.5 * 0.29 * d));
auto r_step = static_cast<int>(max(0.2f * (max_r + min_r), 1.0f));
// TODO: padding so we consider the borders as well!
......@@ -89,7 +89,7 @@ Rect PupilDetectionMethod::coarsePupilDetection(const Mat& frame, const float& m
if (response > res.ptr<float>(y)[x]) {
res.ptr<float>(y)[x] = response;
// The pupil is too small, the padding too large; we combine them.
candidates.push_back(make_pair(Rect(0.5 * (ia + oa), 0.5 * (ic + oc)), response));
candidates.emplace_back(make_pair(Rect(0.5 * (ia + oa), 0.5 * (ic + oc)), response));
}
}
}
......@@ -107,10 +107,10 @@ Rect PupilDetectionMethod::coarsePupilDetection(const Mat& frame, const float& m
// Now add until we reach the minimum coverage or run out of candidates
Rect coarse;
int minWidth = static_cast<int>(minCoverage * downscaled.cols);
int minHeight = static_cast<int>(minCoverage * downscaled.rows);
for (unsigned int i = 0; i < candidates.size(); i++) {
auto& c = candidates[i];
auto minWidth = static_cast<int>(minCoverage * downscaled.cols);
auto minHeight = static_cast<int>(minCoverage * downscaled.rows);
//for (unsigned int i = 0; i < candidates.size(); i++) {
for (auto& c : candidates) {
if (coarse.area() == 0)
coarse = c.first;
else
......@@ -167,22 +167,21 @@ float PupilDetectionMethod::outlineContrastConfidence(const Mat& frame, const Pu
int validCount = 0;
vector<Point> outlinePoints = ellipse2Points(pupil, 10);
for (auto p = outlinePoints.begin(); p != outlinePoints.end(); p++) {
Point& pc = *p;
float dx = pupil.center.x - p->x;
float dy = pupil.center.y - p->y;
for (auto p : outlinePoints) {
float dx = pupil.center.x - p.x;
float dy = pupil.center.y - p.y;
float r = delta / sqrt(pow(dx, 2) + pow(dy, 2));
Point2f d = { roundf(r * dx), roundf(r * dy) };
Point2f start = Point2f(*p) - d;
Point2f end = Point2f(*p) + d;
Point2f start = Point2f(p) - d;
Point2f end = Point2f(p) + d;
evaluated++;
if (!(boundaries.contains(start) && boundaries.contains(end)))
continue;
LineIterator inner(frame, start, pc);
LineIterator outer(frame, pc, end);
LineIterator inner(frame, start, p);
LineIterator outer(frame, p, end);
float innerMean = 0;
for (int i = 0; i < inner.count; i++, ++inner)
......@@ -226,14 +225,14 @@ float PupilDetectionMethod::angularSpreadConfidence(const vector<Point>& points,
std::bitset<4> anchorPointSlices;
anchorPointSlices.reset();
for (auto p = points.begin(); p != points.end(); p++) {
if (p->x - center.x < 0) {
if (p->y - center.y < 0)
for (auto p : points) {
if (p.x - center.x < 0) {
if (p.y - center.y < 0)
anchorPointSlices.set(Q0);
else
anchorPointSlices.set(Q3);
} else {
if (p->y - center.y < 0)
if (p.y - center.y < 0)
anchorPointSlices.set(Q1);
else
anchorPointSlices.set(Q2);
......@@ -269,7 +268,7 @@ float PupilDetectionMethod::edgeRatioConfidence(const Mat& edgeImage, const Pupi
*/
float PupilDetectionMethod::ellipseDistanceConfidence(const Pupil& pupil, const std::vector<cv::Point>& edgePoints, std::vector<cv::Point>& validPoints, const int& dist)
{
if (!pupil.valid() || edgePoints.size() == 0)
if (!pupil.valid() || edgePoints.empty())
return Pupil::NoConfidence;
vector<double> distances;
distFromPoints(pupil, edgePoints, distances);
......
......@@ -52,7 +52,7 @@ void PupilTrackingMethod::detectAndTrack(const Timestamp& ts, const cv::Mat& fra
pupil = invokeDetection(frame, estimateTemporalROI(ts, roi), pupilDetectionMethod, minPupilDiameterPx, maxPupilDiameterPx);
// TODO: keep?
// If detection failed, try tracking with old
if (pupil.confidence < minDetectionConfidence && previousPupils.size() > 0) {
if (pupil.confidence < minDetectionConfidence && !previousPupils.empty()) {
track(frame, roi, previousPupils.back(), alternative, minPupilDiameterPx, maxPupilDiameterPx);
if (alternative.confidence > pupil.confidence)
pupil = alternative;
......@@ -84,8 +84,6 @@ void PupilTrackingMethod::detectAndTrack(const Timestamp& ts, const cv::Mat& fra
pupil.confidence = 0;
registerPupil(ts, pupil);
return;
}
Pupil PupilTrackingMethod::invokeDetection(const cv::Mat& frame, const cv::Rect& roi, std::shared_ptr<PupilDetectionMethod> pupilDetectionMethod, const float& minPupilDiameterPx, const float& maxPupilDiameterPx)
......@@ -98,7 +96,7 @@ Pupil PupilTrackingMethod::invokeDetection(const cv::Mat& frame, const cv::Rect&
cv::Rect PupilTrackingMethod::estimateTemporalROI(const Timestamp& ts, const cv::Rect& roi)
{
Rect trackingRect = roi;
if (previousPupils.size() > 0) {
if (!previousPupils.empty()) {
const auto& mostRecent = previousPupils.back();
auto trackingRectHalfSide = std::max<int>(mostRecent.size.width, mostRecent.size.height);
const auto remaining = 0.5 * std::max<int>(roi.width, roi.height) - trackingRectHalfSide;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment