Repository navigation
Expand file tree
/
Copy pathtask7.cpp
More file actions
417 lines (357 loc) · 19 KB
/
Copy pathtask7.cpp
File metadata and controls
417 lines (357 loc) · 19 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include "task7.h"
#include "config.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
// ─────────────────────────────────────────────────────────────────────────────
// detectHarrisCorners
// ─────────────────────────────────────────────────────────────────────────────
std::vector<cv::Point> detectHarrisCorners(const cv::Mat& gray,
int blockSize,
int apertureSize,
double kHarris,
double threshold)
{
cv::Mat response;
cv::cornerHarris(gray, response, blockSize, apertureSize, kHarris);
cv::Mat norm;
cv::normalize(response, norm, 0, 255, cv::NORM_MINMAX, CV_32F);
float thresh = static_cast<float>(threshold * 255.0f);
std::vector<cv::Point> corners;
for (int r = 0; r < norm.rows; ++r)
for (int c = 0; c < norm.cols; ++c)
if (norm.at<float>(r, c) > thresh)
corners.emplace_back(c, r);
return corners;
}
// ─────────────────────────────────────────────────────────────────────────────
// drawHarrisResults
// ─────────────────────────────────────────────────────────────────────────────
void drawHarrisResults(cv::Mat& frame,
const std::vector<cv::Point>& corners,
const cv::Mat& response,
cv::Mat& responseVis)
{
cv::Mat norm;
cv::normalize(response, norm, 0, 255, cv::NORM_MINMAX, CV_32F);
for (const auto& pt : corners) {
float s = norm.at<float>(pt.y, pt.x) / 255.f;
// Strong corners → cyan-white, weak → pure cyan
cv::Scalar fill(
static_cast<int>(s * 180), // B
static_cast<int>(200 + s * 55), // G
static_cast<int>(200 + s * 55) // R → warm white at peak
);
cv::circle(frame, pt, 5, fill, cv::FILLED, cv::LINE_AA);
cv::circle(frame, pt, 5, cv::Scalar(0, 60, 100), 1, cv::LINE_AA);
// Bright centre dot
cv::circle(frame, pt, 1, cv::Scalar(255,255,255), cv::FILLED, cv::LINE_AA);
}
cv::Mat u8;
cv::normalize(response, u8, 0, 255, cv::NORM_MINMAX, CV_8U);
cv::applyColorMap(u8, responseVis, cv::COLORMAP_INFERNO);
}
// ─────────────────────────────────────────────────────────────────────────────
// Design tokens
// ─────────────────────────────────────────────────────────────────────────────
// Panel geometry
static const int PX = 10; // panel top-left X
static const int PY = 10; // panel top-left Y
static const int PW = 200; // panel width (was 260)
static const int TITLE_H = 24; // title bar height (was 32)
static const int ROW_H = 40; // height per parameter row (was 52)
static const int NUM_ROWS = 3;
static const int PH = TITLE_H + NUM_ROWS * ROW_H + 6;
// Button geometry
static const int BTN_R = 10; // button circle radius (was 13)
static const int BTN_PAD = 5;
// Button centres for each row
static cv::Point upCentre(int row)
{
return { PX + BTN_PAD + BTN_R,
PY + TITLE_H + row * ROW_H + ROW_H / 2 };
}
static cv::Point downCentre(int row)
{
return { PX + PW - BTN_PAD - BTN_R,
PY + TITLE_H + row * ROW_H + ROW_H / 2 };
}
// Colours (BGR)
static const cv::Scalar C_PANEL_BG (18, 18, 26 );
static const cv::Scalar C_TITLE_BG (30, 30, 46 );
static const cv::Scalar C_BORDER (70, 130, 200); // blue accent
static const cv::Scalar C_DIVIDER (45, 45, 60 );
static const cv::Scalar C_BTN_FILL (40, 90, 160); // blue button
static const cv::Scalar C_BTN_HOVER (60, 140, 220); // lighter on hover
static const cv::Scalar C_ARROW (230, 240, 255); // near-white arrow
static const cv::Scalar C_LABEL (180, 190, 210);
static const cv::Scalar C_VALUE (80, 240, 200); // teal value
static const cv::Scalar C_TRACK_BG (50, 50, 70 );
static const cv::Scalar C_TRACK_FG (60, 180, 255); // progress fill
static const cv::Scalar C_TITLE_TXT (220, 230, 255);
// ─────────────────────────────────────────────────────────────────────────────
// Parameter state + mouse state
// ─────────────────────────────────────────────────────────────────────────────
struct Params {
int blockIdx = 0; // 0–13 → blockSize = idx+2
int aptIdx = 0; // 0–2 → aperture {3,5,7}
int threshPct = 5; // 0–100
};
static Params g_p;
static cv::Point g_mouse(-1, -1); // last known mouse position
static void onMouse(int event, int x, int y, int /*flags*/, void* /*userdata*/)
{
g_mouse = {x, y};
if (event != cv::EVENT_LBUTTONDOWN) return;
for (int row = 0; row < NUM_ROWS; ++row) {
// Hit-test: circle of radius BTN_R+4 for easy clicking
auto hit = [&](cv::Point centre) {
int dx = x - centre.x, dy = y - centre.y;
return dx*dx + dy*dy <= (BTN_R+4)*(BTN_R+4);
};
if (hit(upCentre(row))) {
if (row == 0) g_p.blockIdx = std::min(g_p.blockIdx + 1, 13);
if (row == 1) g_p.aptIdx = std::min(g_p.aptIdx + 1, 2);
if (row == 2) g_p.threshPct = std::min(g_p.threshPct + 5, 100);
}
if (hit(downCentre(row))) {
if (row == 0) g_p.blockIdx = std::max(g_p.blockIdx - 1, 0);
if (row == 1) g_p.aptIdx = std::max(g_p.aptIdx - 1, 0);
if (row == 2) g_p.threshPct = std::max(g_p.threshPct - 5, 0);
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Drawing helpers
// ─────────────────────────────────────────────────────────────────────────────
// Filled rounded rectangle (approximated with ellipses at corners + rects)
static void fillRoundRect(cv::Mat& img,
cv::Rect r, int radius,
cv::Scalar colour, double alpha = 1.0)
{
if (alpha < 1.0) {
// Clip rect to image bounds
cv::Rect safe = r & cv::Rect(0, 0, img.cols, img.rows);
if (safe.area() <= 0) return;
cv::Mat roi = img(safe);
cv::Mat over = roi.clone();
// Draw on clone
cv::rectangle(over, cv::Rect(radius, 0, r.width-2*radius, r.height), colour, cv::FILLED);
cv::rectangle(over, cv::Rect(0, radius, r.width, r.height-2*radius), colour, cv::FILLED);
for (auto [cx,cy] : std::vector<std::pair<int,int>>{
{radius,radius},{r.width-radius,radius},
{radius,r.height-radius},{r.width-radius,r.height-radius}})
cv::circle(over, {cx,cy}, radius, colour, cv::FILLED, cv::LINE_AA);
cv::addWeighted(over, alpha, roi, 1.0-alpha, 0, roi);
} else {
cv::rectangle(img, cv::Rect(r.x+radius, r.y, r.width-2*radius, r.height), colour, cv::FILLED);
cv::rectangle(img, cv::Rect(r.x, r.y+radius, r.width, r.height-2*radius),colour, cv::FILLED);
for (auto [cx,cy] : std::vector<std::pair<int,int>>{
{r.x+radius, r.y+radius},
{r.x+r.width-radius,r.y+radius},
{r.x+radius, r.y+r.height-radius},
{r.x+r.width-radius,r.y+r.height-radius}})
cv::circle(img, {cx,cy}, radius, colour, cv::FILLED, cv::LINE_AA);
}
}
// Rounded rectangle border only
static void strokeRoundRect(cv::Mat& img,
cv::Rect r, int radius,
cv::Scalar colour, int thick = 1)
{
int x=r.x, y=r.y, w=r.width, h=r.height;
cv::line(img, {x+radius,y}, {x+w-radius,y}, colour, thick, cv::LINE_AA);
cv::line(img, {x+radius,y+h}, {x+w-radius,y+h}, colour, thick, cv::LINE_AA);
cv::line(img, {x,y+radius}, {x,y+h-radius}, colour, thick, cv::LINE_AA);
cv::line(img, {x+w,y+radius}, {x+w,y+h-radius}, colour, thick, cv::LINE_AA);
cv::ellipse(img,{x+radius, y+radius}, {radius,radius},180, 0,90, colour,thick,cv::LINE_AA);
cv::ellipse(img,{x+w-radius, y+radius}, {radius,radius},270, 0,90, colour,thick,cv::LINE_AA);
cv::ellipse(img,{x+radius, y+h-radius}, {radius,radius}, 90, 0,90, colour,thick,cv::LINE_AA);
cv::ellipse(img,{x+w-radius, y+h-radius}, {radius,radius}, 0, 0,90, colour,thick,cv::LINE_AA);
}
// Draw a filled circle button with an up or down triangle inside
static void drawArrowButton(cv::Mat& frame, cv::Point centre,
bool up, bool hovered)
{
cv::Scalar fill = hovered ? C_BTN_HOVER : C_BTN_FILL;
cv::circle(frame, centre, BTN_R, fill, cv::FILLED, cv::LINE_AA);
cv::circle(frame, centre, BTN_R, C_BORDER, 1, cv::LINE_AA);
// Triangle (scaled to smaller button)
int tip = up ? -5 : 5;
int base = up ? 3 : -3;
std::vector<cv::Point> tri = {
{centre.x, centre.y + tip },
{centre.x - 5, centre.y + base},
{centre.x + 5, centre.y + base}
};
cv::fillPoly(frame,
std::vector<std::vector<cv::Point>>{tri},
C_ARROW, cv::LINE_AA);
}
// Mini horizontal progress bar
static void drawProgressBar(cv::Mat& frame,
int x, int y, int w, int h,
float fraction)
{
cv::Rect bg(x, y, w, h);
fillRoundRect(frame, bg, h/2, C_TRACK_BG);
if (fraction > 0.0f) {
int fw = std::max(h, static_cast<int>(fraction * w));
cv::Rect fg(x, y, fw, h);
fillRoundRect(frame, fg, h/2, C_TRACK_FG);
}
}
// ─────────────────────────────────────────────────────────────────────────────
// drawControlPanel
// ─────────────────────────────────────────────────────────────────────────────
static void drawControlPanel(cv::Mat& frame,
int blockSize, int apertureSize, int threshPct)
{
cv::Rect panelRect(PX, PY, PW, PH);
// ── 1. Panel background (more transparent) ───────────────────────────────
{
cv::Rect safe = panelRect & cv::Rect(0,0,frame.cols,frame.rows);
cv::Mat roi = frame(safe);
cv::Mat dark(safe.height, safe.width, CV_8UC3, C_PANEL_BG);
cv::addWeighted(roi, 0.40, dark, 0.60, 0, roi);
}
strokeRoundRect(frame, panelRect, 7, C_BORDER, 1);
// ── 2. Title bar ──────────────────────────────────────────────────────────
{
cv::Rect tr(PX+1, PY+1, PW-2, TITLE_H-1);
cv::Rect safe = tr & cv::Rect(0,0,frame.cols,frame.rows);
cv::Mat roi = frame(safe);
cv::Mat bg(safe.height, safe.width, CV_8UC3, C_TITLE_BG);
cv::addWeighted(roi, 0.25, bg, 0.75, 0, roi);
}
cv::putText(frame, "HARRIS PARAMS",
cv::Point(PX + 8, PY + TITLE_H - 7),
cv::FONT_HERSHEY_SIMPLEX, 0.38, C_TITLE_TXT, 1, cv::LINE_AA);
cv::circle(frame, {PX + PW - 10, PY + TITLE_H/2}, 4,
C_BORDER, cv::FILLED, cv::LINE_AA);
cv::line(frame,
{PX, PY + TITLE_H}, {PX + PW, PY + TITLE_H},
C_DIVIDER, 1, cv::LINE_AA);
// ── 3. Parameter rows ─────────────────────────────────────────────────────
const char* labels[NUM_ROWS] = { "Block Size", "Aperture", "Threshold" };
// Values and progress fractions
std::string valStr[NUM_ROWS];
float frac[NUM_ROWS];
valStr[0] = std::to_string(blockSize);
frac[0] = (blockSize - 2) / 13.f;
valStr[1] = std::to_string(apertureSize) + " px";
frac[1] = g_p.aptIdx / 2.f;
valStr[2] = std::to_string(threshPct) + "%";
frac[2] = threshPct / 100.f;
for (int row = 0; row < NUM_ROWS; ++row) {
int rowY = PY + TITLE_H + row * ROW_H;
// Row divider (skip first)
if (row > 0)
cv::line(frame, {PX, rowY}, {PX+PW, rowY},
C_DIVIDER, 1, cv::LINE_AA);
// Button centres
cv::Point uc = upCentre(row);
cv::Point dc = downCentre(row);
// Hover detection
bool upHov = cv::norm(g_mouse - uc) <= BTN_R + 4;
bool dnHov = cv::norm(g_mouse - dc) <= BTN_R + 4;
drawArrowButton(frame, uc, true, upHov);
drawArrowButton(frame, dc, false, dnHov);
// Label
int labelX = PX + BTN_PAD*2 + BTN_R*2 + 4;
int labelY = rowY + 15;
cv::putText(frame, labels[row],
{labelX, labelY},
cv::FONT_HERSHEY_SIMPLEX, 0.38, C_LABEL, 1, cv::LINE_AA);
// Progress bar
int barX = labelX;
int barY = rowY + 20;
int barW = PW - BTN_PAD*4 - BTN_R*4 - 12;
drawProgressBar(frame, barX, barY, barW, 5, frac[row]);
// Value text
int valX = dc.x - BTN_R - 4 - 38;
cv::putText(frame, valStr[row],
{valX, labelY},
cv::FONT_HERSHEY_SIMPLEX, 0.42, C_VALUE, 1, cv::LINE_AA);
}
}
// ─────────────────────────────────────────────────────────────────────────────
// drawCornerBadge — pill in the bottom-left showing the count
// ─────────────────────────────────────────────────────────────────────────────
// ─────────────────────────────────────────────────────────────────────────────
// runTask7
// ─────────────────────────────────────────────────────────────────────────────
void runTask7(int argc, char* argv[])
{
cv::VideoCapture cap = openVideoSource(argc, argv);
if (!cap.isOpened()) {
std::cerr << "[Task7][ERROR] Cannot open video source.\n";
return;
}
const std::string WIN_LIVE = "Task 7 - Harris Features";
const std::string WIN_RESP = "Task 7 - Response Map";
cv::namedWindow(WIN_LIVE, cv::WINDOW_AUTOSIZE);
cv::namedWindow(WIN_RESP, cv::WINDOW_AUTOSIZE);
cv::setMouseCallback(WIN_LIVE, onMouse);
const int apertureMap[3] = {3, 5, 7};
const double kHarris = 0.04;
std::cout << "=== Task 7: Harris Corner Detection ===\n"
<< " Click the ▲ / ▼ buttons on the panel to adjust:\n"
<< " Block size (2–15) — gradient matrix neighbourhood\n"
<< " Aperture (3/5/7) — Sobel kernel size\n"
<< " Threshold % (0–100) — fraction of peak response\n"
<< " Press 's' to save | 'q'/ESC to quit\n\n";
cv::Mat frame, gray, responseVis;
int frameCount = 0, savedCount = 0;
while (true) {
cap >> frame;
if (frame.empty()) break;
++frameCount;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
int blockSize = g_p.blockIdx + 2;
int apertureSize = apertureMap[std::max(0, std::min(2, g_p.aptIdx))];
double threshold = g_p.threshPct / 100.0;
// Harris response (single call — reused below)
cv::Mat response;
cv::cornerHarris(gray, response, blockSize, apertureSize, kHarris);
cv::Mat norm;
cv::normalize(response, norm, 0, 255, cv::NORM_MINMAX, CV_32F);
float thresh = static_cast<float>(threshold * 255.0f);
std::vector<cv::Point> corners;
for (int r = 0; r < norm.rows; ++r)
for (int c = 0; c < norm.cols; ++c)
if (norm.at<float>(r, c) > thresh)
corners.emplace_back(c, r);
// Draw feature dots + heat-map
drawHarrisResults(frame, corners, response, responseVis);
// Draw the control panel (on top of dots so it stays readable)
drawControlPanel(frame, blockSize, apertureSize, g_p.threshPct);
// Status bar
drawStatusBar(frame,
"Harris | block=" + std::to_string(blockSize) +
" apt=" + std::to_string(apertureSize) +
" thr=" + std::to_string(g_p.threshPct) + "% | " +
std::to_string(corners.size()) + " corners",
!corners.empty());
if (frameCount % 30 == 0)
std::cout << "[Task7][" << frameCount << "]"
<< " block=" << blockSize
<< " apt=" << apertureSize
<< " thr=" << g_p.threshPct << "%"
<< " corners=" << corners.size() << "\n";
cv::imshow(WIN_LIVE, frame);
cv::imshow(WIN_RESP, responseVis);
int key = cv::waitKey(1) & 0xFF;
if (key == 'q' || key == 27) break;
if (key == 's') {
std::string a = "task7_live_" + std::to_string(++savedCount) + ".png";
std::string b = "task7_response_" + std::to_string(savedCount) + ".png";
cv::imwrite(a, frame);
cv::imwrite(b, responseVis);
std::cout << "[Task7][SAVED] " << a << " " << b << "\n";
}
}
cap.release();
cv::destroyAllWindows();
}