-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecogniserKNearest.cpp
More file actions
175 lines (151 loc) · 4.1 KB
/
RecogniserKNearest.cpp
File metadata and controls
175 lines (151 loc) · 4.1 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <fstream>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include "RecogniserKNearest.h"
#include "debug.h"
using namespace std;
using namespace cv;
namespace ocr {
unsigned int RecogniserKNearest::MAX_DISTANCE = 3000000; // 600000;
RecogniserKNearest::RecogniserKNearest(const char *filename)
{
if (filename) {
loadTrainingData(filename);
}
}
RecogniserKNearest::~RecogniserKNearest()
{
}
/**
* Prepare an image of a digit to work as a sample for the model.
*/
Mat RecogniserKNearest::prepareSample(const Mat& img, bool black_on_white) {
#if 0
display_image("prepareSample input", img);
#endif
Mat grey;
if (img.channels() == 1) {
grey = img;
} else {
cvtColor(img, grey, COLOR_BGR2GRAY);
}
if (black_on_white) {
grey = 255 - grey;
}
Mat roi, sample;
resize(grey, roi, Size(14, 14));
threshold(roi, roi, 0, 255, THRESH_BINARY | THRESH_OTSU);
#if 0
display_image("prepareSample 10x10", roi);
#endif
roi.reshape(1, 1).convertTo(sample, CV_32F);
#if 0
display_image("prepareSample sample", sample);
#endif
return sample;
}
/**
* Learn a single digit.
*/
void RecogniserKNearest::learn(const Mat & img, char key) {
#if 0
cerr << key << endl;
imshow("Learn", img);
waitKey(0);
#endif
_responses.push_back(Mat(1, 1, CV_32F, (float) key - '0'));
_samples.push_back(prepareSample(img));
}
/**
* Learn a vector of digits.
*/
void RecogniserKNearest::learn(const std::vector<cv::Mat>& images, const std::string &answers) {
std::vector<cv::Mat>::const_iterator it;
std::string::const_iterator answer_it = answers.begin();
for (it = images.begin(), answer_it = answers.begin();
it < images.end(); ++it, ++answer_it) {
learn(*it, *answer_it);
}
}
/**
* Initialize the model.
*/
void RecogniserKNearest::initModel() {
_pModel = new CvKNearest(_samples, _responses);
}
/**
* Recognize a single digit.
*/
char RecogniserKNearest::recognise(const Mat& img, bool black_on_white) {
#if 0
cerr << "Sample count: " << _pModel->get_sample_count() << "; var count: " <<_pModel->get_var_count() << endl;
#endif
char cres = '?';
Mat results, neighborResponses, dists;
Mat sample = prepareSample(img, black_on_white);
#if 0
display_image("recognize sample", sample);
#endif
float result = _pModel->find_nearest(
sample, 2, results, neighborResponses, dists);
#if 0
cerr << "results: " << results << endl;
cerr << "neighborResponses: " << neighborResponses << endl;
cerr << "dists: " << dists << endl;
#endif
if (/*0 == int(neighborResponses.at<float>(0, 0) - neighborResponses.at<float>(0, 1))
&&*/ dists.at<float>(0, 0) < MAX_DISTANCE) {
cres = (int) result + '0';
// rlog << log4cpp::Priority::DEBUG << "recog succes: " << cres;
} else {
// rlog << log4cpp::Priority::DEBUG << "recog fail: " << dists.at<float>(0, 0) << ", " << MAX_DISTANCE;
}
return cres;
}
/**
* Save training data to file.
*/
void RecogniserKNearest::saveTrainingData(const char *filename)
{
FileStorage fs(filename, FileStorage::WRITE);
fs << "samples" << _samples;
fs << "responses" << _responses;
fs.release();
}
/**
* Load training data from file and init model.
*/
void RecogniserKNearest::loadTrainingData(const char *filename)
{
FileStorage fs(filename, FileStorage::READ);
if (fs.isOpened()) {
fs["samples"] >> _samples;
fs["responses"] >> _responses;
fs.release();
initModel();
}
}
void RecogniserKNearest::learnOcr(VideoCapture &pImageInput, const string &answers, const char *filename) {
if (!pImageInput.isOpened()) {
throw "Image source not open";
}
RecogniserKNearest ocr;
string::const_iterator answer_iter = answers.begin();
for (;;) {
Mat img;
if (!pImageInput.read(img)) {
break;
}
if (!img.data) {
break;
}
#if 0
display_image("learnOcr", img);
#endif
ocr.learn(img, *answer_iter);
answer_iter++;
}
ocr.saveTrainingData(filename);
}
}