-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib_oc.h
executable file
·96 lines (79 loc) · 2.62 KB
/
lib_oc.h
1
2
3
4
5
6
7
8
9
10
11
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
59
60
61
62
63
64
65
66
67
68
69
70
71
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
/**
* @file lib_oc
* @brief Object Classification library
*
* Functions used to classify objects based on their shape.
* The code uses chain code histograms and features such as solidity, circularity and aspect ratio.
* Also, the code uses a kNN and logistic regression algorithms to classify objects.
*
* @author $Author: Catarina Silva $
* @version $Revision: 1.0 $
* @date $Date: 2020/09/10 $
*/
#ifndef OC_H
#define OC_H
#include "json.hpp"
#include "lib_od.h"
using json = nlohmann::json;
/**
* Features extraction class to obtain the following features: circularity, roundness, aspect ratio and solidity
*
*/
class Features {
private:
std::array<double, 8> hist;
double circularity, roundness, aspect_ratio, solidity;
friend std::ostream& operator<<(std::ostream&, const Features&);
public:
Features(const std::array<double, 8> &, const double, const double, const double, const double);
Features(const std::vector<cv::Point>&);
double distance(const Features&, const unsigned int p=2) const;
std::vector<double> get_features() const;
std::array<double, 8> get_histogram() const;
double get_circularity() const;
double get_roundness() const;
double get_aspect_ratio() const;
double get_solidity() const;
};
class ML {
public:
virtual void learn(const std::vector<std::pair<std::string, Features>>&) = 0;
virtual std::string predict(const Object&) const = 0;
virtual void store(const std::string&) const = 0;
static ML& load(const std::string&);
friend std::ostream& operator<<(std::ostream&, const ML&);
};
/**
* A kNN implementation class
*
*/
class KNN : public ML {
private:
unsigned int k, d;
std::vector<std::pair<std::string, Features>> instances;
friend std::ostream& operator<<(std::ostream&, const KNN&);
public:
KNN(const unsigned int, const unsigned int);
KNN(const unsigned int, const unsigned int, const std::vector<std::pair<std::string, Features>>&);
void learn(const std::vector<std::pair<std::string, Features>>&);
std::string predict(const Object&) const;
void store(const std::string&) const;
static KNN& load(const json&);
};
/**
* A Logistic Regression implementation class
*
*/
class LR : public ML {
private:
std::vector<double> parameters;
friend std::ostream& operator<<(std::ostream&, const LR&);
public:
LR();
LR(const std::vector<double> parameters);
void learn(const std::vector<std::pair<std::string, Features>>&);
std::string predict(const Object&) const;
void store(const std::string&) const;
static LR& load(const json&);
};
#endif