PupilDetectionMethod.h 3.45 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3
#ifndef PUPILDETECTIONMETHOD_H
#define PUPILDETECTIONMETHOD_H

4 5 6
#include <QMetaType>
#include <QDebug>

Thiago Santini's avatar
Thiago Santini committed
7
#include <string>
8
#include <deque>
Thiago Santini's avatar
Thiago Santini committed
9
#include <bitset>
Thiago Santini's avatar
Thiago Santini committed
10 11

#include <opencv2/core.hpp>
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#define NO_CONFIDENCE -1.0
#define SMALLER_THAN_NO_CONFIDENCE NO_CONFIDENCE-1.0

class Pupil : public cv::RotatedRect {
public:
	Pupil(const RotatedRect &outline, const float &confidence) :
		RotatedRect(outline),
		confidence(confidence) {}

	Pupil(const RotatedRect &outline) :
		RotatedRect(outline),
		confidence(NO_CONFIDENCE) { }

	Pupil() { clear(); }

	float confidence;

	void clear() {
		angle = -1.0;
		center = { -1.0, -1.0 };
		size = { -1.0, -1.0 };
		confidence = NO_CONFIDENCE;
    }

	void resize(const float &xf, const float &yf) {
		center.x *= xf;
		center.y *= yf;
		size.width *= xf;
		size.height *= yf;
	}
	void resize(const float &f) {
		center *= f;
		size *= f;
	}
	void shift( cv::Point2f p ) { center += p; }

	bool valid(const double &confidenceThreshold=SMALLER_THAN_NO_CONFIDENCE) const {
		return center.x > 0 &&
			center.y > 0 &&
			size.width > 0 &&
			size.height > 0 &&
			confidence > confidenceThreshold;
	}

59 60 61 62
	bool hasOutline() const { return size.width > 0 && size.height > 0; }
	int majorAxis() const { return std::max<int>(size.width, size.height); }
	int minorAxis() const { return std::min<int>(size.width, size.height); }
	int diameter() const { return majorAxis(); }
63 64 65 66 67
	float circumference() const {
		float a = 0.5*majorAxis();
		float b = 0.5*minorAxis();
		return CV_PI * abs( 3*(a+b) - sqrt( 10*a*b + 3*( pow(a,2) + pow(b,2) ) ) );
	}
68
};
Thiago Santini's avatar
Thiago Santini committed
69

70 71
Q_DECLARE_METATYPE(Pupil);

Thiago Santini's avatar
Thiago Santini committed
72 73 74 75 76 77 78
class PupilDetectionMethod
{
public:
    PupilDetectionMethod() {}
    ~PupilDetectionMethod() {}

    virtual cv::RotatedRect run(const cv::Mat &frame) = 0;
79 80 81 82 83 84 85 86 87
	virtual bool hasConfidence() = 0;
	virtual bool hasCoarseLocation() = 0;
	std::string description() { return mDesc; }

	virtual void run(const cv::Mat &frame, Pupil &pupil) {
        pupil.clear();
		pupil = run(frame);
		pupil.confidence = 1;
    }
88
	virtual void run(const cv::Mat &frame, const cv::Rect &roi, Pupil &pupil, const float &minPupilDiameterPx=-1, const float &maxPupilDiameterPx=-1) {
89 90 91 92 93 94
		(void) roi;
		(void) minPupilDiameterPx;
		(void) maxPupilDiameterPx;
		run(frame, pupil);
	}

95
	// Pupil detection interface used in the tracking
96 97 98
	Pupil runWithConfidence(const cv::Mat &frame, const cv::Rect &roi, const float &minPupilDiameterPx=-1, const float &maxPupilDiameterPx=-1) {
		Pupil pupil;
		run(frame, roi, pupil, minPupilDiameterPx, maxPupilDiameterPx);
99 100
		if ( ! hasConfidence() )
			pupil.confidence = outlineContrastConfidence(frame, pupil);
101 102 103
		return pupil;
	}

104 105
	virtual Pupil getNextCandidate() { return Pupil(); }

106 107 108 109 110
	// Generic coarse pupil detection
	static cv::Rect coarsePupilDetection(const cv::Mat &frame, const float &minCoverage=0.5f, const int &workingWidth=80, const int &workingHeight=60);

	// Generic confidence metrics
	static float outlineContrastConfidence(const cv::Mat &frame, const Pupil &pupil, const int &bias=5);
111
	static float edgeRatioConfidence(const cv::Mat &edgeImage, const Pupil &pupil, std::vector<cv::Point> &edgePoints, const int &band=5);
Thiago Santini's avatar
Thiago Santini committed
112 113
	static float angularSpreadConfidence(const std::vector<cv::Point> &points, const cv::Point2f &center);
	static float aspectRatioConfidence(const Pupil &pupil);
Thiago Santini's avatar
Thiago Santini committed
114

115
	//Pupil test(const cv::Mat &frame, const cv::Rect &roi, Pupil pupil) { return pupil; }
Thiago Santini's avatar
Thiago Santini committed
116
protected:
117
	std::string mDesc;
Thiago Santini's avatar
Thiago Santini committed
118 119
};

120

Thiago Santini's avatar
Thiago Santini committed
121
#endif // PUPILDETECTIONMETHOD_H