-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHTSerial.cpp
More file actions
160 lines (134 loc) · 5.71 KB
/
GHTSerial.cpp
File metadata and controls
160 lines (134 loc) · 5.71 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
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <opencv2/opencv.hpp>
#include <chrono>
using namespace cv;
using namespace std;
// ==================================================
// Hyperparameters
// ==================================================
const int ROTATION_LEVELS = 360; // Number of rotation levels (1-degree resolution)
const int ACCUMULATOR_DEPTH = 2; // Resolution of the accumulator (scaling factor)
const int CANNY_LOW_THRESHOLD = 30; // Canny edge detection low threshold
const int CANNY_HIGH_THRESHOLD = 110; // Canny edge detection high threshold
const int VOTE_THRESHOLD = 40; // Accumulator threshold for object detection
const int MIN_DISTANCE = 80; // Minimum distance between detected objects
// ==================================================
typedef map<int, vector<Point>> RTable; // R-Table: maps quantized angles to displacement vectors.
int calculateGradientDirection(const Mat &image, int x, int y) {
Mat grad_x, grad_y;
Sobel(image, grad_x, CV_32F, 1, 0, 3);
Sobel(image, grad_y, CV_32F, 0, 1, 3);
float dx = grad_x.at<float>(y, x);
float dy = grad_y.at<float>(y, x);
return static_cast<int>(atan2(dy, dx) * 180 / CV_PI) % ROTATION_LEVELS;
}
Mat applyCannyEdgeDetection(const Mat &image, int lowThreshold, int highThreshold) {
Mat edges;
Canny(image, edges, lowThreshold, highThreshold);
return edges;
}
void constructRTable(const Mat &templ, RTable &rTable, Point reference) {
for (int y = 0; y < templ.rows; y++) {
for (int x = 0; x < templ.cols; x++) {
if (templ.at<uchar>(y, x) > 200) {
int angle = calculateGradientDirection(templ, x, y);
int quantizedAngle = angle % ROTATION_LEVELS;
Point vector = {reference.x - x, reference.y - y};
rTable[quantizedAngle].push_back(vector);
}
}
}
}
vector<Point> detectObjects(const Mat &edgeImage, const RTable &rTable, int voteThreshold, int dp, int minDistance) {
int width = edgeImage.cols / dp;
int height = edgeImage.rows / dp;
Mat accumulator = Mat::zeros(height, width, CV_32SC1);
for (int y = 0; y < edgeImage.rows; y++) {
for (int x = 0; x < edgeImage.cols; x++) {
if (edgeImage.at<uchar>(y, x) > 200) {
int angle = calculateGradientDirection(edgeImage, x, y);
if (rTable.find(angle) != rTable.end()) {
for (const auto &vec : rTable.at(angle)) {
int cx = (x + vec.x) / dp;
int cy = (y + vec.y) / dp;
if (cx >= 0 && cx < width && cy >= 0 && cy < height) {
accumulator.at<int>(cy, cx)++;
}
}
}
}
}
}
vector<Point> detections;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (accumulator.at<int>(y, x) > voteThreshold) {
detections.push_back(Point(x * dp, y * dp));
}
}
}
vector<Point> finalDetections;
for (size_t i = 0; i < detections.size(); i++) {
bool isMax = true;
for (size_t j = 0; j < detections.size(); j++) {
if (i != j && norm(detections[i] - detections[j]) < minDistance) {
if (accumulator.at<int>(detections[i].y / dp, detections[i].x / dp) <
accumulator.at<int>(detections[j].y / dp, detections[j].x / dp)) {
isMax = false;
break;
}
}
}
if (isMax) {
finalDetections.push_back(detections[i]);
}
}
return finalDetections;
}
int main() {
// Start timing the core GHT process.
auto start = chrono::high_resolution_clock::now();
// Load the template and convert it to grayscale.
Mat templ = imread("resources/template_key.png", IMREAD_GRAYSCALE);
if (templ.empty()) {
cerr << "Error: Could not load template image." << endl;
return EXIT_FAILURE;
}
// Load the input image.
Mat coloredImage = imread("resources/image_key.png");
if (coloredImage.empty()) {
cerr << "Error: Could not load input image." << endl;
return EXIT_FAILURE;
}
// Create grayscale image.
Mat image;
cvtColor(coloredImage, image, COLOR_RGB2GRAY);
// Apply Canny edge detection to the template and input image.
Mat edgeTemplate = applyCannyEdgeDetection(templ, CANNY_LOW_THRESHOLD, CANNY_HIGH_THRESHOLD);
Mat edgeImage = applyCannyEdgeDetection(image, CANNY_LOW_THRESHOLD, CANNY_HIGH_THRESHOLD);
// Define the reference point (center of the template).
Point reference = {templ.cols / 2, templ.rows / 2};
// Construct the R-Table
RTable rTable;
constructRTable(edgeTemplate, rTable, reference);
// Detect objects in the input image using the R-Table.
vector<Point> detections = detectObjects(edgeImage, rTable, VOTE_THRESHOLD, ACCUMULATOR_DEPTH, MIN_DISTANCE);
// Draw the detected objects on the input image.
for (const auto ¢er : detections) {
circle(coloredImage, center, 10, Scalar(255, 0, 0), 2);
Rect boundingBox(center.x - templ.cols / 2, center.y - templ.rows / 2, templ.cols, templ.rows);
rectangle(coloredImage, boundingBox, Scalar(0, 255, 0), 2);
}
// Stop timing.
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> duration = end - start;
// Print execution time.
cout << "Execution time of GHT core process: " << duration.count() << " seconds" << endl;
// Display the result
// imshow("Detected Objects", coloredImage);
// waitKey(0);
return EXIT_SUCCESS;
}