...
 
Commits (1)
......@@ -30,6 +30,9 @@
#include "ScreenCameraInfo.h"
#include "KalmanFilter.h"
#include "MousePositionProvider.h"
#include "Recorder.h"
#include "RecordManager.h"
#define debugcout(x) std::cout<<#x<<x<<std::endl
......
#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