Skip to content

Commit 337cad6

Browse files
author
Moskalenko
committed
NOT_READY
1 parent 62b82ac commit 337cad6

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(LIBRARY_DEPS ${OpenCV_LIBS})
1818

1919
# BUILD
2020
add_subdirectory(sample_template)
21+
add_subdirectory(sample_Moskalenko)
2122
# Add you directory here
2223
# add_subdirectory(sample_YOUR_NAME)
2324

sample_Moskalenko/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(target "sample_Moskalenko")
2+
3+
file(GLOB hdrs "*.hpp")
4+
file(GLOB srcs "*.cpp")
5+
6+
add_executable(${target} ${srcs} ${hdrs})
7+
target_link_libraries(${target} ${LIBRARY_DEPS})

sample_Moskalenko/application.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include "application.hpp"
2+
#include "processing.hpp"
3+
4+
#include <opencv2/highgui/highgui.hpp>
5+
6+
using namespace cv;
7+
8+
int Application::parseArguments(int argc, const char **argv,
9+
Application::Parameters &params)
10+
{
11+
if (argc < 2)
12+
{
13+
return 1;
14+
}
15+
params.imgFileName = std::string(argv[1]);
16+
return 0;
17+
}
18+
19+
int Application::getFrame(const std::string &fileName, Mat& src)
20+
{
21+
src = imread(fileName);
22+
if (src.empty())
23+
{
24+
return 1;
25+
}
26+
return 0;
27+
}
28+
29+
int Application::processFrame(const Mat& src, Mat& dst)
30+
{
31+
processor.processFrame(src, dst);
32+
33+
if (dst.empty())
34+
{
35+
return 1;
36+
}
37+
38+
return 0;
39+
}
40+
41+
int Application::drawButtons(Mat &display)
42+
{
43+
guiState.onButtonPlace = Rect(20, display.rows - 60, 120, 40);
44+
guiState.offButtonPlace = Rect(160, display.rows - 60, 120, 40);
45+
rectangle(display, guiState.onButtonPlace,
46+
Scalar(128, 128, 128), CV_FILLED);
47+
rectangle(display, guiState.offButtonPlace,
48+
Scalar(128, 128, 128), CV_FILLED);
49+
50+
putText(display, "on",
51+
Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15,
52+
guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10),
53+
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);
54+
putText(display, "off",
55+
Point(guiState.offButtonPlace.x + guiState.offButtonPlace.width / 2 - 20,
56+
guiState.offButtonPlace.y + guiState.offButtonPlace.height / 2 + 10),
57+
FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2);
58+
59+
return 0;
60+
}
61+
62+
int Application::showFrame(const std::string &caption,
63+
const Mat& src, Mat& dst)
64+
{
65+
if (guiState.state == OffFilter)
66+
{
67+
src.copyTo(dst);
68+
}
69+
else if (guiState.state == OnFilter)
70+
{
71+
processFrame(src, dst);
72+
}
73+
else
74+
{
75+
return 1;
76+
}
77+
78+
Mat display(src.rows, src.cols + dst.cols, src.type());
79+
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows));
80+
src.copyTo(srcRoi);
81+
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows));
82+
dst.copyTo(dstRoi);
83+
84+
drawButtons(display);
85+
86+
namedWindow(caption);
87+
imshow(caption, display);
88+
setMouseCallback(caption, onButtonsOnOffClick, &guiState);
89+
char key = waitKey(1);
90+
91+
return key;
92+
}
93+
94+
void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData)
95+
{
96+
if (eventId != EVENT_LBUTTONDOWN)
97+
{
98+
return;
99+
}
100+
Application::GUIElementsState *elems =
101+
(Application::GUIElementsState *)userData;
102+
if (onButtonClicked(elems->onButtonPlace, x, y))
103+
{
104+
elems->state = Application::OnFilter;
105+
return;
106+
}
107+
if (onButtonClicked(elems->offButtonPlace, x, y))
108+
{
109+
elems->state = Application::OffFilter;
110+
return;
111+
}
112+
}
113+
114+
bool onButtonClicked(cv::Rect buttonPlace, int x, int y)
115+
{
116+
if (x < buttonPlace.x || x > buttonPlace.x + buttonPlace.width ||
117+
y < buttonPlace.y || y > buttonPlace.y + buttonPlace.height)
118+
{
119+
return false;
120+
}
121+
return true;
122+
}
123+

