...
 
Commits (3)
......@@ -30,6 +30,10 @@
#include "ScreenCameraInfo.h"
#include "KalmanFilter.h"
#include "MousePositionProvider.h"
#include "Recorder.h"
#include "RecordManager.h"
#include "IrisDataStorage.h"
#define debugcout(x) std::cout<<#x<<x<<std::endl
......
#include "irisDataStorage.h"
#include <fstream>
using namespace std;
using namespace cv;
void wirteIrisData(const std::string & prefix, const std::vector<cv::Mat> & matVec, const std::vector<std::vector<cv::Point2f>> & contourVec, const double & eyeRadius, const std::vector<cv::Vec3d> & eyeCenters, const std::vector<cv::RotatedRect> & eyeRotatedRects, const cv::Matx44d & transformFaceToFrontCamera, const cv::Matx33d & cameraMatrix) {
int count = matVec.size();
for (int i = 0; i < count; i++) {
string filename = prefix + std::to_string(i) + "_irisData.txt";
string picname = prefix + std::to_string(i) + "_eyeImages.jpg";
imwrite(picname,matVec[i]);
ofstream out(filename, ios::trunc);
out << contourVec[i].size() << endl;
for (Point2f j : contourVec[i]) {
out << j.x << " " << j.y << " ";
}
out << endl;
out << eyeRadius << endl;
out << eyeCenters[i][0] << " " << eyeCenters[i][1] << " " << eyeCenters[i][2] << endl;
out << eyeRotatedRects[i].center.x << " " << eyeRotatedRects[i].center.y << " ";
out << eyeRotatedRects[i].size.width << " " << eyeRotatedRects[i].size.height << " ";
out << eyeRotatedRects[i].angle << endl;
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
out << transformFaceToFrontCamera.val[j*4 + k] << " ";
}
}
out << endl;
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
out << cameraMatrix.val[j * 3 + k] << " ";
}
}
out << endl;
out.close();
}
}
bool fileExists(std::string filename) {
ifstream fin(filename);
if (!fin)
return false;
else
return true;
}
bool readIrisData(const std::string & prefix, std::vector<cv::Mat> & matVec, std::vector<std::vector<cv::Point2f>> & contourVec, double & eyeRadius, std::vector<cv::Vec3d> & eyeCenters, std::vector<cv::RotatedRect> & eyeRotatedRects, cv::Matx44d & transformFaceToFrontCamera, cv::Matx33d & cameraMatrix) {
int i = 0;
matVec = std::vector<cv::Mat>();
contourVec = std::vector<std::vector<cv::Point2f>>();
eyeCenters = std::vector<cv::Vec3d>();
eyeRotatedRects = std::vector<cv::RotatedRect>();
transformFaceToFrontCamera = cv::Matx44d::zeros();
if (!fileExists(prefix + std::to_string(i) + "_irisData.txt") && fileExists(prefix + std::to_string(i) + "_eyeImages.jpg")) {
return false;
}
while (fileExists(prefix + std::to_string(i) + "_irisData.txt") && fileExists(prefix + std::to_string(i) + "_eyeImages.jpg")) {
Mat tmp = imread(prefix + std::to_string(i) + "_eyeImages.jpg");
matVec.push_back(tmp);
ifstream out(prefix + std::to_string(i) + "_irisData.txt");
int size = 0;
out >> size;
std::vector<cv::Point2f> points(0);
for (int j = 0; j < size;j++) {
float x, y;
out >> x >> y;
points.push_back(Point2f(x,y));
}
contourVec.push_back(points);
out >> eyeRadius;
double x, y, z;
out >> x >> y >> z;
eyeCenters.push_back(Vec3d(x, y, z));
float t1, t2, angle;
out >> t1 >> t2;
Point2f center(t1,t2);
out >> t1 >> t2;
Size2f sizet(t1, t2);
out >> angle;
eyeRotatedRects.push_back(RotatedRect(center, sizet, angle));
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
out >> transformFaceToFrontCamera.val[j * 4 + k];
}
}
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
out >> cameraMatrix.val[j * 3 + k];
}
}
out.close();
i++;
}
return true;
}
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
void wirteIrisData(const std::string & prefix, const std::vector<cv::Mat> & matVec, const std::vector<std::vector<cv::Point2f>> & contourVec, const double & eyeRadius, const std::vector<cv::Vec3d> & eyeCenters, const std::vector<cv::RotatedRect> & eyeRotatedRects, const cv::Matx44d & transformFaceToFrontCamera, const cv::Matx33d & cameraMatrix);
bool readIrisData(const std::string & prefix, std::vector<cv::Mat> & matVec, std::vector<std::vector<cv::Point2f>> & contourVec, double & eyeRadius, std::vector<cv::Vec3d> & eyeCenters, std::vector<cv::RotatedRect> & eyeRotatedRects, cv::Matx44d & transformFaceToFrontCamera, cv::Matx33d & cameraMatrix);
#include "KalmanFilter.h"
#include <opencv2/opencv.hpp>
#include "Kalmanfilter.h"
KalmanFilter::KalmanFilter(int number, int dtype) {
int depth = CV_MAT_DEPTH(dtype);
......
#pragma once
#include <vector>
#include <CoreOpenCVExtensions/CoreOpenCVExtensions.h>
#include <opencv2/opencv.hpp>
class KalmanFilter
{
......
#include <atomic>
#include "MousePositionProvider.h"
using namespace cv;
Point currentMousePosition = Point(-1,-1);
void onmouse(int event, int x, int y, int flags, void* param) {
if (event == CV_EVENT_MOUSEMOVE) {
currentMousePosition = Point(x, y);
}
}
void setWindowNameOfMouse(string title) {
setMouseCallback(title,onmouse);
}
Point getMousePosition() {
return currentMousePosition;
}
#pragma once
#include <opencv2/opencv.hpp>
using namespace std;
cv::Point getMousePosition();
void setWindowNameOfMouse(string);
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
#include <thread>
#include <string>
#include <vector>
using namespace std;
class RecordManager {
private:
vector<string> recordName;
vector<std::function<string()>> recordGetter;
public:
void addRecorder(string name, std::function<string(void)> getter) {
recordName.push_back(name);
recordGetter.push_back(getter);
}
vector<string> getStatus() {
vector<string> result;
getStatus(result);
return result;
}
void getStatus(vector<string> & target) {
for (int i = 0; i < recordName.size(); i++) {
target.push_back(recordName[i] + ":" + recordGetter[i]());
}
}
void drawOnTopRight(cv::Mat & canvas) {
vector<string> items;
getStatus(items);
int offsetLeft = 0;
int offsetTop = 0;
double fontScale = 1.0;
int thickness = 2;
for (string i : items) {
cv::Size stringSize = getTextSize(cv::String(i), CV_FONT_HERSHEY_SIMPLEX, fontScale, thickness, 0);
offsetLeft = 5;
offsetTop += stringSize.height+5;
putText(canvas, cv::String(i), cv::Point(offsetLeft, offsetTop), CV_FONT_HERSHEY_SIMPLEX, fontScale, cv::Scalar(0, 0, 0), thickness );
}
}
};
\ No newline at end of file
#pragma once
#include <opencv2/opencv.hpp>
#include <string>
#include <list>
#include <sstream>
using namespace std;
template <class T>
class Recorder
{
public:
virtual void push(T value)=0;
virtual T get() = 0;
virtual string toString()=0;
Recorder() {};
virtual ~Recorder() {};
};
template <class T>
class NormalRecorder : public Recorder<T>
{
private:
T currentValue;
public:
void push(T value) {
currentValue = value;
}
T get() {
return currentValue;
}
string toString() {
stringstream buffer;
buffer << currentValue;
return buffer.str();
}
};
template <class T>
class VarianceRecorder : public Recorder<T>
{
private:
list<T> currentValues;
T variance;
int listSize = 5;
bool caculated = false;
void caculate() {
T averageValue = 0;
for (T i : currentValues) {
averageValue = averageValue + i / double(currentValues.size());
}
T tmpVariance = 0;
for (T i : currentValues) {
tmpVariance = tmpVariance + (i - averageValue) * (i - averageValue) / double(currentValues.size());
}
variance = tmpVariance;
caculated = true;
}
public:
VarianceRecorder() {}
int getListSize() {
return listSize;
}
void setListSize(int value) {
listSize = value > 0 ? value : 0;
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
void push(T value) {
currentValues.push_back(value);
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
T get(){
if (caculated) {
return variance;
}
else {
caculate();
return variance;
}
}
string toString() {
get();
stringstream buffer;
buffer << variance;
return buffer.str();
}
};
template <>
class VarianceRecorder<cv::Vec2d> : public Recorder<cv::Vec2d>
{
private:
list<cv::Vec2d> currentValues;
double variance;
int listSize = 5;
bool caculated = false;
bool caculateAsVector = false;
void caculate() {
if (caculateAsVector) {
double averageDistance = 0;
for (cv::Vec2d i : currentValues) {
averageDistance += sqrt(i[0] * i[0] + i[1] * i[1]) / double(currentValues.size());
}
double tmpVariance = 0;
for (cv::Vec2d i : currentValues) {
tmpVariance += (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) * (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) / double(currentValues.size());
}
variance = tmpVariance;
}
else {
cv::Vec2d averageValue = 0;
for (cv::Vec2d i : currentValues) {
averageValue = averageValue + i / double(currentValues.size());
}
double averageDistance = 0;
for (cv::Vec2d vec : currentValues) {
cv::Vec2d i = vec - averageValue;
averageDistance += sqrt(i[0] * i[0] + i[1] * i[1]) / double(currentValues.size());
}
double tmpVariance = 0;
for (cv::Vec2d vec : currentValues) {
cv::Vec2d i = vec - averageValue;
tmpVariance += (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) * (sqrt(i[0] * i[0] + i[1] * i[1]) - averageDistance) / double(currentValues.size());
}
variance = tmpVariance;
}
caculated = true;
}
public:
VarianceRecorder() {}
VarianceRecorder(int size) :Recorder(), listSize(size) {}
int getListSize() {
return listSize;
}
void setListSize(int value) {
listSize = value > 0 ? value : 0;
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
void push(cv::Vec2d value) {
currentValues.push_back(value);
while (currentValues.size() > listSize) {
currentValues.pop_front();
}
caculated = false;
}
cv::Vec2d get() {
if (caculated) {
return variance;
}
else {
caculate();
return variance;
}
}
string toString() {
get();
stringstream buffer;
buffer << variance;
return buffer.str();
}
void caculatingAsPoint() {
caculateAsVector = false;
}
void caculatingAsVector() {
caculateAsVector = true;
}
};
\ No newline at end of file