From 54e129b7ec04e1d717b6117a119f060aaf14381d Mon Sep 17 00:00:00 2001 From: kuanna Date: Tue, 18 Aug 2015 14:46:47 +0300 Subject: [PATCH 1/6] Add new directiry --- CMakeLists.txt | 1 + sample_Kuklina/CMakeLists.txt | 7 ++ sample_Kuklina/application.cpp | 123 +++++++++++++++++++++++++++++++++ sample_Kuklina/application.hpp | 49 +++++++++++++ sample_Kuklina/main.cpp | 44 ++++++++++++ sample_Kuklina/processing.cpp | 18 +++++ sample_Kuklina/processing.hpp | 9 +++ 7 files changed, 251 insertions(+) create mode 100644 sample_Kuklina/CMakeLists.txt create mode 100644 sample_Kuklina/application.cpp create mode 100644 sample_Kuklina/application.hpp create mode 100644 sample_Kuklina/main.cpp create mode 100644 sample_Kuklina/processing.cpp create mode 100644 sample_Kuklina/processing.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a1949c..8db2351 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(LIBRARY_DEPS ${OpenCV_LIBS}) # BUILD add_subdirectory(sample_template) +add_subdirectory(sample_Kuklina) # Add you directory here # add_subdirectory(sample_YOUR_NAME) diff --git a/sample_Kuklina/CMakeLists.txt b/sample_Kuklina/CMakeLists.txt new file mode 100644 index 0000000..111b9be --- /dev/null +++ b/sample_Kuklina/CMakeLists.txt @@ -0,0 +1,7 @@ +set(target "sample_Kuklina") + +file(GLOB hdrs "*.hpp") +file(GLOB srcs "*.cpp") + +add_executable(${target} ${srcs} ${hdrs}) +target_link_libraries(${target} ${LIBRARY_DEPS}) diff --git a/sample_Kuklina/application.cpp b/sample_Kuklina/application.cpp new file mode 100644 index 0000000..4cc18d1 --- /dev/null +++ b/sample_Kuklina/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_Kuklina/application.hpp b/sample_Kuklina/application.hpp new file mode 100644 index 0000000..862cc3f --- /dev/null +++ b/sample_Kuklina/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_Kuklina/main.cpp b/sample_Kuklina/main.cpp new file mode 100644 index 0000000..43ff041 --- /dev/null +++ b/sample_Kuklina/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_Kuklina/processing.cpp b/sample_Kuklina/processing.cpp new file mode 100644 index 0000000..bd51a88 --- /dev/null +++ b/sample_Kuklina/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_Kuklina/processing.hpp b/sample_Kuklina/processing.hpp new file mode 100644 index 0000000..65f29fc --- /dev/null +++ b/sample_Kuklina/processing.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +class Processing +{ + public: + void processFrame(const cv::Mat& src, cv::Mat& dst); +}; From a5e08016444d6df2d638dacd7ceb39d9368095a7 Mon Sep 17 00:00:00 2001 From: kuanna Date: Tue, 18 Aug 2015 15:34:43 +0300 Subject: [PATCH 2/6] Add runing frame --- sample_Kuklina/main.cpp | 2 +- sample_Kuklina/processing.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sample_Kuklina/main.cpp b/sample_Kuklina/main.cpp index 43ff041..18bf79b 100644 --- a/sample_Kuklina/main.cpp +++ b/sample_Kuklina/main.cpp @@ -20,7 +20,7 @@ int main(int argc, const char **argv) if (app.parseArguments(argc, argv, params) != 0) { - cout << "sample_template " << endl; + cout << "sample_Kuklina " << endl; cout << " - image name for filtering" << endl; return WRONG_ARGUMENTS; } diff --git a/sample_Kuklina/processing.cpp b/sample_Kuklina/processing.cpp index bd51a88..f2ef471 100644 --- a/sample_Kuklina/processing.cpp +++ b/sample_Kuklina/processing.cpp @@ -1,4 +1,6 @@ #include "processing.hpp" +#include +#include #include @@ -8,7 +10,10 @@ 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); + + int random = rand () % 16 + 2; + + cv::Rect region(src.rows/random, src.cols/random, src.rows/2, src.cols/2); Mat roi = dst(region); const int kSize = 11; From 40935e62510760ea31b44a570075a6dce1d8282a Mon Sep 17 00:00:00 2001 From: kuanna Date: Tue, 18 Aug 2015 17:41:58 +0300 Subject: [PATCH 3/6] Add button Save --- sample_Kuklina/application.cpp | 26 +++++++++++++++++++++++++- sample_Kuklina/application.hpp | 5 ++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sample_Kuklina/application.cpp b/sample_Kuklina/application.cpp index 4cc18d1..bdae4ea 100644 --- a/sample_Kuklina/application.cpp +++ b/sample_Kuklina/application.cpp @@ -1,5 +1,6 @@ #include "application.hpp" #include "processing.hpp" +#include #include @@ -42,10 +43,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 +59,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 - 30, + guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -62,6 +70,8 @@ int Application::drawButtons(Mat &display) int Application::showFrame(const std::string &caption, const Mat& src, Mat& dst) { + time_t name_time = time(NULL); + if (guiState.state == OffFilter) { src.copyTo(dst); @@ -79,7 +89,16 @@ 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); + + if(guiState.saveState) + { + char buff [50]; + struct tm * timeinfo = localtime (&name_time); + sprintf (buff,"%d.%d.%d.png",timeinfo->tm_hour,timeinfo->tm_min, timeinfo->tm_sec); + imwrite(buff, display); + guiState.saveState = false; + } drawButtons(display); @@ -109,6 +128,11 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->state = Application::OffFilter; return; } + if (onButtonClicked(elems->saveButtonPlace, x, y)) + { + elems->saveState = true; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Kuklina/application.hpp b/sample_Kuklina/application.hpp index 862cc3f..8dfa589 100644 --- a/sample_Kuklina/application.hpp +++ b/sample_Kuklina/application.hpp @@ -15,7 +15,7 @@ class Application enum WindowState { OnFilter, - OffFilter + OffFilter, }; struct Parameters { @@ -26,6 +26,8 @@ class Application WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; + cv::Rect saveButtonPlace; + bool saveState; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); int getFrame(const std::string &fileName, cv::Mat& src); @@ -37,6 +39,7 @@ class Application Application() { guiState.state = OnFilter; + guiState.saveState = false; }; private: From af275436c3aaffcf493dfc752a94f340c29b2ce7 Mon Sep 17 00:00:00 2001 From: kuanna Date: Tue, 18 Aug 2015 18:27:30 +0300 Subject: [PATCH 4/6] 3 buttons:gray. not ready --- sample_Kuklina/application.cpp | 7 +++++++ sample_Kuklina/application.hpp | 1 + sample_Kuklina/processing.cpp | 1 + 3 files changed, 9 insertions(+) diff --git a/sample_Kuklina/application.cpp b/sample_Kuklina/application.cpp index bdae4ea..eeb2887 100644 --- a/sample_Kuklina/application.cpp +++ b/sample_Kuklina/application.cpp @@ -44,12 +44,15 @@ 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); + guiState.cvtColorButtonPlace = Rect(440, 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); + rectangle(display, guiState.cvtColorButtonPlace, + Scalar(128, 128, 128), CV_FILLED); putText(display, "on", Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, @@ -63,6 +66,10 @@ int Application::drawButtons(Mat &display) Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 30, guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "gray", + Point(guiState.cvtColorButtonPlace.x + guiState.cvtColorButtonPlace.width / 2 - 40, + guiState.cvtColorButtonPlace.y + guiState.cvtColorButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } diff --git a/sample_Kuklina/application.hpp b/sample_Kuklina/application.hpp index 8dfa589..062bad2 100644 --- a/sample_Kuklina/application.hpp +++ b/sample_Kuklina/application.hpp @@ -27,6 +27,7 @@ class Application cv::Rect onButtonPlace; cv::Rect offButtonPlace; cv::Rect saveButtonPlace; + cv::Rect cvtColorButtonPlace; bool saveState; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); diff --git a/sample_Kuklina/processing.cpp b/sample_Kuklina/processing.cpp index f2ef471..71a7aa4 100644 --- a/sample_Kuklina/processing.cpp +++ b/sample_Kuklina/processing.cpp @@ -18,6 +18,7 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) const int kSize = 11; medianBlur(roi, roi, kSize); + cvtColor(roi, roi, CV_RGB2GRAY, 0); rectangle(dst, region, Scalar(255, 0, 0)); } From d1b506b2428dcf38ffa7b43930e77018e4aeb154 Mon Sep 17 00:00:00 2001 From: kuanna Date: Sat, 22 Aug 2015 22:33:29 +0300 Subject: [PATCH 5/6] Bottons Gray, Pixel, Canny --- sample_Kuklina/application.cpp | 62 ++++++++++++++++++++++++---------- sample_Kuklina/application.hpp | 14 +++++--- sample_Kuklina/processing.cpp | 56 ++++++++++++++++++++++++++---- sample_Kuklina/processing.hpp | 11 +++++- 4 files changed, 114 insertions(+), 29 deletions(-) diff --git a/sample_Kuklina/application.cpp b/sample_Kuklina/application.cpp index eeb2887..54f5f1d 100644 --- a/sample_Kuklina/application.cpp +++ b/sample_Kuklina/application.cpp @@ -1,6 +1,7 @@ #include "application.hpp" #include "processing.hpp" #include +#include #include @@ -27,9 +28,9 @@ int Application::getFrame(const std::string &fileName, Mat& src) return 0; } -int Application::processFrame(const Mat& src, Mat& dst) +int Application::processFrame(const Mat& src, Mat& dst, Processing::FilterType filter) { - processor.processFrame(src, dst); + processor.processFrame(src, dst, filter ); if (dst.empty()) { @@ -43,34 +44,42 @@ 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); - guiState.cvtColorButtonPlace = Rect(440, display.rows - 60, 120, 40); + guiState.saveButtonPlace = Rect(300, display.rows - 60, 120, 40); + guiState.cvtColorButtonPlace = Rect(440, display.rows - 60, 120, 40); + guiState.pixelButtonPlace = Rect(580, display.rows - 60, 120, 40); + guiState.cannyButtonPlace = Rect(720, 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, + rectangle(display, guiState.saveButtonPlace, Scalar(128, 128, 128), CV_FILLED); - rectangle(display, guiState.cvtColorButtonPlace, + rectangle(display, guiState.cvtColorButtonPlace, Scalar(128, 128, 128), CV_FILLED); - - putText(display, "on", - Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, + rectangle(display, guiState.pixelButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.cannyButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + + putText(display, "median", + Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 55, 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); - putText(display, "save", + putText(display, "save", Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 30, guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); - putText(display, "gray", - Point(guiState.cvtColorButtonPlace.x + guiState.cvtColorButtonPlace.width / 2 - 40, - guiState.cvtColorButtonPlace.y + guiState.cvtColorButtonPlace.height / 2 + 10), - FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); - + putText(display, "gray", + Point(guiState.cvtColorButtonPlace.x + guiState.cvtColorButtonPlace.width / 2 - 40, guiState.cvtColorButtonPlace.y + guiState.cvtColorButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "pixel", + Point(guiState.pixelButtonPlace.x + guiState.pixelButtonPlace.width / 2 - 40, guiState.pixelButtonPlace.y + guiState.pixelButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "canny", + Point(guiState.cannyButtonPlace.x + guiState.cannyButtonPlace.width / 2 - 40, guiState.cannyButtonPlace.y + guiState.cannyButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + return 0; } @@ -85,7 +94,7 @@ int Application::showFrame(const std::string &caption, } else if (guiState.state == OnFilter) { - processFrame(src, dst); + processFrame(src, dst, guiState.filter); } else { @@ -106,7 +115,7 @@ int Application::showFrame(const std::string &caption, imwrite(buff, display); guiState.saveState = false; } - + drawButtons(display); namedWindow(caption); @@ -128,6 +137,7 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) if (onButtonClicked(elems->onButtonPlace, x, y)) { elems->state = Application::OnFilter; + elems->filter = Processing::MEDIAN; return; } if (onButtonClicked(elems->offButtonPlace, x, y)) @@ -140,6 +150,24 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->saveState = true; return; } + if (onButtonClicked(elems->cvtColorButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::CVT_CONVERT_GRAY; + return; + } + if (onButtonClicked(elems->pixelButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::PIXELIZED; + return; + } + if (onButtonClicked(elems->cannyButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::CANNY; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Kuklina/application.hpp b/sample_Kuklina/application.hpp index 062bad2..51d7742 100644 --- a/sample_Kuklina/application.hpp +++ b/sample_Kuklina/application.hpp @@ -26,13 +26,16 @@ class Application WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; - cv::Rect saveButtonPlace; - cv::Rect cvtColorButtonPlace; - bool saveState; + cv::Rect saveButtonPlace; + cv::Rect cvtColorButtonPlace; + cv::Rect pixelButtonPlace; + cv::Rect cannyButtonPlace; + bool saveState; + Processing::FilterType filter; }; 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 processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter); int showFrame(const std::string &caption, const cv::Mat& src, cv::Mat& dst); friend void onButtonsOnOffClick(int eventId, int x, int y, @@ -40,7 +43,8 @@ class Application Application() { guiState.state = OnFilter; - guiState.saveState = false; + guiState.saveState = false; + guiState.filter = Processing::MEDIAN; }; private: diff --git a/sample_Kuklina/processing.cpp b/sample_Kuklina/processing.cpp index 71a7aa4..61edc64 100644 --- a/sample_Kuklina/processing.cpp +++ b/sample_Kuklina/processing.cpp @@ -6,19 +6,63 @@ using namespace cv; -void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) +void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter) { src.copyTo(dst); - - int random = rand () % 16 + 2; + int random = rand () % 16 + 2; cv::Rect region(src.rows/random, src.cols/random, src.rows/2, src.cols/2); Mat roi = dst(region); - const int kSize = 11; - medianBlur(roi, roi, kSize); - cvtColor(roi, roi, CV_RGB2GRAY, 0); + switch (filter) + { + case Processing::MEDIAN: + { + const int kSize = 11; + medianBlur(roi, roi, kSize); + break; + } + case Processing::CVT_CONVERT_GRAY: + { + Mat gray; + cvtColor(roi, gray, CV_BGR2GRAY); + int from_to[] = { 0, 0, 0, 1, 0, 2}; + mixChannels(&gray, 1, & roi, 1, from_to, 3); + break; + } + case Processing::PIXELIZED: + { + cv::Mat cir = cv::Mat::zeros(src.size(), CV_8UC1); + int bsize = 10; + + for (int i = 0; i < roi.rows; i += bsize) + { + for (int j = 0; j < roi.cols; j += bsize) + { + cv::Rect rect = cv::Rect(j, i, bsize, bsize) & + cv::Rect(0, 0, roi.cols, roi.rows); + + cv::Mat sub_dst(roi, rect); + sub_dst.setTo(cv::mean(src(rect))); + + cv::circle(cir, cv::Point(j+bsize/2, i+bsize/2), bsize/2-1, CV_RGB(255,255,255), -1, CV_AA ); + } + } + break; + } + case Processing::CANNY: + { + Mat gray, detected_edges; + cvtColor(roi, gray, CV_BGR2GRAY); + blur(gray, detected_edges, Size(3,3) ); + Canny( detected_edges, detected_edges, 30, 30*3,3); + roi = Scalar::all(0); + int from_to[] = { 0, 0, 0, 1, 0, 2}; + mixChannels(&detected_edges, 1, & roi, 1, from_to, 3); + break; + } + } rectangle(dst, region, Scalar(255, 0, 0)); } diff --git a/sample_Kuklina/processing.hpp b/sample_Kuklina/processing.hpp index 65f29fc..370be0c 100644 --- a/sample_Kuklina/processing.hpp +++ b/sample_Kuklina/processing.hpp @@ -5,5 +5,14 @@ class Processing { public: - void processFrame(const cv::Mat& src, cv::Mat& dst); + enum FilterType + { + MEDIAN, + CVT_CONVERT_GRAY, + PIXELIZED, + CANNY + }; + + void processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter); + }; From c6a4206169d9e09f741a8d5f1d132a507e0bfee7 Mon Sep 17 00:00:00 2001 From: kuanna Date: Sat, 22 Aug 2015 22:33:29 +0300 Subject: [PATCH 6/6] Buttons Gray, Pixel, Canny --- sample_Kuklina/application.cpp | 62 ++++++++++++++++++++++++---------- sample_Kuklina/application.hpp | 14 +++++--- sample_Kuklina/processing.cpp | 56 ++++++++++++++++++++++++++---- sample_Kuklina/processing.hpp | 11 +++++- 4 files changed, 114 insertions(+), 29 deletions(-) diff --git a/sample_Kuklina/application.cpp b/sample_Kuklina/application.cpp index eeb2887..54f5f1d 100644 --- a/sample_Kuklina/application.cpp +++ b/sample_Kuklina/application.cpp @@ -1,6 +1,7 @@ #include "application.hpp" #include "processing.hpp" #include +#include #include @@ -27,9 +28,9 @@ int Application::getFrame(const std::string &fileName, Mat& src) return 0; } -int Application::processFrame(const Mat& src, Mat& dst) +int Application::processFrame(const Mat& src, Mat& dst, Processing::FilterType filter) { - processor.processFrame(src, dst); + processor.processFrame(src, dst, filter ); if (dst.empty()) { @@ -43,34 +44,42 @@ 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); - guiState.cvtColorButtonPlace = Rect(440, display.rows - 60, 120, 40); + guiState.saveButtonPlace = Rect(300, display.rows - 60, 120, 40); + guiState.cvtColorButtonPlace = Rect(440, display.rows - 60, 120, 40); + guiState.pixelButtonPlace = Rect(580, display.rows - 60, 120, 40); + guiState.cannyButtonPlace = Rect(720, 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, + rectangle(display, guiState.saveButtonPlace, Scalar(128, 128, 128), CV_FILLED); - rectangle(display, guiState.cvtColorButtonPlace, + rectangle(display, guiState.cvtColorButtonPlace, Scalar(128, 128, 128), CV_FILLED); - - putText(display, "on", - Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, + rectangle(display, guiState.pixelButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.cannyButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + + putText(display, "median", + Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 55, 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); - putText(display, "save", + putText(display, "save", Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 30, guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); - putText(display, "gray", - Point(guiState.cvtColorButtonPlace.x + guiState.cvtColorButtonPlace.width / 2 - 40, - guiState.cvtColorButtonPlace.y + guiState.cvtColorButtonPlace.height / 2 + 10), - FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); - + putText(display, "gray", + Point(guiState.cvtColorButtonPlace.x + guiState.cvtColorButtonPlace.width / 2 - 40, guiState.cvtColorButtonPlace.y + guiState.cvtColorButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "pixel", + Point(guiState.pixelButtonPlace.x + guiState.pixelButtonPlace.width / 2 - 40, guiState.pixelButtonPlace.y + guiState.pixelButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "canny", + Point(guiState.cannyButtonPlace.x + guiState.cannyButtonPlace.width / 2 - 40, guiState.cannyButtonPlace.y + guiState.cannyButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + return 0; } @@ -85,7 +94,7 @@ int Application::showFrame(const std::string &caption, } else if (guiState.state == OnFilter) { - processFrame(src, dst); + processFrame(src, dst, guiState.filter); } else { @@ -106,7 +115,7 @@ int Application::showFrame(const std::string &caption, imwrite(buff, display); guiState.saveState = false; } - + drawButtons(display); namedWindow(caption); @@ -128,6 +137,7 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) if (onButtonClicked(elems->onButtonPlace, x, y)) { elems->state = Application::OnFilter; + elems->filter = Processing::MEDIAN; return; } if (onButtonClicked(elems->offButtonPlace, x, y)) @@ -140,6 +150,24 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->saveState = true; return; } + if (onButtonClicked(elems->cvtColorButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::CVT_CONVERT_GRAY; + return; + } + if (onButtonClicked(elems->pixelButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::PIXELIZED; + return; + } + if (onButtonClicked(elems->cannyButtonPlace, x, y)) + { + elems->state = Application::OnFilter; + elems->filter = Processing::CANNY; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Kuklina/application.hpp b/sample_Kuklina/application.hpp index 062bad2..51d7742 100644 --- a/sample_Kuklina/application.hpp +++ b/sample_Kuklina/application.hpp @@ -26,13 +26,16 @@ class Application WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; - cv::Rect saveButtonPlace; - cv::Rect cvtColorButtonPlace; - bool saveState; + cv::Rect saveButtonPlace; + cv::Rect cvtColorButtonPlace; + cv::Rect pixelButtonPlace; + cv::Rect cannyButtonPlace; + bool saveState; + Processing::FilterType filter; }; 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 processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter); int showFrame(const std::string &caption, const cv::Mat& src, cv::Mat& dst); friend void onButtonsOnOffClick(int eventId, int x, int y, @@ -40,7 +43,8 @@ class Application Application() { guiState.state = OnFilter; - guiState.saveState = false; + guiState.saveState = false; + guiState.filter = Processing::MEDIAN; }; private: diff --git a/sample_Kuklina/processing.cpp b/sample_Kuklina/processing.cpp index 71a7aa4..61edc64 100644 --- a/sample_Kuklina/processing.cpp +++ b/sample_Kuklina/processing.cpp @@ -6,19 +6,63 @@ using namespace cv; -void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) +void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter) { src.copyTo(dst); - - int random = rand () % 16 + 2; + int random = rand () % 16 + 2; cv::Rect region(src.rows/random, src.cols/random, src.rows/2, src.cols/2); Mat roi = dst(region); - const int kSize = 11; - medianBlur(roi, roi, kSize); - cvtColor(roi, roi, CV_RGB2GRAY, 0); + switch (filter) + { + case Processing::MEDIAN: + { + const int kSize = 11; + medianBlur(roi, roi, kSize); + break; + } + case Processing::CVT_CONVERT_GRAY: + { + Mat gray; + cvtColor(roi, gray, CV_BGR2GRAY); + int from_to[] = { 0, 0, 0, 1, 0, 2}; + mixChannels(&gray, 1, & roi, 1, from_to, 3); + break; + } + case Processing::PIXELIZED: + { + cv::Mat cir = cv::Mat::zeros(src.size(), CV_8UC1); + int bsize = 10; + + for (int i = 0; i < roi.rows; i += bsize) + { + for (int j = 0; j < roi.cols; j += bsize) + { + cv::Rect rect = cv::Rect(j, i, bsize, bsize) & + cv::Rect(0, 0, roi.cols, roi.rows); + + cv::Mat sub_dst(roi, rect); + sub_dst.setTo(cv::mean(src(rect))); + + cv::circle(cir, cv::Point(j+bsize/2, i+bsize/2), bsize/2-1, CV_RGB(255,255,255), -1, CV_AA ); + } + } + break; + } + case Processing::CANNY: + { + Mat gray, detected_edges; + cvtColor(roi, gray, CV_BGR2GRAY); + blur(gray, detected_edges, Size(3,3) ); + Canny( detected_edges, detected_edges, 30, 30*3,3); + roi = Scalar::all(0); + int from_to[] = { 0, 0, 0, 1, 0, 2}; + mixChannels(&detected_edges, 1, & roi, 1, from_to, 3); + break; + } + } rectangle(dst, region, Scalar(255, 0, 0)); } diff --git a/sample_Kuklina/processing.hpp b/sample_Kuklina/processing.hpp index 65f29fc..370be0c 100644 --- a/sample_Kuklina/processing.hpp +++ b/sample_Kuklina/processing.hpp @@ -5,5 +5,14 @@ class Processing { public: - void processFrame(const cv::Mat& src, cv::Mat& dst); + enum FilterType + { + MEDIAN, + CVT_CONVERT_GRAY, + PIXELIZED, + CANNY + }; + + void processFrame(const cv::Mat& src, cv::Mat& dst, Processing::FilterType filter); + };