|
| 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 ¶ms) |
| 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 | + |
0 commit comments