Skip to content

Commit 0bb951a

Browse files
On/Off buttons sample.
1 parent 4c3e866 commit 0bb951a

File tree

3 files changed

+111
-16
lines changed

3 files changed

+111
-16
lines changed

sample_template/application.cpp

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

66
using namespace cv;
77

8-
int Application::parseArguments(int argc, const char **argv, Application::Parameters &params)
8+
int Application::parseArguments(int argc, const char **argv,
9+
Application::Parameters &params)
910
{
1011
if (argc < 2)
1112
{
@@ -37,9 +38,39 @@ int Application::processFrame(const Mat& src, Mat& dst)
3738
return 0;
3839
}
3940

40-
int Application::showFrame(const std::string &caption, const Mat& src, const Mat& dst)
41+
int Application::drawButtons(Mat &display)
4142
{
42-
if (src.rows != dst.rows || src.cols != dst.cols)
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
4374
{
4475
return 1;
4576
}
@@ -48,11 +79,54 @@ int Application::showFrame(const std::string &caption, const Mat& src, const Mat
4879
Mat srcRoi = display(Rect(0, 0, src.cols, src.rows));
4980
src.copyTo(srcRoi);
5081
Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows));
51-
dst.copyTo(dstRoi);
52-
53-
namedWindow(caption);
82+
dst.copyTo(dstRoi);
83+
84+
drawButtons(display);
85+
86+
namedWindow(caption);
5487
imshow(caption, display);
88+
setMouseCallback(caption, onButtonsOnOffClick, &guiState);
5589
char key = waitKey(1);
5690

5791
return key;
5892
}
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::WindowState::OnFilter;
105+
return;
106+
}
107+
if (offButtonClicked(elems->offButtonPlace, x, y))
108+
{
109+
elems->state = Application::WindowState::OffFilter;
110+
return;
111+
}
112+
}
113+
114+
bool onButtonClicked(cv::Rect onButtonPlace, int x, int y)
115+
{
116+
if (x < onButtonPlace.x || x > onButtonPlace.x + onButtonPlace.width ||
117+
y < onButtonPlace.y || y > onButtonPlace.y + onButtonPlace.height)
118+
{
119+
return false;
120+
}
121+
return true;
122+
}
123+
124+
bool offButtonClicked(cv::Rect offButtonPlace, int x, int y)
125+
{
126+
if (x < offButtonPlace.x || x > offButtonPlace.x + offButtonPlace.width ||
127+
y < offButtonPlace.y || y > offButtonPlace.y + offButtonPlace.height)
128+
{
129+
return false;
130+
}
131+
return true;
132+
}

sample_template/application.hpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
#pragma once
22

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

67
#include "processing.hpp"
78

89
class Application
910
{
1011
public:
12+
enum WindowState
13+
{
14+
OnFilter,
15+
OffFilter
16+
};
1117
struct Parameters
1218
{
1319
std::string imgFileName;
1420
};
21+
struct GUIElementsState
22+
{
23+
WindowState state;
24+
cv::Rect onButtonPlace;
25+
cv::Rect offButtonPlace;
26+
};
1527
int parseArguments(int argc, const char **argv, Parameters &params);
16-
1728
int getFrame(const std::string &fileName, cv::Mat& src);
1829
int processFrame(const cv::Mat& src, cv::Mat& dst);
19-
int showFrame(const std::string &caption, const cv::Mat& src, const cv::Mat& dst);
30+
int showFrame(const std::string &caption,
31+
const cv::Mat& src, cv::Mat& dst);
32+
friend void onButtonsOnOffClick(int eventId, int x, int y,
33+
int flags, void *userData);
34+
Application()
35+
{
36+
guiState.state = OnFilter;
37+
};
2038

2139
private:
2240
Processing processor;
41+
GUIElementsState guiState;
42+
43+
int drawButtons(cv::Mat &display);
44+
45+
friend bool onButtonClicked(cv::Rect onButtonPlace, int x, int y);
46+
friend bool offButtonClicked(cv::Rect offButtonPlace, int x, int y);
2347
};

sample_template/main.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ int main(int argc, const char **argv)
3232
return WRONG_INPUT;
3333
}
3434

35-
Mat dst;
36-
if (app.processFrame(src, dst) != 0)
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
3739
{
38-
cout << "Error: Filtering failed!" << endl;
39-
return CANT_PROCESS;
40+
key = app.showFrame(caption, src, dst);
4041
}
4142

42-
char key = 0;
43-
while(key != 27) // Esc
44-
key = app.showFrame("OpenCV Sample", src, dst);
45-
4643
return OK;
4744
}

0 commit comments

Comments
 (0)