Skip to content

Commit e6cb8d3

Browse files
author
Kirill Kornyakov
committed
Use classes instead of plan functions
1 parent dcfdcb2 commit e6cb8d3

File tree

5 files changed

+84
-59
lines changed

5 files changed

+84
-59
lines changed

sample_template/application.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
11
#include "application.hpp"
2+
#include "processing.hpp"
23

3-
int parseArguments(int argc, const char **argv, Parameters &params)
4+
#include <opencv2/highgui/highgui.hpp>
5+
6+
using namespace cv;
7+
8+
int Application::parseArguments(int argc, const char **argv, Application::Parameters &params)
49
{
510
if (argc < 2)
611
{
712
return 1;
813
}
914
params.imgFileName = std::string(argv[1]);
1015
return 0;
11-
}
16+
}
17+
18+
int Application::getFrame(const std::string &fileName, Mat& src)
19+
{
20+
src = imread(fileName);
21+
if (src.empty())
22+
{
23+
return 1;
24+
}
25+
return 0;
26+
}
27+
28+
int Application::processFrame(const Mat& src, Mat& dst)
29+
{
30+
processor.processFrame(src, dst);
31+
32+
if (dst.empty())
33+
{
34+
return 1;
35+
}
36+
37+
return 0;
38+
}
39+
40+
int Application::show(const std::string &caption, const Mat& src, const Mat& dst)
41+
{
42+
if (src.rows != dst.rows || src.cols != dst.cols)
43+
{
44+
return 1;
45+
}
46+
47+
Mat display(src.rows, src.cols + dst.cols, src.type());
48+
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows));
49+
src.copyTo(srcRoi);
50+
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows));
51+
dst.copyTo(dstRoi);
52+
53+
namedWindow(caption);
54+
imshow(caption, display);
55+
char key = waitKey(1);
56+
57+
return key;
58+
}

sample_template/application.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#pragma once
22

33
#include <string>
4+
#include <opencv2/core/core.hpp>
45

5-
struct Parameters
6+
#include "processing.hpp"
7+
8+
class Application
69
{
7-
std::string imgFileName;
8-
};
10+
public:
11+
struct Parameters
12+
{
13+
std::string imgFileName;
14+
};
15+
int parseArguments(int argc, const char **argv, Parameters &params);
916

10-
int parseArguments(int argc, const char **argv, Parameters &params);
17+
int getFrame(const std::string &fileName, cv::Mat& src);
18+
int processFrame(const cv::Mat& src, cv::Mat& dst);
19+
int show(const std::string &caption, const cv::Mat& src, const cv::Mat& dst);
20+
private:
21+
Processing processor;
22+
};

sample_template/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22
#include <iostream>
33

44
#include "application.hpp"
5-
#include "processing.hpp"
65

76
using namespace std;
87
using namespace cv;
98

109
int main(int argc, const char **argv)
1110
{
12-
Parameters params;
13-
if (parseArguments(argc, argv, params) != 0)
11+
Application app;
12+
Application::Parameters params;
13+
14+
if (app.parseArguments(argc, argv, params) != 0)
1415
{
1516
cout << "practice2 <image_name>" << endl;
1617
cout << "<image_name> - image name for filtering" << endl;
1718
return 1;
1819
}
1920

2021
Mat src;
21-
if (getFrame(params.imgFileName, src) != 0)
22+
if (app.getFrame(params.imgFileName, src) != 0)
2223
{
2324
cout << "Error: \'src\' image is null or empty!" << endl;
2425
return 2;
2526
}
2627

2728
Mat dst;
28-
if (processFrame(src, dst) != 0)
29+
if (app.processFrame(src, dst) != 0)
2930
{
3031
cout << "Error: Filtering failed!" << endl;
3132
return 3;
@@ -34,7 +35,7 @@ int main(int argc, const char **argv)
3435
const std::string caption = "OpenCV Sample";
3536
char key = 0;
3637
while(key != 27) // Esc
37-
key = show(caption, src, dst);
38+
key = app.show(caption, src, dst);
3839

3940
return 0;
4041
}

sample_template/processing.cpp

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
11
#include "processing.hpp"
22

3-
int getFrame(const std::string &fileName, Mat& src)
4-
{
5-
src = imread(fileName);
6-
if (src.empty())
7-
{
8-
return 1;
9-
}
10-
return 0;
11-
}
3+
#include <opencv2/imgproc/imgproc.hpp>
124

13-
int processFrame(const Mat& src, Mat& dst)
5+
using namespace cv;
6+
7+
void Processing::processFrame(const cv::Mat& src, cv::Mat& dst)
148
{
159
src.copyTo(dst);
1610

1711
cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2);
18-
const int kSize = 11;
1912
Mat roi = dst(region);
20-
medianBlur(roi, roi, kSize);
21-
rectangle(dst, region, Scalar(255, 0, 0));
2213

23-
if (dst.empty())
24-
{
25-
return 1;
26-
}
14+
const int kSize = 11;
15+
medianBlur(roi, roi, kSize);
2716

28-
return 0;
17+
rectangle(dst, region, Scalar(255, 0, 0));
2918
}
30-
31-
int show(const std::string &caption, const Mat& src, const Mat& dst)
32-
{
33-
if (src.rows != dst.rows || src.cols != dst.cols)
34-
{
35-
return 1;
36-
}
37-
38-
Mat display(src.rows, src.cols + dst.cols, src.type());
39-
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows));
40-
src.copyTo(srcRoi);
41-
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows));
42-
dst.copyTo(dstRoi);
43-
44-
namedWindow(caption);
45-
imshow(caption, display);
46-
char key = waitKey(1);
47-
48-
return key;
49-
}

sample_template/processing.hpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#pragma once
22

33
#include <opencv2/core/core.hpp>
4-
#include <opencv2/highgui/highgui.hpp>
5-
#include <opencv2/imgproc/imgproc.hpp>
64

7-
using namespace cv;
8-
9-
int getFrame(const std::string &fileName, Mat& src);
10-
11-
int processFrame(const Mat& src, Mat& dst);
12-
13-
int show(const std::string &caption, const Mat& src, const Mat& dst);
5+
class Processing
6+
{
7+
public:
8+
void processFrame(const cv::Mat& src, cv::Mat& dst);
9+
};

0 commit comments

Comments
 (0)