sample_Moskalenko/application.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <iostream>
5+
#include <opencv2/core/core.hpp>
6+
7+
#include "processing.hpp"
8+
9+
bool onButtonClicked(cv::Rect buttonPlace, int x, int y);
10+
void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData);
11+
12+
class Application
13+
{
14+
public:
15+
enum WindowState
16+
{
17+
OnFilter,
18+
OffFilter
19+
};
20+
struct Parameters
21+
{
22+
std::string imgFileName;
23+
};
24+
struct GUIElementsState
25+
{
26+
WindowState state;
27+
cv::Rect onButtonPlace;
28+
cv::Rect offButtonPlace;
29+
};
30+
int parseArguments(int argc, const char **argv, Parameters &params);
31+
int getFrame(const std::string &fileName, cv::Mat& src);
32+
int processFrame(const cv::Mat& src, cv::Mat& dst);
33+
int showFrame(const std::string &caption,
34+
const cv::Mat& src, cv::Mat& dst);
35+
friend void onButtonsOnOffClick(int eventId, int x, int y,
36+
int flags, void *userData);
37+
Application()
38+
{
39+
guiState.state = OnFilter;
40+
};
41+
42+
private:
43+
Processing processor;
44+
GUIElementsState guiState;
45+
46+
int drawButtons(cv::Mat &display);
47+
48+
friend bool onButtonClicked(cv::Rect buttonPlace, int x, int y);
49+
};

sample_Moskalenko/main.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <opencv2/core/core.hpp>
2+
#include <iostream>
3+
4+
#include "application.hpp"
5+
6+
using namespace std;
7+
using namespace cv;
8+
9+
enum ErrorCode {
10+
OK,
11+
WRONG_ARGUMENTS,
12+
WRONG_INPUT,
13+
CANT_PROCESS
14+
};
15+
16+
int main(int argc, const char **argv)
17+
{
18+
Application app;
19+
Application::Parameters params;
20+
21+
if (app.parseArguments(argc, argv, params) != 0)
22+
{
23+
cout << "sample_template <image_name>" << endl;
24+
cout << "<image_name> - image name for filtering" << endl;
25+
return WRONG_ARGUMENTS;
26+
}
27+
28+
Mat src;
29+
if (app.getFrame(params.imgFileName, src) != 0)
30+
{
31+
cout << "Error: \'src\' image is null or empty!" << endl;
32+
return WRONG_INPUT;
33+
}
34+
35+
const std::string caption = "OpenCV Sample";
36+
char key = 0;
37+
Mat dst(src.rows, src.cols, src.type());
38+
while (key != 27) // Esc
39+
{
40+
key = app.showFrame(caption, src, dst);
41+
}
42+
43+
return OK;
44+
}

sample_Moskalenko/processing.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "processing.hpp"
2+
3+
#include <opencv2/imgproc/imgproc.hpp>
4+
5+
using namespace cv;
6+
7+
void Processing::processFrame(const cv::Mat& src, cv::Mat& dst)
8+
{
9+
src.copyTo(dst);
10+
11+
cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2);
12+
Mat roi = dst(region);
13+
14+
const int kSize = 11;
15+
medianBlur(roi, roi, kSize);
16+
17+
rectangle(dst, region, Scalar(255, 0, 0));
18+
}

sample_Moskalenko/processing.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <opencv2/core/core.hpp>
4+
5+
class Processing
6+
{
7+
public:
8+
void processFrame(const cv::Mat& src, cv::Mat& dst);
9+
};

sample_template/application.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
using namespace cv;
77

8+
bool testfunction() { return true; }
9+
810
int Application::parseArguments(int argc, const char **argv,
911
Application::Parameters &params)
1012
{

0 commit comments

Comments
 (0)