From 88745d573dc1c6c064b09ca649191832bc5f83e4 Mon Sep 17 00:00:00 2001 From: Thiago Santini Date: Wed, 16 Jan 2019 14:34:42 +0100 Subject: [PATCH] MOdernizing a bit the pupil detection and tracking interfaces --- .../pupil-detection/PupilDetectionMethod.cpp | 47 +++++++++---------- .../pupil-tracking/PupilTrackingMethod.cpp | 6 +-- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/EyeRecToo/src/pupil-detection/PupilDetectionMethod.cpp b/EyeRecToo/src/pupil-detection/PupilDetectionMethod.cpp index 98a0e47..9eb8b97 100644 --- a/EyeRecToo/src/pupil-detection/PupilDetectionMethod.cpp +++ b/EyeRecToo/src/pupil-detection/PupilDetectionMethod.cpp @@ -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(ceil(max(0.01f * downscaled.rows, 1.0f))); - int xstep = static_cast(ceil(max(0.01f * downscaled.cols, 1.0f))); + auto ystep = static_cast(ceil(max(0.01f * downscaled.rows, 1.0f))); + auto xstep = static_cast(ceil(max(0.01f * downscaled.cols, 1.0f))); - float d = static_cast(sqrt(pow(downscaled.rows, 2) + pow(downscaled.cols, 2))); + auto d = static_cast(sqrt(pow(downscaled.rows, 2) + pow(downscaled.cols, 2))); // Pupil radii is based on PuRe assumptions - int min_r = static_cast((0.5 * 0.07 * d)); - int max_r = static_cast((0.5 * 0.29 * d)); - int r_step = static_cast(max(0.2f * (max_r + min_r), 1.0f)); + auto min_r = static_cast((0.5 * 0.07 * d)); + auto max_r = static_cast((0.5 * 0.29 * d)); + auto r_step = static_cast(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(y)[x]) { res.ptr(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(minCoverage * downscaled.cols); - int minHeight = static_cast(minCoverage * downscaled.rows); - for (unsigned int i = 0; i < candidates.size(); i++) { - auto& c = candidates[i]; + auto minWidth = static_cast(minCoverage * downscaled.cols); + auto minHeight = static_cast(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 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& 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& edgePoints, std::vector& validPoints, const int& dist) { - if (!pupil.valid() || edgePoints.size() == 0) + if (!pupil.valid() || edgePoints.empty()) return Pupil::NoConfidence; vector distances; distFromPoints(pupil, edgePoints, distances); diff --git a/EyeRecToo/src/pupil-tracking/PupilTrackingMethod.cpp b/EyeRecToo/src/pupil-tracking/PupilTrackingMethod.cpp index a2f3e16..bfd18c1 100644 --- a/EyeRecToo/src/pupil-tracking/PupilTrackingMethod.cpp +++ b/EyeRecToo/src/pupil-tracking/PupilTrackingMethod.cpp @@ -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, 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(mostRecent.size.width, mostRecent.size.height); const auto remaining = 0.5 * std::max(roi.width, roi.height) - trackingRectHalfSide; -- GitLab