Skip to content

Commit 568af3c

Browse files
committed
[feat] add Positioning(template matching)
1 parent d796689 commit 568af3c

File tree

4 files changed

+289
-50
lines changed

4 files changed

+289
-50
lines changed

include/component/positioning.h

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ enum PositioningRectEnums {
1515
};
1616
enum FeatureAttributeEnums {
1717
HESSIAN_THRESHOLD = 0, // 100~3000
18-
LOWE_RATIO = 1, // 0~1.0f
18+
LOWE_RATIO = 1, // 0~1.0
1919
};
2020
enum TemplateAttributeEnums {
21-
ANGLE_TOLERANCE = 0,
22-
NUMBER_OF_LEVELS = 1,
23-
THRESHOLD_SCORE = 2,
21+
ANGLE_TOLERANCE = 0, // 0~180
22+
NUMBER_OF_LEVELS = 1, // 1~5
23+
THRESHOLD_SCORE = 2, // 0~1.0
2424
};
2525

2626
class IPositioning {
2727
public:
2828
virtual int SetGoldenSampleImage(const cv::Mat &golden_sample_img) = 0;
2929
virtual int SetRect(const PositioningRectEnums &rect_type, const cv::Rect &rect) = 0;
30-
virtual int SetAttribute(const int &attribute_type, const float &value) = 0;
30+
virtual int SetAttribute(const int &attribute_type, const double &value) = 0;
3131
virtual cv::Mat GetResult(const cv::Mat &sample_img) = 0;
3232
};
3333

@@ -36,38 +36,46 @@ class FeatureMatching : public IPositioning {
3636
int SetGoldenSampleImage(const cv::Mat &golden_sample_img);
3737
int SetRect(const PositioningRectEnums &rect_type, const cv::Rect &rect);
3838
// template <typename T>
39-
int SetAttribute(const int &attribute_type, const float &value);
39+
int SetAttribute(const int &attribute_type, const double &value);
4040
cv::Mat GetResult(const cv::Mat &sample_img);
4141

4242
private:
43-
cv::Mat golden_sample_image;
43+
cv::Mat GetHomography(const cv::Mat &sample_img);
44+
45+
private:
46+
//----cv::Rect----
4447
cv::Rect template_rect;
4548
cv::Rect searching_rect;
46-
47-
int hessian_threshold;
48-
float lowe_ratio;
49+
//----cv::Mat----
50+
cv::Mat golden_sample_image;
4951
cv::Mat template_img;
5052
cv::Mat searching_img;
51-
cv::Mat GetHomography(const cv::Mat &sample_img);
53+
//----attribute----
54+
int hessian_threshold;
55+
float lowe_ratio;
5256
};
5357

54-
// class TemplateMatching : public IPositioning {
55-
// public:
56-
// int SetGoldenSampleImage(const cv::Mat &golden_sample_img);
57-
// int SetRect(const PositioningRectEnums &rect_type, const cv::Rect &rect);
58-
// // template <typename T>
59-
// int SetAttribute(const int &attribute_type, const float &value);
60-
// cv::Mat GetResult(const cv::Mat &sample_img);
61-
62-
// private:
63-
// cv::Mat golden_sample_image;
64-
// cv::Rect template_rect;
65-
// cv::Rect searching_rect;
58+
class TemplateMatching : public IPositioning {
59+
public:
60+
int SetGoldenSampleImage(const cv::Mat &golden_sample_img);
61+
int SetRect(const PositioningRectEnums &rect_type, const cv::Rect &rect);
62+
// template <typename T>
63+
int SetAttribute(const int &attribute_type, const double &value);
64+
cv::Mat GetResult(const cv::Mat &sample_img);
6665

67-
// double angle_tolerance;
68-
// int number_of_levels;
69-
// double threshold_score;
70-
// };
66+
private:
67+
//----cv::Rect----
68+
cv::Rect template_rect;
69+
cv::Rect searching_rect;
70+
//----cv::Mat----
71+
cv::Mat golden_sample_image;
72+
cv::Mat template_img;
73+
cv::Mat searching_img;
74+
//----attribute----
75+
double angle_tolerance;
76+
int number_of_levels;
77+
double similarity_score;
78+
};
7179

