-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslidingWindow.cpp
50 lines (47 loc) · 2.07 KB
/
slidingWindow.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
#include "slidingWindow.hpp"
Mat slidingWindowThresholdOptimized(Mat img, pair windowSize, uint8_t threshold){
Mat out;
const uint32_t movementRows = (img.rows - windowSize.first) + 1;
const uint32_t movementCols = (img.cols - windowSize.second)+ 1;
const uint32_t thresh = threshold*windowSize.first*windowSize.second;
out = Mat::zeros(cv::Size(movementCols, movementRows), CV_8UC1);
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
uchar* arr = img.ptr();
uint32_t sum = 0;
int32_t offset = 1-windowSize.second;
for (uint32_t imgRow = 0; imgRow < movementRows; imgRow++){
offset += windowSize.second-1;
for (uint32_t imgCol = 0; imgCol < movementCols; imgCol++){
for (uint32_t windowRow = 0; windowRow < windowSize.first; windowRow++){
for (uint32_t windowCol = 0; windowCol < windowSize.second; windowCol++){
sum += *(arr + windowCol + windowRow*img.cols + imgCol + imgRow*movementCols + offset);
}
}
if (sum > thresh){
out.at<uchar>(imgRow, imgCol) = 255;
}
sum = 0;
}
}
return out;
}
void slidingWindowOptimized(Mat img, pair windowSize) {
const uint32_t movementRows = (img.rows - windowSize.first) + 1;
const uint32_t movementCols = (img.cols - windowSize.second) + 1;
// cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
uchar* arr = img.ptr();
int32_t offset = 1-windowSize.second;
for (uint32_t imgRow = 0; imgRow < movementRows; imgRow++){
offset += windowSize.second-1;
for (uint32_t imgCol = 0; imgCol < movementCols; imgCol++){
std::cout << "\n";
for (uint32_t windowRow = 0; windowRow < windowSize.first; windowRow++){
std::cout << "\n";
for (uint32_t windowCol = 0; windowCol < windowSize.second; windowCol++){
std::cout << (int) *(arr + windowCol + windowRow*img.cols + imgCol + imgRow*movementCols + offset)
<< " ";
}
}
}
}
}