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