-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (70 loc) · 2.84 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "grayMatch.h"
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
auto src = cv::imread(std::string(IMG_DIR) + "/3.bmp", cv::IMREAD_GRAYSCALE);
auto dst = cv::imread(std::string(IMG_DIR) + "/h.bmp", cv::IMREAD_GRAYSCALE);
const std::string modelName("model.bin");
{
auto t0 = cv::getTickCount();
auto model = trainModel(src.data, src.cols, src.rows, src.channels(), int(src.step), 0, 0,
src.cols, src.rows, -1);
auto t1 = cv::getTickCount();
// get size
int size;
serialize(model, nullptr, &size);
// serialize to buffer
std::vector<uchar> buffer(size);
serialize(model, buffer.data(), &size);
// write to file
std::ofstream ofs(modelName, std::ios::binary | std::ios::out);
if (!ofs.is_open()) {
return -1;
}
ofs.write((const char *)buffer.data(), size);
freeModel(&model);
auto trainCost = double(t1 - t0) / cv::getTickFrequency();
std::cout << "train(s):" << trainCost << std::endl;
}
int num = 70;
std::vector<Pose> poses(num);
{
// open file
std::ifstream ifs(modelName, std::ios::binary | std::ios::in);
if (!ifs.is_open()) {
return -2;
}
// get size
ifs.seekg(0, std::ios::end);
auto size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
// read to buffer
std::vector<uchar> buffer(size);
ifs.read((char *)buffer.data(), size);
// deserialize from buffer
auto model = deserialize(buffer.data(), int(buffer.size()));
auto t2 = cv::getTickCount();
matchModel(dst.data, dst.cols, dst.rows, dst.channels(), int(dst.step), 0, 0, dst.cols,
dst.rows, model, &num, poses.data(), -1, 0, 360, 0, 0.5, 1);
auto t3 = cv::getTickCount();
auto matchCost = double(t3 - t2) / cv::getTickFrequency();
std::cout << "match(s):" << matchCost << std::endl;
}
cv::Mat color;
cv::cvtColor(dst, color, cv::COLOR_GRAY2RGB);
for (int i = 0; i < num; i++) {
auto &pose = poses[ i ];
cv::RotatedRect rect(cv::Point2f(pose.x, pose.y), src.size(), -pose.angle);
cv::Point2f pts[ 4 ];
rect.points(pts);
cv::line(color, pts[ 0 ], pts[ 1 ], cv::Scalar(255, 0, 0), 1, cv::LINE_AA);
cv::line(color, pts[ 1 ], pts[ 2 ], cv::Scalar(255, 0, 0), 1, cv::LINE_AA);
cv::line(color, pts[ 2 ], pts[ 3 ], cv::Scalar(255, 0, 0), 1, cv::LINE_AA);
cv::line(color, pts[ 3 ], pts[ 0 ], cv::Scalar(255, 0, 0), 1, cv::LINE_AA);
std::cout << pose.x << "," << pose.y << "," << pose.angle << "," << pose.score << std::endl;
}
cv::imshow("img", color);
cv::waitKey();
return 0;
}