diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a1949c..49b8785 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_Kozitsin) # REPORT message( STATUS "") diff --git a/sample_Kozitsin/CMakeLists.txt b/sample_Kozitsin/CMakeLists.txt new file mode 100644 index 0000000..5479157 --- /dev/null +++ b/sample_Kozitsin/CMakeLists.txt @@ -0,0 +1,7 @@ +set(target "sample_Kozitsin") + +file(GLOB hdrs "*.hpp") +file(GLOB srcs "*.cpp") + +add_executable(${target} ${srcs} ${hdrs}) +target_link_libraries(${target} ${LIBRARY_DEPS}) diff --git a/sample_Kozitsin/application.cpp b/sample_Kozitsin/application.cpp new file mode 100644 index 0000000..1694cbc --- /dev/null +++ b/sample_Kozitsin/application.cpp @@ -0,0 +1,240 @@ +#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::processFrameMedian(const Mat& src, Mat& dst) +{ + processor.processFrameMedian(src, dst); + + if (dst.empty()) + { + return 1; + } + + return 0; +} + +int Application::processFrameGrey(const Mat& src, Mat& dst) +{ + processor.processFrameGrey(src, dst); + + if (dst.empty()) + { + return 1; + } + + return 0; +} + +int Application::processFramePixel(const cv::Mat& src, cv::Mat& dst) +{ + processor.processFramePixel(src, dst); + + if (dst.empty()) + { + return 1; + } + + return 0; +} + +int Application::processFrameCanny(const Mat& src, Mat& dst) +{ + processor.processFrameCanny(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); + + guiState.saveButtonPlace = Rect(300, display.rows - 60, 120, 40); + + guiState.greyOnButtonPlace = Rect(20, display.rows - 120, 120, 40); + guiState.cannyButtonPlace = Rect(160, display.rows - 120, 120, 40); + guiState.pixelButtonPlace = Rect(300, display.rows - 120, 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.greyOnButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.cannyButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + rectangle(display, guiState.pixelButtonPlace, + Scalar(128, 128, 128), CV_FILLED); + + putText(display, "median", + 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); + + 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); + + putText(display, "grey", + Point(guiState.greyOnButtonPlace.x + guiState.greyOnButtonPlace.width / 2 - 25, + guiState.greyOnButtonPlace.y + guiState.greyOnButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + + putText(display, "canny", + Point(guiState.cannyButtonPlace.x + guiState.cannyButtonPlace.width / 2 - 25, + guiState.cannyButtonPlace.y + guiState.cannyButtonPlace.height / 2 + 10), + FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 0), 2); + + putText(display, "pixel", + Point(guiState.pixelButtonPlace.x + guiState.pixelButtonPlace.width / 2 - 25, + guiState.pixelButtonPlace.y + guiState.pixelButtonPlace.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) + { + processFrameMedian(src, dst); + } + else if (guiState.state == OnGrey) + { + processFrameGrey(src, dst); + } + else if (guiState.state == Canny) + { + processFrameCanny(src, dst); + } + else if (guiState.state == Pixel) + { + processFramePixel(src, dst); + } + else if (guiState.state == Saving) + { + isSaveEnabled = true; + guiState.state = OnFilter; + } + 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); + + if (isSaveEnabled) + { + imwrite("saving.png", display); + isSaveEnabled = false; + } + + 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; + } + + if (onButtonClicked(elems->greyOnButtonPlace, x, y)) + { + elems->state = Application::OnGrey; + return; + } + if (onButtonClicked(elems->cannyButtonPlace, x, y)) + { + elems->state = Application::Canny; + return; + } + if (onButtonClicked(elems->pixelButtonPlace, x, y)) + { + elems->state = Application::Pixel; + return; + } + if (onButtonClicked(elems->saveButtonPlace, x, y)) + { + elems->state = Application::Saving; + 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_Kozitsin/application.hpp b/sample_Kozitsin/application.hpp new file mode 100644 index 0000000..64475cc --- /dev/null +++ b/sample_Kozitsin/application.hpp @@ -0,0 +1,64 @@ +#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, + Saving, + OnGrey, + Canny, + Pixel + }; + struct Parameters + { + std::string imgFileName; + }; + struct GUIElementsState + { + WindowState state; + cv::Rect onButtonPlace; + cv::Rect offButtonPlace; + cv::Rect saveButtonPlace; + cv::Rect greyOnButtonPlace; + cv::Rect cannyButtonPlace; + cv::Rect pixelButtonPlace; + }; + + int parseArguments(int argc, const char **argv, Parameters ¶ms); + int getFrame(const std::string &fileName, cv::Mat& src); + int processFrameMedian(const cv::Mat& src, cv::Mat& dst); + int processFrameGrey(const cv::Mat& src, cv::Mat& dst); + int processFrameCanny(const cv::Mat& src, cv::Mat& dst); + int processFramePixel(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; + isSaveEnabled = false; + }; + + private: + Processing processor; + GUIElementsState guiState; + + bool isSaveEnabled; + int drawButtons(cv::Mat &display); + + friend bool onButtonClicked(cv::Rect buttonPlace, int x, int y); +}; diff --git a/sample_Kozitsin/main.cpp b/sample_Kozitsin/main.cpp new file mode 100644 index 0000000..43ff041 --- /dev/null +++ b/sample_Kozitsin/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_Kozitsin/processing.cpp b/sample_Kozitsin/processing.cpp new file mode 100644 index 0000000..881d98e --- /dev/null +++ b/sample_Kozitsin/processing.cpp @@ -0,0 +1,159 @@ +#include "processing.hpp" + +#include + +#include +#include +#include +#include + +using std::vector; +using namespace cv; + +void Processing::processFrameMedian(const cv::Mat& src, cv::Mat& dst) +{ + srand(time(NULL)); + + src.copyTo(dst); + + int x = rand() % src.rows; + int y = rand() % src.cols; + + while (x > 400 || y > 400) + { + x = rand() % src.rows; + y = rand() % src.cols; + } + + cv::Rect region(x, y, 100, 100); + Mat roi = dst(region); + + const int kSize = 11; + medianBlur(roi, roi, kSize); + + rectangle(dst, region, Scalar(255, 0, 0)); +} + +void Processing::processFrameGrey(const cv::Mat& src, cv::Mat& dst) +{ + srand(time(NULL)); + + src.copyTo(dst); + + int x = rand() % src.rows; + int y = rand() % src.cols; + + while (x > 400 || y > 400) + { + x = rand() % src.rows; + y = rand() % src.cols; + } + + cv::Rect region(x, y, 100, 100); + Mat roi = dst(region); + + Mat grey; + cvtColor(roi, grey, CV_BGR2GRAY, 1); + vector channels(3); + + channels[0] = grey; + channels[1] = grey; + channels[2] = grey; + + merge(channels, roi); + + rectangle(dst, region, Scalar(255, 0, 0)); +} + +void Processing::processFrameCanny(const cv::Mat& src, cv::Mat& dst) +{ + srand(time(NULL)); + + src.copyTo(dst); + + int x = rand() % src.rows; + int y = rand() % src.cols; + + while (x > 400 || y > 400) + { + x = rand() % src.rows; + y = rand() % src.cols; + } + + cv::Rect region(x, y, 100, 100); + Mat roi = dst(region); + + Mat edges; + Canny(roi, edges, 60, 120); + vector channels(3); + + channels[0] = edges; + channels[1] = edges; + channels[2] = edges; + + merge(channels, roi); + + rectangle(dst, region, Scalar(255, 0, 0)); +} + +void Processing::processFramePixel(const cv::Mat& src, cv::Mat& dst) +{ + srand(time(NULL)); + + src.copyTo(dst); + + int x, y; + do + { + x = rand() % src.rows; + y = rand() % src.cols; + } + while (x > 400 || y > 400); + + cv::Rect region(x, y, 100, 100); + + std::cout << region.x << " " << region.y << std::endl; + + Mat roi = dst(region); + + //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); + + Mat sub_src(roi, rect); + sub_src.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; + roi.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); +*/ + rectangle(dst, region, Scalar(255, 0, 0)); +} \ No newline at end of file diff --git a/sample_Kozitsin/processing.hpp b/sample_Kozitsin/processing.hpp new file mode 100644 index 0000000..619401a --- /dev/null +++ b/sample_Kozitsin/processing.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +class Processing +{ + public: + void processFrameMedian(const cv::Mat& src, cv::Mat& dst); + void processFrameGrey(const cv::Mat& src, cv::Mat& dst); + void processFrameCanny(const cv::Mat& src, cv::Mat& dst); + void processFramePixel(const cv::Mat& src, cv::Mat& dst); +};