EyeImageProcessor.cpp 4.36 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3 4 5 6 7 8 9 10
#include "EyeImageProcessor.h"
#include <opencv2/highgui.hpp>

using namespace cv;

static int gEyeDataId = qRegisterMetaType<EyeData>("EyeData");

EyeImageProcessor::EyeImageProcessor(QString id, QObject *parent)
    : id(id),
      pupilDetectionMethod(NULL),
11 12
	  pupilTrackingMethod(NULL),
	  sROI(QPointF(0,0)),
Thiago Santini's avatar
Thiago Santini committed
13 14 15
      eROI(QPointF(1,1)),
      QObject(parent)
{
16 17 18
	availablePupilDetectionMethods.push_back(new PuRe());
	availablePupilDetectionMethods.push_back(new ElSe());
	availablePupilDetectionMethods.push_back(new ExCuSe());
Thiago Santini's avatar
Thiago Santini committed
19 20 21 22 23 24 25 26
#ifdef STARBURST
    availablePupilDetectionMethods.push_back(new Starburst());
#endif
#ifdef SWIRSKI
    availablePupilDetectionMethods.push_back(new Swirski());
#endif
    settings = new QSettings(gCfgDir + "/" + id + " ImageProcessor", QSettings::IniFormat);
    updateConfig();
27

28 29
	pmIdx = gPerformanceMonitor.enrol(id, "Image Processor");

Thiago Santini's avatar
Thiago Santini committed
30
	pupilTrackingMethod = new PuReST();
Thiago Santini's avatar
Thiago Santini committed
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
}

void EyeImageProcessor::updateConfig()
{
    QMutexLocker locker(&cfgMutex);
    cfg.load(settings);

    pupilDetectionMethod = NULL;
    for (int i=0; i<availablePupilDetectionMethods.size(); i++)
        if (cfg.pupilDetectionMethod == QString(availablePupilDetectionMethods[i]->description().c_str()) )
            pupilDetectionMethod = availablePupilDetectionMethods[i];
}

EyeImageProcessor::~EyeImageProcessor()
{
    for (int i=0; i<availablePupilDetectionMethods.size(); i++)
        delete availablePupilDetectionMethods[i];
    availablePupilDetectionMethods.clear();

    if(settings)
        settings->deleteLater();
}

void EyeImageProcessor::process(Timestamp timestamp, const Mat &frame)
{
    // TODO: parametrize frame drop due to lack of processing power
Thiago Santini's avatar
Thiago Santini committed
57 58
	if ( gPerformanceMonitor.shouldDrop(pmIdx, gTimer.elapsed() - timestamp, 50) )
		return;
Thiago Santini's avatar
Thiago Santini committed
59 60 61 62 63

    QMutexLocker locker(&cfgMutex);

    data.timestamp = timestamp;

Thiago Santini's avatar
Thiago Santini committed
64 65 66 67 68 69 70 71
	Q_ASSERT_X(frame.data != data.input.data, "Eye Image Processing", "Previous and current input image matches!");
	if (cfg.inputSize.width > 0 && cfg.inputSize.height > 0) {
		data.input = Mat(cfg.inputSize, frame.type() );
		resize(frame, data.input, cfg.inputSize);
	}
	else {
		data.input = frame;
	}
Thiago Santini's avatar
Thiago Santini committed
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	if (cfg.flip != CV_FLIP_NONE)
		flip(data.input, data.input, cfg.flip);

	if (data.input.channels() > 1) // TODO: make it algorithm dependent
		cvtColor(data.input, data.input, CV_BGR2GRAY);

	data.pupil = Pupil();
	data.validPupil = false;
	if (pupilDetectionMethod != NULL)  {
		Rect userROI = Rect(
				Point(sROI.x() * data.input.cols, sROI.y() * data.input.rows),
				Point( eROI.x() * data.input.cols, eROI.y() * data.input.rows)
			);

		float scalingFactor = 1;
		if (cfg.processingDownscalingFactor > 1)
			scalingFactor = 1.0 / cfg.processingDownscalingFactor;

		/*
		 *  From here on, our reference frame is the scaled user ROI
		 */
		Mat downscaled;
		resize(data.input(userROI), downscaled, Size(),
			   scalingFactor, scalingFactor,
			   INTER_AREA);
		Rect coarseROI = {0, 0, downscaled.cols, downscaled.rows };

		// If the user wants a coarse location and the method has none embedded,
		// we further constrain the search using the generic one
		if (!pupilDetectionMethod->hasCoarseLocation() && cfg.coarseDetection) {
103
			coarseROI = PupilDetectionMethod::coarsePupilDetection( downscaled, 0.5f );
104 105 106 107 108 109 110
			data.coarseROI = Rect(
								 userROI.tl() + coarseROI.tl() / scalingFactor,
								 userROI.tl() + coarseROI.br() / scalingFactor
							);
		} else
			data.coarseROI = Rect();

111
		if (cfg.tracking && pupilTrackingMethod) {
112 113
			pupilTrackingMethod->run(timestamp, downscaled, coarseROI, data.pupil, *pupilDetectionMethod);
		} else {
114
			pupilDetectionMethod->run( downscaled, coarseROI, data.pupil );
115
			// TODO: expose this to the user
116 117
			if ( ! pupilDetectionMethod->hasConfidence() )
				data.pupil.confidence = PupilDetectionMethod::outlineContrastConfidence(downscaled, data.pupil);
118
		}
Thiago Santini's avatar
Thiago Santini committed
119

120 121 122 123 124 125 126 127
		if (data.pupil.center.x > 0 && data.pupil.center.y > 0) {
			// Upscale
			data.pupil.resize( 1.0 / scalingFactor );

			// User region shift
			data.pupil.shift( userROI.tl() );
			data.validPupil = true;
		}
Thiago Santini's avatar
Thiago Santini committed
128 129 130

    }

Thiago Santini's avatar
Thiago Santini committed
131
	data.processingTimestamp = gTimer.elapsed() - data.timestamp;
Thiago Santini's avatar
Thiago Santini committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    emit newData(data);
}

void EyeImageProcessor::newROI(QPointF sROI, QPointF eROI)
{
    QMutexLocker locker(&cfgMutex);
    if (sROI.isNull() || eROI.isNull()) {
        this->sROI = QPointF(0,0);
        this->eROI = QPointF(1,1);
    } else {
        this->sROI = sROI;
        this->eROI = eROI;
    }
}