-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknearest.cpp
76 lines (61 loc) · 2.09 KB
/
knearest.cpp
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
#include "knearest.h"
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
using std::cerr;
using std::endl;
using std::function;
namespace {
static const int kOcrMaxDistance = 60000;
}
KNearest::KNearest(function<char(const cv::Mat&)> recognise_callback)
: model_(cv::ml::KNearest::create()),
recognise_callback_(recognise_callback) {}
void KNearest::Learn(const cv::Mat& image, char c) {
responses_.push_back(cv::Mat(1, 1, CV_32F, (float)c));
samples_.push_back(PrepareSample(image));
}
void KNearest::Train() {
model_->train(cv::InputArray(samples_), cv::ml::ROW_SAMPLE,
cv::InputArray(responses_));
}
char KNearest::Recognise(const cv::Mat& image) const {
cv::Mat results;
cv::Mat neighbour_responses;
cv::Mat distances;
float result = model_->findNearest(cv::InputArray(PrepareSample(image)), 1,
results, neighbour_responses, distances);
if (!recognise_callback_) {
// Just assume it's correct if there's no one to ask.
return static_cast<char>(result);
}
if (static_cast<int>(neighbour_responses.at<float>(0, 0) -
neighbour_responses.at<float>(0, 1)) == 0 &&
distances.at<float>(0, 0) < kOcrMaxDistance) {
// Pretty certain about this result so just return it.
return static_cast<char>(result);
}
// A bit fuzzy about this answer so ask someone.
return recognise_callback_(image);
}
cv::Mat KNearest::PrepareSample(const cv::Mat& image) const {
cv::Mat roi;
cv::resize(image, roi, cv::Size(20, 20));
cv::Mat sample;
roi.reshape(1, 1).convertTo(sample, CV_32F);
return sample;
}
void KNearest::Save(const std::string& path) { model_->save(path); }
bool KNearest::Load(const std::string& path) {
model_ = cv::ml::KNearest::load<cv::ml::KNearest>(path);
if (!model_) {
cerr << "Failed to load KNearest model" << endl;
}
return model_;
}
bool KNearest::LoadFromString(const std::string& model) {
model_ = cv::ml::KNearest::loadFromString<cv::ml::KNearest>(model);
if (!model_) {
cerr << "Failed to load KNearest model" << endl;
}
return model_;
}