From d91d8297462805ba4d38882a044b432a2b3fef2b Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 14:30:05 +0300 Subject: [PATCH 01/18] created my sample from template --- CMakeLists.txt | 2 +- sample_Shchedrin/CMakeLists.txt | 7 ++ sample_Shchedrin/application.cpp | 123 +++++++++++++++++++++++++++++++ sample_Shchedrin/application.hpp | 49 ++++++++++++ sample_Shchedrin/main.cpp | 44 +++++++++++ sample_Shchedrin/processing.cpp | 18 +++++ sample_Shchedrin/processing.hpp | 9 +++ 7 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 sample_Shchedrin/CMakeLists.txt create mode 100644 sample_Shchedrin/application.cpp create mode 100644 sample_Shchedrin/application.hpp create mode 100644 sample_Shchedrin/main.cpp create mode 100644 sample_Shchedrin/processing.cpp create mode 100644 sample_Shchedrin/processing.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a1949c..061049b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ set(LIBRARY_DEPS ${OpenCV_LIBS}) # BUILD add_subdirectory(sample_template) # Add you directory here -# add_subdirectory(sample_YOUR_NAME) +add_subdirectory(sample_Shchedrin) # REPORT message( STATUS "") diff --git a/sample_Shchedrin/CMakeLists.txt b/sample_Shchedrin/CMakeLists.txt new file mode 100644 index 0000000..65c5916 --- /dev/null +++ b/sample_Shchedrin/CMakeLists.txt @@ -0,0 +1,7 @@ +set(target "sample_Shchedrin") + +file(GLOB hdrs "*.hpp") +file(GLOB srcs "*.cpp") + +add_executable(${target} ${srcs} ${hdrs}) +target_link_libraries(${target} ${LIBRARY_DEPS}) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp new file mode 100644 index 0000000..4cc18d1 --- /dev/null +++ b/sample_Shchedrin/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_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp new file mode 100644 index 0000000..862cc3f --- /dev/null +++ b/sample_Shchedrin/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_Shchedrin/main.cpp b/sample_Shchedrin/main.cpp new file mode 100644 index 0000000..43ff041 --- /dev/null +++ b/sample_Shchedrin/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_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp new file mode 100644 index 0000000..bd51a88 --- /dev/null +++ b/sample_Shchedrin/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_Shchedrin/processing.hpp b/sample_Shchedrin/processing.hpp new file mode 100644 index 0000000..65f29fc --- /dev/null +++ b/sample_Shchedrin/processing.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +class Processing +{ + public: + void processFrame(const cv::Mat& src, cv::Mat& dst); +}; From cf3ec8df7f6348c8a203855464b752021c0065a7 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 14:50:13 +0300 Subject: [PATCH 02/18] set region as a parametr --- sample_Shchedrin/application.cpp | 7 ++++--- sample_Shchedrin/application.hpp | 2 +- sample_Shchedrin/processing.cpp | 3 +-- sample_Shchedrin/processing.hpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 4cc18d1..674b965 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -26,9 +26,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, cv::Rect region) { - processor.processFrame(src, dst); + processor.processFrame(src, dst, region); if (dst.empty()) { @@ -68,7 +68,8 @@ int Application::showFrame(const std::string &caption, } else if (guiState.state == OnFilter) { - processFrame(src, dst); + cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2); + processFrame(src, dst, region); } else { diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index 862cc3f..d119a4c 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -29,7 +29,7 @@ class Application }; 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, cv::Rect region); int showFrame(const std::string &caption, const cv::Mat& src, cv::Mat& dst); friend void onButtonsOnOffClick(int eventId, int x, int y, diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index bd51a88..a01cc5b 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -4,11 +4,10 @@ using namespace cv; -void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) +void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) { 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; diff --git a/sample_Shchedrin/processing.hpp b/sample_Shchedrin/processing.hpp index 65f29fc..3b99e7d 100644 --- a/sample_Shchedrin/processing.hpp +++ b/sample_Shchedrin/processing.hpp @@ -5,5 +5,5 @@ class Processing { public: - void processFrame(const cv::Mat& src, cv::Mat& dst); + void processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region); }; From 488f1b2595a11957d98daf65937b3598c4a0e253 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:00:43 +0300 Subject: [PATCH 03/18] now window moves --- sample_Shchedrin/application.cpp | 4 ++-- sample_Shchedrin/application.hpp | 2 +- sample_Shchedrin/main.cpp | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 674b965..fe310d7 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -60,7 +60,7 @@ int Application::drawButtons(Mat &display) } int Application::showFrame(const std::string &caption, - const Mat& src, Mat& dst) + const Mat& src, Mat& dst, int shift) { if (guiState.state == OffFilter) { @@ -68,7 +68,7 @@ int Application::showFrame(const std::string &caption, } else if (guiState.state == OnFilter) { - cv::Rect region(src.rows/4, src.cols/4, src.rows/2, src.cols/2); + cv::Rect region(shift % (src.cols/2), src.cols/4, src.rows/2, src.cols/2); processFrame(src, dst, region); } else diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index d119a4c..353b2e4 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -31,7 +31,7 @@ class Application int getFrame(const std::string &fileName, cv::Mat& src); int processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region); int showFrame(const std::string &caption, - const cv::Mat& src, cv::Mat& dst); + const cv::Mat& src, cv::Mat& dst, int shift); friend void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData); Application() diff --git a/sample_Shchedrin/main.cpp b/sample_Shchedrin/main.cpp index 43ff041..b1706f5 100644 --- a/sample_Shchedrin/main.cpp +++ b/sample_Shchedrin/main.cpp @@ -36,8 +36,10 @@ int main(int argc, const char **argv) char key = 0; Mat dst(src.rows, src.cols, src.type()); while (key != 27) // Esc - { - key = app.showFrame(caption, src, dst); + { + static int shift = 0; + shift++; + key = app.showFrame(caption, src, dst, shift); } return OK; From 934296274da6b8e9ff130a2cd2b318c28ad9e2aa Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:15:12 +0300 Subject: [PATCH 04/18] draw button --- sample_Shchedrin/application.cpp | 7 +++++++ sample_Shchedrin/application.hpp | 1 + 2 files changed, 8 insertions(+) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index fe310d7..8cb87ba 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -42,10 +42,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 +58,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 - 20, + guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index 353b2e4..65d1c3d 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -26,6 +26,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); From 8d2d2e5a531c25c2797d39314b7dd6a905cf751a Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:18:40 +0300 Subject: [PATCH 05/18] shifted label on save button --- sample_Shchedrin/application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 8cb87ba..706aa80 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -59,7 +59,7 @@ int Application::drawButtons(Mat &display) 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 - 20, + Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 35, guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); From fd7279c25304ab29d600fef9a8fc12a008674dff Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:26:15 +0300 Subject: [PATCH 06/18] now save button works --- sample_Shchedrin/application.cpp | 10 +++++++++- sample_Shchedrin/application.hpp | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 706aa80..adc02d3 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -88,7 +88,10 @@ int Application::showFrame(const std::string &caption, src.copyTo(srcRoi); Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows)); dst.copyTo(dstRoi); - + if(guiState.saveImage){ + guiState.saveImage = false; + imwrite("processed_image.png",display); + } drawButtons(display); namedWindow(caption); @@ -117,6 +120,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->saveImage = true; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index 65d1c3d..7ddf541 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -23,6 +23,7 @@ class Application }; struct GUIElementsState { + bool saveImage; WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; @@ -38,6 +39,7 @@ class Application Application() { guiState.state = OnFilter; + guiState.saveImage = false; }; private: From ea38d50f3d5f1c2f12dd3edfb27e94873fb7cdee Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:44:35 +0300 Subject: [PATCH 07/18] now filename is time --- sample_Shchedrin/application.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index adc02d3..e4bfdf3 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -1,6 +1,7 @@ #include "application.hpp" #include "processing.hpp" +#include #include using namespace cv; @@ -90,7 +91,16 @@ int Application::showFrame(const std::string &caption, dst.copyTo(dstRoi); if(guiState.saveImage){ guiState.saveImage = false; - imwrite("processed_image.png",display); + + time_t now = time(0); + struct tm tstruct; + char buf[80]; + char filename[100]; + tstruct = *localtime(&now); + strftime(buf, sizeof(buf), "%Y_%m_%d_%H_%M_%S", &tstruct); + sprintf (filename,"%s.png", buf); + printf("%s",filename); + imwrite(filename,display); } drawButtons(display); From dc443a974a09e26e02d6dbbe3cdc5e37fde3a881 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 15:52:21 +0300 Subject: [PATCH 08/18] added includes, removed output --- sample_Shchedrin/application.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index e4bfdf3..51b848e 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -2,6 +2,8 @@ #include "processing.hpp" #include +#include +#include #include using namespace cv; @@ -99,7 +101,6 @@ int Application::showFrame(const std::string &caption, tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%Y_%m_%d_%H_%M_%S", &tstruct); sprintf (filename,"%s.png", buf); - printf("%s",filename); imwrite(filename,display); } drawButtons(display); From 565bcb3bd5e72b531dc94c1515e54054b0e41dc4 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 16:18:33 +0300 Subject: [PATCH 09/18] added some buttons --- sample_Shchedrin/application.cpp | 60 ++++++++++++++++++++++++++++++-- sample_Shchedrin/application.hpp | 9 +++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 51b848e..58c600c 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -43,9 +43,13 @@ int Application::processFrame(const Mat& src, Mat& dst, cv::Rect region) 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.onButtonPlace = Rect(20 + 140 * 0, display.rows - 60, 120, 40); + guiState.offButtonPlace = Rect(20 + 140 * 1, display.rows - 60, 120, 40); + guiState.saveButtonPlace = Rect(20 + 140 * 2, display.rows - 60, 120, 40); + guiState.grayscaleButtonPlace = Rect(20 + 140 * 3, display.rows - 60, 120, 40); + guiState.pixelButtonPlace = Rect(20 + 140 * 4, display.rows - 60, 120, 40); + guiState.edgesButtonPlace = Rect(20 + 140 * 5, display.rows - 60, 120, 40); + rectangle(display, guiState.onButtonPlace, Scalar(128, 128, 128), CV_FILLED); rectangle(display, guiState.offButtonPlace, @@ -53,6 +57,29 @@ int Application::drawButtons(Mat &display) rectangle(display, guiState.saveButtonPlace, Scalar(128, 128, 128), CV_FILLED); + Scalar color(128,128,128); + if(guiState.grayEnabled){ + color = Scalar(128,200,128); + }else{ + color = Scalar(128,128,128); + } + rectangle(display, guiState.grayscaleButtonPlace, + color, CV_FILLED); + if(guiState.pixelEnabled){ + color = Scalar(128,200,128); + }else{ + color = Scalar(128,128,128); + } + rectangle(display, guiState.pixelButtonPlace, + color, CV_FILLED); + if(guiState.edgesEnabled){ + color = Scalar(128,200,128); + }else{ + color = Scalar(128,128,128); + } + rectangle(display, guiState.edgesButtonPlace, + color, CV_FILLED); + putText(display, "on", Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10), @@ -65,6 +92,18 @@ int Application::drawButtons(Mat &display) Point(guiState.saveButtonPlace.x + guiState.saveButtonPlace.width / 2 - 35, guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "gray", + Point(guiState.grayscaleButtonPlace.x + guiState.grayscaleButtonPlace.width / 2 - 35, + guiState.grayscaleButtonPlace.y + guiState.grayscaleButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "pixel", + Point(guiState.pixelButtonPlace.x + guiState.pixelButtonPlace.width / 2 - 35, + guiState.pixelButtonPlace.y + guiState.pixelButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "edges", + Point(guiState.edgesButtonPlace.x + guiState.edgesButtonPlace.width / 2 - 35, + guiState.edgesButtonPlace.y + guiState.edgesButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -136,6 +175,21 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->saveImage = true; return; } + if (onButtonClicked(elems->grayscaleButtonPlace, x, y)) + { + elems->grayEnabled = !elems->grayEnabled; + return; + } + if (onButtonClicked(elems->pixelButtonPlace, x, y)) + { + elems->pixelEnabled = !elems->pixelEnabled; + return; + } + if (onButtonClicked(elems->edgesButtonPlace, x, y)) + { + elems->edgesEnabled = !elems->edgesEnabled; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index 7ddf541..f7ccd76 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -24,10 +24,16 @@ class Application struct GUIElementsState { bool saveImage; + bool grayEnabled; + bool pixelEnabled; + bool edgesEnabled; WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; cv::Rect saveButtonPlace; + cv::Rect grayscaleButtonPlace; + cv::Rect pixelButtonPlace; + cv::Rect edgesButtonPlace; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); int getFrame(const std::string &fileName, cv::Mat& src); @@ -40,6 +46,9 @@ class Application { guiState.state = OnFilter; guiState.saveImage = false; + guiState.grayEnabled = false; + guiState.pixelEnabled = false; + guiState.edgesEnabled = false; }; private: From cbc8b52a624c55454c2478ceca421839db336d33 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 16:47:25 +0300 Subject: [PATCH 10/18] added grayscale filter --- sample_Shchedrin/application.cpp | 5 ++++- sample_Shchedrin/processing.cpp | 20 +++++++++++++++++++- sample_Shchedrin/processing.hpp | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 58c600c..916c2e0 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -31,8 +31,11 @@ int Application::getFrame(const std::string &fileName, Mat& src) int Application::processFrame(const Mat& src, Mat& dst, cv::Rect region) { - processor.processFrame(src, dst, region); + processor.applyEdges = guiState.edgesEnabled; + processor.applyPixel = guiState.pixelEnabled; + processor.applyGray = guiState.grayEnabled; + processor.processFrame(src, dst, region); if (dst.empty()) { return 1; diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index a01cc5b..1515806 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -9,9 +9,27 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) src.copyTo(dst); Mat roi = dst(region); - + if(applyEdges){ + } + if(applyPixel){ + } + if(applyGray){ + Mat buf; + cvtColor(roi, buf, CV_RGB2GRAY); + vector rgb; + rgb.push_back(buf); + rgb.push_back(buf); + rgb.push_back(buf); + merge(rgb, roi); + } const int kSize = 11; medianBlur(roi, roi, kSize); rectangle(dst, region, Scalar(255, 0, 0)); } + +Processing::Processing(){ + applyEdges = false; + applyGray = true; + applyPixel = false; +} diff --git a/sample_Shchedrin/processing.hpp b/sample_Shchedrin/processing.hpp index 3b99e7d..11fb3d6 100644 --- a/sample_Shchedrin/processing.hpp +++ b/sample_Shchedrin/processing.hpp @@ -5,5 +5,7 @@ class Processing { public: + bool applyGray, applyPixel, applyEdges; void processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region); + Processing(); }; From 4a31491b4165853d621f7710762f0c9497897a80 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 17:21:47 +0300 Subject: [PATCH 11/18] implemented canny --- sample_Shchedrin/processing.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index 1515806..709090d 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -1,6 +1,7 @@ #include "processing.hpp" #include +#include //TODO:delete using namespace cv; @@ -10,6 +11,15 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) Mat roi = dst(region); if(applyEdges){ + Mat buf; + cvtColor(roi, buf, CV_RGB2GRAY); + GaussianBlur(buf, buf, Size(3, 3), 1.6); + Canny(buf,buf,60,200); + vector rgb; + rgb.push_back(buf); + rgb.push_back(buf); + rgb.push_back(buf); + merge(rgb, roi); } if(applyPixel){ } @@ -23,13 +33,15 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) merge(rgb, roi); } const int kSize = 11; - medianBlur(roi, roi, kSize); + if(!applyEdges && !applyGray && !applyPixel){ + medianBlur(roi, roi, kSize); + } rectangle(dst, region, Scalar(255, 0, 0)); } Processing::Processing(){ applyEdges = false; - applyGray = true; + applyGray = false; applyPixel = false; } From 2b65a938d10446b924d6fc4e6d2d5b2d30982a27 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 17:32:02 +0300 Subject: [PATCH 12/18] added median button --- sample_Shchedrin/application.cpp | 23 +++++++++++++++++++++-- sample_Shchedrin/application.hpp | 2 ++ sample_Shchedrin/processing.cpp | 3 ++- sample_Shchedrin/processing.hpp | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 916c2e0..8f57792 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -34,7 +34,7 @@ int Application::processFrame(const Mat& src, Mat& dst, cv::Rect region) processor.applyEdges = guiState.edgesEnabled; processor.applyPixel = guiState.pixelEnabled; processor.applyGray = guiState.grayEnabled; - + processor.applyMedian = guiState.medianEnabled; processor.processFrame(src, dst, region); if (dst.empty()) { @@ -52,7 +52,7 @@ int Application::drawButtons(Mat &display) guiState.grayscaleButtonPlace = Rect(20 + 140 * 3, display.rows - 60, 120, 40); guiState.pixelButtonPlace = Rect(20 + 140 * 4, display.rows - 60, 120, 40); guiState.edgesButtonPlace = Rect(20 + 140 * 5, display.rows - 60, 120, 40); - + guiState.medianButtonPlace = Rect(20 + 140 * 6, display.rows - 60, 120, 40); rectangle(display, guiState.onButtonPlace, Scalar(128, 128, 128), CV_FILLED); rectangle(display, guiState.offButtonPlace, @@ -83,6 +83,14 @@ int Application::drawButtons(Mat &display) rectangle(display, guiState.edgesButtonPlace, color, CV_FILLED); + if(guiState.medianEnabled){ + color = Scalar(128,200,128); + }else{ + color = Scalar(128,128,128); + } + rectangle(display, guiState.medianButtonPlace, + color, CV_FILLED); + putText(display, "on", Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, guiState.onButtonPlace.y + guiState.onButtonPlace.height / 2 + 10), @@ -107,6 +115,12 @@ int Application::drawButtons(Mat &display) Point(guiState.edgesButtonPlace.x + guiState.edgesButtonPlace.width / 2 - 35, guiState.edgesButtonPlace.y + guiState.edgesButtonPlace.height / 2 + 10), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "median", + Point(guiState.medianButtonPlace.x + + guiState.medianButtonPlace.width / 2 - 55, + guiState.medianButtonPlace.y + + guiState.medianButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -193,6 +207,11 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->edgesEnabled = !elems->edgesEnabled; return; } + if (onButtonClicked(elems->medianButtonPlace, x, y)) + { + elems->medianEnabled = !elems->medianEnabled; + return; + } } bool onButtonClicked(cv::Rect buttonPlace, int x, int y) diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index f7ccd76..69354dc 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -27,6 +27,7 @@ class Application bool grayEnabled; bool pixelEnabled; bool edgesEnabled; + bool medianEnabled; WindowState state; cv::Rect onButtonPlace; cv::Rect offButtonPlace; @@ -34,6 +35,7 @@ class Application cv::Rect grayscaleButtonPlace; cv::Rect pixelButtonPlace; cv::Rect edgesButtonPlace; + cv::Rect medianButtonPlace; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); int getFrame(const std::string &fileName, cv::Mat& src); diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index 709090d..0f10098 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -22,6 +22,7 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) merge(rgb, roi); } if(applyPixel){ + } if(applyGray){ Mat buf; @@ -33,7 +34,7 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) merge(rgb, roi); } const int kSize = 11; - if(!applyEdges && !applyGray && !applyPixel){ + if(applyMedian){ medianBlur(roi, roi, kSize); } diff --git a/sample_Shchedrin/processing.hpp b/sample_Shchedrin/processing.hpp index 11fb3d6..4a3efa3 100644 --- a/sample_Shchedrin/processing.hpp +++ b/sample_Shchedrin/processing.hpp @@ -5,7 +5,7 @@ class Processing { public: - bool applyGray, applyPixel, applyEdges; + bool applyGray, applyPixel, applyEdges, applyMedian; void processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region); Processing(); }; From 8451f720393c2c0b0af5a771e80433f0221af7a6 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 17:44:13 +0300 Subject: [PATCH 13/18] added pixelisation --- sample_Shchedrin/processing.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index 0f10098..b375460 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -22,7 +22,13 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) merge(rgb, roi); } if(applyPixel){ - + int pixelSize = 10; + for(int i = 0; pixelSize * i < roi.rows; i++){ + for(int j = 0; pixelSize * j < roi.cols; j++){ + Rect square(j*pixelSize, i* pixelSize, pixelSize, pixelSize); + roi(square).setTo(mean(roi(square))); + } + } } if(applyGray){ Mat buf; From 2456aa6a0adde112a8cdf5ef02a448aa5b27e498 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 18:02:47 +0300 Subject: [PATCH 14/18] circles work --- sample_Shchedrin/processing.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index b375460..b0a585b 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -26,7 +26,12 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) for(int i = 0; pixelSize * i < roi.rows; i++){ for(int j = 0; pixelSize * j < roi.cols; j++){ Rect square(j*pixelSize, i* pixelSize, pixelSize, pixelSize); - roi(square).setTo(mean(roi(square))); + Point center(square.x + square.width/2, square.y + square.height /2); + Scalar color = mean(roi(square)); + roi(square).setTo(Scalar(0,0,0)); + //roi(square).setTo(color); + //Mat mask(roi.rows / pixelSize, roi.cols/pixelSize, CV_8UC3); + circle(roi(square), Point(pixelSize/2, pixelSize/2), pixelSize/2, color, -1); } } } From 9292c0e336afb22e4acb1a51979e1292a47ace46 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 18:04:50 +0300 Subject: [PATCH 15/18] cleaned --- sample_Shchedrin/processing.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index b0a585b..a9310c7 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -29,8 +29,6 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) Point center(square.x + square.width/2, square.y + square.height /2); Scalar color = mean(roi(square)); roi(square).setTo(Scalar(0,0,0)); - //roi(square).setTo(color); - //Mat mask(roi.rows / pixelSize, roi.cols/pixelSize, CV_8UC3); circle(roi(square), Point(pixelSize/2, pixelSize/2), pixelSize/2, color, -1); } } From aa7742789472a1aa836b31d4444790f54bd76db5 Mon Sep 17 00:00:00 2001 From: rashchedrin Date: Tue, 18 Aug 2015 18:23:53 +0300 Subject: [PATCH 16/18] trying to add camera, fails --- sample_Shchedrin/application.cpp | 4 ++++ sample_Shchedrin/application.hpp | 1 + sample_Shchedrin/main.cpp | 13 ++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sample_Shchedrin/application.cpp b/sample_Shchedrin/application.cpp index 8f57792..cf01078 100644 --- a/sample_Shchedrin/application.cpp +++ b/sample_Shchedrin/application.cpp @@ -16,6 +16,10 @@ int Application::parseArguments(int argc, const char **argv, return 1; } params.imgFileName = std::string(argv[1]); + params.useCamera = false; + if(std::string(argv[1]) == "--camera"){ + params.useCamera = true; + } return 0; } diff --git a/sample_Shchedrin/application.hpp b/sample_Shchedrin/application.hpp index 69354dc..a5f1cf4 100644 --- a/sample_Shchedrin/application.hpp +++ b/sample_Shchedrin/application.hpp @@ -20,6 +20,7 @@ class Application struct Parameters { std::string imgFileName; + bool useCamera; }; struct GUIElementsState { diff --git a/sample_Shchedrin/main.cpp b/sample_Shchedrin/main.cpp index b1706f5..6fa8af2 100644 --- a/sample_Shchedrin/main.cpp +++ b/sample_Shchedrin/main.cpp @@ -1,4 +1,5 @@ #include +#include "opencv2/opencv.hpp" #include #include "application.hpp" @@ -26,7 +27,12 @@ int main(int argc, const char **argv) } Mat src; - if (app.getFrame(params.imgFileName, src) != 0) + VideoCapture cap(0); + if(!cap.isOpened()) // check if we succeeded + return -1; + cout<<"Cam opened"<>src; + cout<<"ok"< Date: Tue, 18 Aug 2015 18:35:29 +0300 Subject: [PATCH 17/18] camera added --- sample_Shchedrin/main.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sample_Shchedrin/main.cpp b/sample_Shchedrin/main.cpp index 6fa8af2..1ebacfb 100644 --- a/sample_Shchedrin/main.cpp +++ b/sample_Shchedrin/main.cpp @@ -28,8 +28,10 @@ int main(int argc, const char **argv) Mat src; VideoCapture cap(0); - if(!cap.isOpened()) // check if we succeeded + if(!cap.isOpened()){ // check if we succeeded + cerr<<"Can not connect to camera."<>src; - cout<<"ok"<>src; + if(src.empty()){ + cout<<"Unable to get frame"< Date: Wed, 19 Aug 2015 13:49:02 +0300 Subject: [PATCH 18/18] renamed roi(square), hope will help --- sample_Shchedrin/processing.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sample_Shchedrin/processing.cpp b/sample_Shchedrin/processing.cpp index a9310c7..913ce06 100644 --- a/sample_Shchedrin/processing.cpp +++ b/sample_Shchedrin/processing.cpp @@ -26,10 +26,10 @@ void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, cv::Rect region) for(int i = 0; pixelSize * i < roi.rows; i++){ for(int j = 0; pixelSize * j < roi.cols; j++){ Rect square(j*pixelSize, i* pixelSize, pixelSize, pixelSize); - Point center(square.x + square.width/2, square.y + square.height /2); + Mat squareMat = roi(square); Scalar color = mean(roi(square)); - roi(square).setTo(Scalar(0,0,0)); - circle(roi(square), Point(pixelSize/2, pixelSize/2), pixelSize/2, color, -1); + squareMat.setTo(Scalar(0,0,0)); + circle(squareMat, Point(pixelSize/2, pixelSize/2), pixelSize/2, color, -1); } } }