From 24fdcec383e9925716fc1fb2d53e69adc5f75686 Mon Sep 17 00:00:00 2001 From: vihuhol Date: Tue, 18 Aug 2015 14:28:34 +0300 Subject: [PATCH 1/4] Add my sample --- CMakeLists.txt | 3 +- sample_semichev/CMakeLists.txt | 7 ++ sample_semichev/application.cpp | 123 ++++++++++++++++++++++++++++++++ sample_semichev/application.hpp | 49 +++++++++++++ sample_semichev/main.cpp | 44 ++++++++++++ sample_semichev/processing.cpp | 18 +++++ sample_semichev/processing.hpp | 9 +++ 7 files changed, 251 insertions(+), 2 deletions(-) create mode 100644 sample_semichev/CMakeLists.txt create mode 100644 sample_semichev/application.cpp create mode 100644 sample_semichev/application.hpp create mode 100644 sample_semichev/main.cpp create mode 100644 sample_semichev/processing.cpp create mode 100644 sample_semichev/processing.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a1949c..5f39b4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,7 @@ set(LIBRARY_DEPS ${OpenCV_LIBS}) # BUILD add_subdirectory(sample_template) -# Add you directory here -# add_subdirectory(sample_YOUR_NAME) +add_subdirectory(sample_semichev) # REPORT message( STATUS "") diff --git a/sample_semichev/CMakeLists.txt b/sample_semichev/CMakeLists.txt new file mode 100644 index 0000000..0e3d8ff --- /dev/null +++ b/sample_semichev/CMakeLists.txt @@ -0,0 +1,7 @@ +set(target "sample_semichev") + +file(GLOB hdrs "*.hpp") +file(GLOB srcs "*.cpp") + +add_executable(${target} ${srcs} ${hdrs}) +target_link_libraries(${target} ${LIBRARY_DEPS}) diff --git a/sample_semichev/application.cpp b/sample_semichev/application.cpp new file mode 100644 index 0000000..4cc18d1 --- /dev/null +++ b/sample_semichev/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_semichev/application.hpp b/sample_semichev/application.hpp new file mode 100644 index 0000000..862cc3f --- /dev/null +++ b/sample_semichev/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_semichev/main.cpp b/sample_semichev/main.cpp new file mode 100644 index 0000000..43ff041 --- /dev/null +++ b/sample_semichev/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_semichev/processing.cpp b/sample_semichev/processing.cpp new file mode 100644 index 0000000..bd51a88 --- /dev/null +++ b/sample_semichev/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_semichev/processing.hpp b/sample_semichev/processing.hpp new file mode 100644 index 0000000..65f29fc --- /dev/null +++ b/sample_semichev/processing.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +class Processing +{ + public: + void processFrame(const cv::Mat& src, cv::Mat& dst); +}; From d48b6b486080e822923eace30a1c0e6721fd4752 Mon Sep 17 00:00:00 2001 From: vihuhol Date: Tue, 18 Aug 2015 15:05:22 +0300 Subject: [PATCH 2/4] Add moving to median region --- sample_semichev/processing.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/sample_semichev/processing.cpp b/sample_semichev/processing.cpp index bd51a88..03d647e 100644 --- a/sample_semichev/processing.cpp +++ b/sample_semichev/processing.cpp @@ -3,16 +3,38 @@ #include using namespace cv; +int move = 0; +bool isMovingRight = true; void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) { src.copyTo(dst); + if (isMovingRight) + if (move >= 50) + isMovingRight = false; + else + { + cv::Rect region(src.rows/4 + move, src.cols/4, src.rows/2 + 10, src.cols/2 + 100); + move++; + Mat roi = dst(region); - 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); - const int kSize = 11; - medianBlur(roi, roi, kSize); + rectangle(dst, region, Scalar(255, 0, 0)); + } + else + if (move <= -50) + isMovingRight = true; + else + { + cv::Rect region(src.rows/4 + move, src.cols/4, src.rows/2 - 10, src.cols/2 - 100); + move--; + Mat roi = dst(region); - rectangle(dst, region, Scalar(255, 0, 0)); + const int kSize = 11; + medianBlur(roi, roi, kSize); + + rectangle(dst, region, Scalar(255, 0, 0)); + } } From 02e78a64ccfadda48e7280e918fca96eb9bba108 Mon Sep 17 00:00:00 2001 From: vihuhol Date: Tue, 18 Aug 2015 15:50:52 +0300 Subject: [PATCH 3/4] Add new button --- sample_semichev/application.cpp | 23 +++++++++++++++++++++++ sample_semichev/application.hpp | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/sample_semichev/application.cpp b/sample_semichev/application.cpp index 4cc18d1..d0705f5 100644 --- a/sample_semichev/application.cpp +++ b/sample_semichev/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(0, 0, 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 - 30, + guiState.saveButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -81,6 +88,17 @@ int Application::showFrame(const std::string &caption, Mat dstRoi = display(Rect(src.cols, 0, dst.cols, dst.rows)); dst.copyTo(dstRoi); + if (guiState.saveState) + { + time_t rawTime; + struct tm* timeInfo; + time(&rawTime); + timeInfo = localtime(&rawTime); + string str = std::to_string(rawTime); + imwrite("picture_" + str + ".png", display); + guiState.saveState = false; + } + drawButtons(display); namedWindow(caption); @@ -109,6 +127,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_semichev/application.hpp b/sample_semichev/application.hpp index 862cc3f..65c6eea 100644 --- a/sample_semichev/application.hpp +++ b/sample_semichev/application.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "processing.hpp" @@ -24,8 +25,10 @@ class Application struct GUIElementsState { WindowState state; + bool saveState; 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); @@ -37,6 +40,7 @@ class Application Application() { guiState.state = OnFilter; + guiState.saveState = false; }; private: From 9db28ca39c0b1c4e32d8138a0f49a9660e714b3b Mon Sep 17 00:00:00 2001 From: vihuhol Date: Tue, 18 Aug 2015 17:52:11 +0300 Subject: [PATCH 4/4] Add new filtres --- sample_semichev/application.cpp | 47 +++++++++++++- sample_semichev/application.hpp | 5 ++ sample_semichev/processing.cpp | 107 +++++++++++++++++++++++++++----- sample_semichev/processing.hpp | 9 ++- 4 files changed, 150 insertions(+), 18 deletions(-) diff --git a/sample_semichev/application.cpp b/sample_semichev/application.cpp index d0705f5..75bc858 100644 --- a/sample_semichev/application.cpp +++ b/sample_semichev/application.cpp @@ -28,7 +28,7 @@ int Application::getFrame(const std::string &fileName, Mat& src) int Application::processFrame(const Mat& src, Mat& dst) { - processor.processFrame(src, dst); + processor.processFrame(src, dst, guiState.filter); if (dst.empty()) { @@ -43,12 +43,22 @@ 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.grayButtonPlace = 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, Scalar(0, 0, 128), CV_FILLED); + rectangle(display, guiState.grayButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.pixelButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.cannyButtonPlace, + Scalar(128, 128, 128), CV_FILLED); putText(display, "on", Point(guiState.onButtonPlace.x + guiState.onButtonPlace.width / 2 - 15, @@ -62,6 +72,18 @@ 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.grayButtonPlace.x + guiState.onButtonPlace.width / 2 - 30, + guiState.grayButtonPlace.y + guiState.onButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "pixel", + Point(guiState.pixelButtonPlace.x + guiState.offButtonPlace.width / 2 - 30, + guiState.pixelButtonPlace.y + guiState.offButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + putText(display, "canny", + Point(guiState.cannyButtonPlace.x + guiState.saveButtonPlace.width / 2 - 40, + guiState.cannyButtonPlace.y + guiState.saveButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); return 0; } @@ -94,7 +116,9 @@ int Application::showFrame(const std::string &caption, struct tm* timeInfo; time(&rawTime); timeInfo = localtime(&rawTime); - string str = std::to_string(rawTime); + std::ostringstream oss; + oss << rawTime; + string str = oss.str(); imwrite("picture_" + str + ".png", display); guiState.saveState = false; } @@ -120,6 +144,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)) @@ -132,6 +157,24 @@ void onButtonsOnOffClick(int eventId, int x, int y, int flags, void *userData) elems->saveState = true; return; } + if (onButtonClicked(elems->grayButtonPlace, 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_semichev/application.hpp b/sample_semichev/application.hpp index 65c6eea..a9d0a49 100644 --- a/sample_semichev/application.hpp +++ b/sample_semichev/application.hpp @@ -26,9 +26,13 @@ class Application { WindowState state; bool saveState; + Processing::FilterType filter; cv::Rect onButtonPlace; cv::Rect offButtonPlace; cv::Rect saveButtonPlace; + cv::Rect grayButtonPlace; + cv::Rect pixelButtonPlace; + cv::Rect cannyButtonPlace; }; int parseArguments(int argc, const char **argv, Parameters ¶ms); int getFrame(const std::string &fileName, cv::Mat& src); @@ -41,6 +45,7 @@ class Application { guiState.state = OnFilter; guiState.saveState = false; + guiState.filter = Processing::MEDIAN; }; private: diff --git a/sample_semichev/processing.cpp b/sample_semichev/processing.cpp index 03d647e..ac38c5c 100644 --- a/sample_semichev/processing.cpp +++ b/sample_semichev/processing.cpp @@ -1,40 +1,117 @@ #include "processing.hpp" - +#include #include using namespace cv; int move = 0; bool isMovingRight = true; -void Processing::processFrame(const cv::Mat& src, cv::Mat& dst) +void Processing::processFrame(const cv::Mat& src, cv::Mat& dst, FilterType filter) { + cv::Rect region; src.copyTo(dst); if (isMovingRight) if (move >= 50) + { isMovingRight = false; + cv::Rect reg(src.rows/4 + move, src.cols/4, src.rows/2 + 10, src.cols/2 + 100); + region = reg; + } else { - cv::Rect region(src.rows/4 + move, src.cols/4, src.rows/2 + 10, src.cols/2 + 100); + cv::Rect reg(src.rows/4 + move, src.cols/4, src.rows/2 + 10, src.cols/2 + 100); + region = reg; move++; - Mat roi = dst(region); - - const int kSize = 11; - medianBlur(roi, roi, kSize); - - rectangle(dst, region, Scalar(255, 0, 0)); } else if (move <= -50) - isMovingRight = true; + { + isMovingRight = false; + cv::Rect reg(src.rows/4 + move, src.cols/4, src.rows/2 + 10, src.cols/2 + 100); + region = reg; + } else { - cv::Rect region(src.rows/4 + move, src.cols/4, src.rows/2 - 10, src.cols/2 - 100); + cv::Rect reg(src.rows/4 + move, src.cols/4, src.rows/2 - 10, src.cols/2 - 100); + region = reg; move--; - Mat roi = dst(region); + } + Mat roi = dst(region); + std::cout << region.x << " " << region.y << " " << region.width << " " << region.height << std::endl; - const int kSize = 11; + const int kSize = 11; + switch (filter) + { + case MEDIAN: + { medianBlur(roi, roi, kSize); - - rectangle(dst, region, Scalar(255, 0, 0)); + break; + } + case CVT_CONVERT_GRAY: + { + Mat gray; + cvtColor(roi, gray, CV_BGR2GRAY); + vector grays; + grays.push_back(gray); + grays.push_back(gray); + grays.push_back(gray); + merge(grays, roi); + break; + } + case PIXELIZED: + { + cv::Mat dst1 = cv::Mat::zeros(roi.size(), CV_8UC3); + cv::Mat cir = cv::Mat::zeros(roi.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(dst1, rect); + sub_dst.setTo(cv::mean(roi(rect))); + + cv::circle( + cir, + cv::Point(j+bsize/2, i+bsize/2), + bsize/2-1, + CV_RGB(255,255,255), -1, CV_AA + ); + } + } + + cv::Mat cir_32f; + cir.convertTo(cir_32f, CV_32F); + cv::normalize(cir_32f, cir_32f, 0, 1, cv::NORM_MINMAX); + + cv::Mat dst_32f; + dst1.convertTo(dst_32f, CV_32F); + + std::vector channels; + cv::split(dst_32f, channels); + for (int i = 0; i < channels.size(); ++i) + channels[i] = channels[i].mul(cir_32f); + + cv::merge(channels, dst_32f); + dst_32f.convertTo(roi, CV_8U); + break; + } + case CANNY: + { + Mat edges; + Canny(roi, edges, 60, 120, 3); + vector grays; + grays.push_back(edges); + grays.push_back(edges); + grays.push_back(edges); + merge(grays, roi); + break; } + } + + + rectangle(dst, region, Scalar(255, 0, 0)); } diff --git a/sample_semichev/processing.hpp b/sample_semichev/processing.hpp index 65f29fc..5e13658 100644 --- a/sample_semichev/processing.hpp +++ b/sample_semichev/processing.hpp @@ -5,5 +5,12 @@ 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, FilterType filter); };