From b46d3027b796661d01fb42c8cc8be089b7fb464e Mon Sep 17 00:00:00 2001 From: Konnect Date: Tue, 18 Aug 2015 14:38:21 +0300 Subject: [PATCH 1/3] Not Ready --- CMakeLists.txt | 1 + sample_barinov_m/CMakeLists.txt | 7 ++ sample_barinov_m/application.cpp | 123 +++++++++++++++++++++++++++++++ sample_barinov_m/application.hpp | 49 ++++++++++++ sample_barinov_m/main.cpp | 44 +++++++++++ sample_barinov_m/processing.cpp | 18 +++++ sample_barinov_m/processing.hpp | 9 +++ 7 files changed, 251 insertions(+) create mode 100644 sample_barinov_m/CMakeLists.txt create mode 100644 sample_barinov_m/application.cpp create mode 100644 sample_barinov_m/application.hpp create mode 100644 sample_barinov_m/main.cpp create mode 100644 sample_barinov_m/processing.cpp create mode 100644 sample_barinov_m/processing.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a1949c..ce41405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ set(LIBRARY_DEPS ${OpenCV_LIBS}) add_subdirectory(sample_template) # Add you directory here # add_subdirectory(sample_YOUR_NAME) +add_subdirectory(sample_barinov_m) # REPORT message( STATUS "") diff --git a/sample_barinov_m/CMakeLists.txt b/sample_barinov_m/CMakeLists.txt new file mode 100644 index 0000000..3e1b9d3 --- /dev/null +++ b/sample_barinov_m/CMakeLists.txt @@ -0,0 +1,7 @@ +set(target "sample_barinov_m") + +file(GLOB hdrs "*.hpp") +file(GLOB srcs "*.cpp") + +add_executable(${target} ${srcs} ${hdrs}) +target_link_libraries(${target} ${LIBRARY_DEPS}) diff --git a/sample_barinov_m/application.cpp b/sample_barinov_m/application.cpp new file mode 100644 index 0000000..4cc18d1 --- /dev/null +++ b/sample_barinov_m/application.cpp @@ -0,0 +1,123 @@ +#include "application.hpp" +#include "processing.hpp" + +#include + +using namespace cv; + +int Application::parseArguments(int argc, const char **argv, + Application::Parameters ¶ms) +{ + if (argc < 2) + { + return 1; + } + params.imgFileName = std::string(argv[1]); + return 0; +} + +int Application::getFrame(const std::string &fileName, Mat& src) +{ + src = imread(fileName); + if (src.empty()) + { + return 1; + } + return 0; +} + +int Application::processFrame(const Mat& src, Mat& dst) +{ + processor.processFrame(src, dst); + + if (dst.empty()) + { + return 1; + } + + return 0; +} + +int Application::drawButtons(Mat &display) +{ + guiState.onButtonPlace = Rect(20, display.rows - 60, 120, 40); + guiState.offButtonPlace = Rect(160, display.rows - 60, 120, 40); + rectangle(display, guiState.onButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.offButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + + putText(display, "on", + Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, + guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "off", + Point(guiState.offButtonPlace.x + guiState.offButtonPlace.width / 2 - 20, + guiState.offButtonPlace.y + guiState.offButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + + return 0; +} + +int Application::showFrame(const std::string &caption, + const Mat& src, Mat& dst) +{ + if (guiState.state == OffFilter) + { + src.copyTo(dst); + } + else if (guiState.state == OnFilter) + { + processFrame(src, dst); + } + else + { + return 1; + } + + Mat display(src.rows, src.cols + dst.cols, src.type()); + Mat srcRoi = display(Rect(0, 0, src.cols, src.rows)); + src.copyTo(srcRoi); + Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows)); + dst.copyTo(dstRoi); + + drawButtons(display); + + namedWindow(caption); + imshow(caption, display); + setMouseCallback(caption, onButtonsOnOffClick, &guiState); + char key = waitKey(1); + + return key; +} + +void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) +{ + if (eventId != EVENT_LBUTTONDOWN) + { + return; + } + Application::GUIElementsState *elems = + (Application::GUIElementsState *)userData; + if (onButtonClicked(elems->onButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + return; + } + if (onButtonClicked(elems->offButtonPlace, x, y)) + { + elems->state = Application::OffFilter; + return; + } +} + +bool onButtonClicked(cv::Rect buttonPlace, int x, int y) +{ + if (x < buttonPlace.x || x > buttonPlace.x + buttonPlace.width || + y < buttonPlace.y || y > buttonPlace.y + buttonPlace.height) + { + return false; + } + return true; +} + diff --git a/sample_barinov_m/application.hpp b/sample_barinov_m/application.hpp new file mode 100644 index 0000000..862cc3f --- /dev/null +++ b/sample_barinov_m/application.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +#include "processing.hpp" + +bool onButtonClicked(cv::Rect buttonPlace, int x, int y); +void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData); + +class Application +{ + public: + enum WindowState + { + OnFilter, + OffFilter + }; + struct Parameters + { + std::string imgFileName; + }; + struct GUIElementsState + { + WindowState state; + cv::Rect onButtonPlace; + cv::Rect offButtonPlace; + }; + int parseArguments(int argc, const char **argv, Parameters ¶ms); + int getFrame(const std::string &fileName, cv::Mat& src); + int processFrame(const cv::Mat& src, cv::Mat& dst); + int showFrame(const std::string &caption, + const cv::Mat& src, cv::Mat& dst); + friend void onButtonsOnOffClick(int eventId, int x, int y, + int flags, void *userData); + Application() + { + guiState.state = OnFilter; + }; + + private: + Processing processor; + GUIElementsState guiState; + + int drawButtons(cv::Mat &display); + + friend bool onButtonClicked(cv::Rect buttonPlace, int x, int y); +}; diff --git a/sample_barinov_m/main.cpp b/sample_barinov_m/main.cpp new file mode 100644 index 0000000..43ff041 --- /dev/null +++ b/sample_barinov_m/main.cpp @@ -0,0 +1,44 @@ +#include +#include + +#include "application.hpp" + +using namespace std; +using namespace cv; + +enum ErrorCode { + OK, + WRONG_ARGUMENTS, + WRONG_INPUT, + CANT_PROCESS +}; + +int main(int argc, const char **argv) +{ + Application app; + Application::Parameters params; + + if (app.parseArguments(argc, argv, params) != 0) + { + cout << "sample_template " << endl; + cout << " - image name for filtering" << endl; + return WRONG_ARGUMENTS; + } + + Mat src; + if (app.getFrame(params.imgFileName, src) != 0) + { + cout << "Error: \'src\' image is null or empty!" << endl; + return WRONG_INPUT; + } + + const std::string caption = "OpenCV Sample"; + char key = 0; + Mat dst(src.rows, src.cols, src.type()); + while (key != 27) // Esc + { + key = app.showFrame(caption, src, dst); + } + + return OK; +} diff --git a/sample_barinov_m/processing.cpp b/sample_barinov_m/processing.cpp new file mode 100644 index 0000000..bd51a88 --- /dev/null +++ b/sample_barinov_m/processing.cpp @@ -0,0 +1,18 @@ +#include "processing.hpp" + +#include + +using namespace cv; + +void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) +{ + src.copyTo(dst); + + cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2); + Mat roi = dst(region); + + const int kSize = 11; + medianBlur(roi, roi, kSize); + + rectangle(dst, region, Scalar(255, 0, 0)); +} diff --git a/sample_barinov_m/processing.hpp b/sample_barinov_m/processing.hpp new file mode 100644 index 0000000..65f29fc --- /dev/null +++ b/sample_barinov_m/processing.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +class Processing +{ + public: + void processFrame(const cv::Mat& src, cv::Mat& dst); +}; From aab6dca15b3066bd9d859400c121271072b47a62 Mon Sep 17 00:00:00 2001 From: Konnect Date: Tue, 18 Aug 2015 17:00:49 +0300 Subject: [PATCH 2/3] Add diagonal move --- sample_barinov_m/application.cpp | 2 ++ sample_barinov_m/processing.cpp | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sample_barinov_m/application.cpp b/sample_barinov_m/application.cpp index 4cc18d1..ce99afd 100644 --- a/sample_barinov_m/application.cpp +++ b/sample_barinov_m/application.cpp @@ -59,6 +59,8 @@ int Application::drawButtons(Mat &display) return 0; } + + int Application::showFrame(const std::string &caption, const Mat& src, Mat& dst) { diff --git a/sample_barinov_m/processing.cpp b/sample_barinov_m/processing.cpp index bd51a88..cfe4fb4 100644 --- a/sample_barinov_m/processing.cpp +++ b/sample_barinov_m/processing.cpp @@ -1,4 +1,6 @@ #include "processing.hpp" +#include "time.h" +#include #include @@ -8,7 +10,21 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) { src.copyTo(dst); - cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2); + cv::Rect region((int)((sin((long double)(clock() / CLOCKS_PER_SEC *3))*src.rows/4)+src.rows/4), + (int)((sin((long double)(clock() / CLOCKS_PER_SEC *3))*src.cols/4)+src.cols/4), + src.rows/2, src.cols/2); + + /*if (y % (src.cols/2) > 1) + { + if (x % (src.rows/2) > 1) + x=x+1; + else x=0; + + y=y+1; + } + else y=0;*/ + + //cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2); Mat roi = dst(region); const int kSize = 11; From ce6191962cd0fa2385a6c3e18aae67d075f7c3e4 Mon Sep 17 00:00:00 2001 From: Konnect Date: Tue, 18 Aug 2015 18:23:58 +0300 Subject: [PATCH 3/3] Save with time --- sample_barinov_m/application.cpp | 43 +++++++++++++++++++++++++++++++- sample_barinov_m/application.hpp | 4 ++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/sample_barinov_m/application.cpp b/sample_barinov_m/application.cpp index ce99afd..bafa15a 100644 --- a/sample_barinov_m/application.cpp +++ b/sample_barinov_m/application.cpp @@ -1,9 +1,12 @@ #include "application.hpp" #include "processing.hpp" +#include "time.h" +#include "stdio.h" #include using namespace cv; +bool SaveFlag=0; int Application::parseArguments(int argc, const char **argv, Application::Parameters ¶ms) @@ -42,10 +45,13 @@ int Application::drawButtons(Mat &display) { guiState.onButtonPlace = Rect(20, display.rows - 60, 120, 40); guiState.offButtonPlace = Rect(160, display.rows - 60, 120, 40); + guiState.saveButtonPlace = Rect(300, display.rows - 60, 120, 40); rectangle(display, guiState.onButtonPlace, Scalar(128, 128, 128), CV_FILLED); rectangle(display, guiState.offButtonPlace, Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.saveButtonPlace, + Scalar(128, 128, 128), CV_FILLED); putText(display, "on", Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, @@ -55,6 +61,10 @@ int Application::drawButtons(Mat &display) Point(guiState.offButtonPlace.x + guiState.offButtonPlace.width / 2 - 20, guiState.offButtonPlace.y + guiState.offButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "save", + Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 25, + guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -72,6 +82,10 @@ int Application::showFrame(const std::string &caption, { processFrame(src, dst); } + /*else if (guiState.state == SaveFilter) + { + SaveFlag=1; + }*/ else { return 1; @@ -81,7 +95,28 @@ int Application::showFrame(const std::string &caption, Mat srcRoi = display(Rect(0, 0, src.cols, src.rows)); src.copyTo(srcRoi); Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows)); - dst.copyTo(dstRoi); + dst.copyTo(dstRoi); + + + // получить текущее время + // сгенерировать название изображения + // - сгенерированное название изображения + // с меткой текущего времени + // вызвать функцию сохранения imwrite(, display) + // сбросить значение guiState.saveState в false + if (SaveFlag==1) + { + time_t timer = time(NULL); + std::ostringstream oss; + oss << timer; + std::string ctime = oss.str()+".png"; + + vector compression_params; + compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); + compression_params.push_back(9); + imwrite(ctime, display, compression_params); + SaveFlag=0; + } drawButtons(display); @@ -111,6 +146,12 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->state = Application::OffFilter; return; } + if (onButtonClicked(elems->saveButtonPlace, x, y)) + { + SaveFlag=1; + //elems->state = Application::SaveFilter; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_barinov_m/application.hpp b/sample_barinov_m/application.hpp index 862cc3f..24453ea 100644 --- a/sample_barinov_m/application.hpp +++ b/sample_barinov_m/application.hpp @@ -15,7 +15,8 @@ class Application enum WindowState { OnFilter, - OffFilter + OffFilter, + SaveFilter }; struct Parameters { @@ -26,6 +27,7 @@ class Application WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; + cv::Rect saveButtonPlace; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); int getFrame(const std::string &fileName, cv::Mat& src);