-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
answer_8.cpp
53 lines (43 loc) · 1.1 KB
/
answer_8.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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
// max pooling
cv::Mat max_pooling(cv::Mat img){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
int r = 8;
double v = 0;
for (int y = 0; y < height; y+=r){
for (int x = 0; x < width; x+=r){
for (int c = 0; c < channel; c++){
v = 0;
for (int dy = 0; dy < r; dy++){
for (int dx = 0; dx < r; dx++){
v = fmax(img.at<cv::Vec3b>(y + dy, x + dx)[c], v);
}
}
for (int dy = 0; dy < r; dy++){
for (int dx = 0; dx < r; dx++){
out.at<cv::Vec3b>(y + dy, x + dx)[c] = (uchar)v;
}
}
}
}
}
return out;
}
int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);
// max pooling
cv::Mat out = max_pooling(img);
//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}