-
Notifications
You must be signed in to change notification settings - Fork 37
/
1349-2.cpp
95 lines (63 loc) · 2.3 KB
/
1349-2.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
91
92
93
94
95
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
typedef struct _PointInfo
{
int x;
int y;
int sad;
}PointInfo;
int main()
{
Mat img_color = imread("image.jpg", IMREAD_COLOR);
Mat img_original;
cvtColor(img_color, img_original, COLOR_BGR2GRAY);
Mat img_template = imread("template.jpg", IMREAD_GRAYSCALE);
int original_h = img_original.rows;
int original_w = img_original.cols;
int template_h = img_template.rows;
int template_w = img_template.cols;
int best_position_y = 0;
int best_position_x = 0;
int best_position_sad = 100000;
vector<PointInfo> point;
// 원본 이미지 스캔
for ( int original_y = 0; original_y < original_h - template_h; original_y++)
{
uchar *data_original = img_original.data;
for (int original_x = 0; original_x < original_w - template_w; original_x++)
{
int SAD = 0;
//템플릿 이미지 스캔
for (int template_y = 0; template_y < template_h; template_y++)
{
uchar *data_template = img_template.data;
for (int template_x = 0; template_x < template_w; template_x++)
{
int original_pixel = data_original[(original_y+template_y)*original_w + (original_x+template_x)];
int template_pixel = data_template[template_y*template_w + template_x];
SAD += abs( original_pixel - template_pixel);
}
}
// 최소 SAD 지점 찾기
if (best_position_sad > SAD)
{
best_position_sad = SAD;
best_position_y = original_y;
best_position_x = original_x;
}
PointInfo pointInfo = {original_x, original_y, SAD};
point.push_back(pointInfo);
}
}
for (int i = 0; i < point.size(); i++)
{
if (abs(best_position_sad - point[i].sad) < 100){
cout << abs(best_position_sad - point[i].sad) << endl;
rectangle(img_color, Point(point[i].x, point[i].y), Point(point[i].x+template_w, point[i].y+template_h), Scalar(255, 0, 0), 3 );;
}
}
imshow("result", img_color);
waitKey(0);
}