7280
class Creator {
7381
public:
@@ -79,12 +87,11 @@ class Positioning : public Creator {
7987
Positioning(const PositioningTypeEnums &type) {
8088
switch (type) {
8189
case PositioningTypeEnums::FEATURE_MATCHING: {
82-
this->ptr_index = 0;
8390
ptr = new FeatureMatching();
8491
break;
8592
}
8693
case PositioningTypeEnums::TEMPLATE_MATCHING: {
87-
this->ptr_index = 1;
94+
ptr = new TemplateMatching();
8895
break;
8996
}
9097
default: {
@@ -109,7 +116,7 @@ class Positioning : public Creator {
109116
}
110117
return 0;
111118
}
112-
int SetAttribute(const int &attribute_type, float value) {
119+
int SetAttribute(const int &attribute_type, const double &value) {
113120
if (ptr->SetAttribute(attribute_type, value) != 0)
114121
return -1;
115122
return 0;

main.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cv::Mat &&src = cv::imread("../images/Okonomiyaki.png");
1616
8 : positioning by feature matching(SURF)
1717
9 : positioning by template matching
1818
*/
19-
#define index 8
19+
#define index 9
2020

2121
#if index == 1
2222
// DynamicThreshold
@@ -124,4 +124,32 @@ int main() {
124124
cv::imshow("dst", dst);
125125
cv::waitKey(0);
126126
}
127+
#elif index == 9
128+
int main() {
129+
//----Load images----
130+
cv::Mat &&golden_sample = cv::imread("../images/positioning/golden_sample.jpg");
131+
cv::Mat &&sample = cv::imread("../images/positioning/sample.jpg");
132+
//----cropped rect----
133+
cv::Rect &&searching_rect = cv::Rect(126, 95, 630, 560);
134+
cv::Rect &&template_rect = cv::Rect(313, 231, 240, 206);
135+
136+
std::shared_ptr<ipo::Positioning> &&position_obj = std::make_shared<ipo::Positioning>(ipo::PositioningTypeEnums::TEMPLATE_MATCHING);
137+
if (position_obj->SetGoldenSampleImage(golden_sample) != 0)
138+
return -1;
139+
if (position_obj->SetRect(ipo::PositioningRectEnums::SEARCHING_IMG_RECT, searching_rect) != 0)
140+
return -1;
141+
if (position_obj->SetRect(ipo::PositioningRectEnums::TEMPLATE_IMG_RECT, template_rect) != 0)
142+
return -1;
143+
if (position_obj->SetAttribute(ipo::TemplateAttributeEnums::ANGLE_TOLERANCE, 30) != 0)
144+
return -1;
145+
if (position_obj->SetAttribute(ipo::TemplateAttributeEnums::NUMBER_OF_LEVELS, 2) != 0)
146+
return -1;
147+
if (position_obj->SetAttribute(ipo::TemplateAttributeEnums::THRESHOLD_SCORE, 0.8) != 0)
148+
return -1;
149+
cv::Mat &&dst = position_obj->GetResult(sample);
150+
if (dst.empty())
151+
return -1;
152+
cv::imshow("dst", dst);
153+
cv::waitKey(0);
154+
}
127155
#endif

src/component/image_proc.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ cv::Mat ImageRotateByCenter(const cv::Mat &src, const double &angle) {
434434
const int &height = src.rows;
435435

436436
cv::Point &&image_center = cv::Point(width / 2, height / 2);
437-
cv::Mat &&rotation_mat = cv::getRotationMatrix2D(image_center, angle, 1.0);
437+
cv::Mat &&rotation_mat = cv::getRotationMatrix2D(image_center, -angle, 1.0);
438438

439439
double &&abs_cos = std::abs(rotation_mat.at<double>(0, 0));
440440
double &&abs_sin = std::abs(rotation_mat.at<double>(0, 1));
@@ -447,10 +447,6 @@ cv::Mat ImageRotateByCenter(const cv::Mat &src, const double &angle) {
447447

448448
cv::Mat &&dst = cv::Mat::zeros(cv::Size(bound_w, bound_h), src.type());
449449
cv::warpAffine(src, dst, rotation_mat, cv::Size(bound_w, bound_h), cv::INTER_CUBIC);
450-
451-
// cv::imshow("SADASDASDdst", dst);
452-
// cv::waitKey(0);
453-
454450
return dst.clone();
455451
}
456452

0 commit comments

Comments
 (0)