From a99e643f7ab4cddaf8cbac726fd3682a60f84423 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 17:00:08 +0300 Subject: [PATCH 01/17] add grayscale filter --- include/image_processing.hpp | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index ca1f116..a2f8a8e 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "opencv2/core/core.hpp" @@ -15,4 +16,40 @@ class ImageProcessor { const int kernelSize) = 0; virtual cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) = 0; +}; + +class ImageProcessorImpl : public ImageProcessor +{ +public: + cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override { + + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::Mat dst_gray_roi; + cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); + std::vector channels(3); + std::for_each(channels.begin(), channels.end(), [&dst_gray_roi](cv::Mat elem){ + elem = dst_gray_roi; + }); + cv::Mat dst_roi; + cv::merge(channels, dst_roi); + dst_roi.copyTo(src_copy_roi); + return src_copy; + } + + cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { + + return cv::Mat(); + } + + cv::Mat + DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, + const int kernelSize) override { + return cv::Mat(); + } + + cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override { + return cv::Mat(); + } }; \ No newline at end of file From ab40bcdf2b03c06986def1f0abafe451f591568e Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 18:07:12 +0300 Subject: [PATCH 02/17] add main for only gray --- include/image_processing.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index a2f8a8e..166ad6e 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -29,9 +29,8 @@ class ImageProcessorImpl : public ImageProcessor cv::Mat dst_gray_roi; cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); std::vector channels(3); - std::for_each(channels.begin(), channels.end(), [&dst_gray_roi](cv::Mat elem){ - elem = dst_gray_roi; - }); + + std::fill(channels.begin(), channels.end(), dst_gray_roi); cv::Mat dst_roi; cv::merge(channels, dst_roi); dst_roi.copyTo(src_copy_roi); From 8abd2a4c846686398c5a2a4df2b6b213f9bf32be Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 19 Oct 2017 18:07:47 +0300 Subject: [PATCH 03/17] add main for only gray --- samples/imgproc_demo.cpp | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 samples/imgproc_demo.cpp diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp new file mode 100644 index 0000000..e40d02b --- /dev/null +++ b/samples/imgproc_demo.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +#include "opencv2/core.hpp" + +using namespace std; +using namespace cv; + +const char* kAbout = + "This is an empty application that can be treated as a template for your " + "own doing-something-cool applications."; + +const char* kOptions = + "{ @image | | image to process }" + "{ gray | | convert ROI to gray scale }" + "{ median | | apply median filter for ROI }" + "{ edges | | detect edges in ROI }" + "{ pix | | pixelize ROI }" + "{ h ? help usage | | print help message }"; +struct MouseCallbackState { + bool is_selection_started; + bool is_selection_finished; + Point point_first; + Point point_second; +} mouseCallbackState; +void onMouse(int event, int x, int y, int flag, void* param) +{ + if(event == cv::EVENT_LBUTTONDOWN) + { + mouseCallbackState.is_selection_started = true; + mouseCallbackState.is_selection_finished = false; + mouseCallbackState.point_first = cv::Point(x, y); + } + if(event == cv::EVENT_LBUTTONUP) + { + mouseCallbackState.is_selection_started = false; + mouseCallbackState.is_selection_finished = true; + mouseCallbackState.point_second = cv::Point(x, y); + } + if(event == cv::EVENT_MOUSEMOVE && !mouseCallbackState.is_selection_finished) + { + mouseCallbackState.point_second = cv::Point(x, y); + } +} + +int main(int argc, const char** argv) { + // Parse command line arguments. + CommandLineParser parser(argc, argv, kOptions); + parser.about(kAbout); + + + // If help option is given, print help message and exit. + if (parser.get("help")) { + parser.printMessage(); + return 0; + } + + + // Do something cool. + cv::namedWindow("input"); + cv::setMouseCallback("input", onMouse); + cv::Mat input = cv::imread("/home/luba/Downloads/Lenna.jpg"); + cv::Mat input_copy; + cv::Rect rect; + while(true) + { + input.copyTo(input_copy); + + if(mouseCallbackState.is_selection_started && !mouseCallbackState.is_selection_finished) { + rect = cv::Rect(mouseCallbackState.point_first.x, mouseCallbackState.point_first.y, + mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, + mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); + } + cv::rectangle(input_copy, rect, cv::Scalar(90, 108, 70)); + cv::imshow("input", input_copy); + char c = cv::waitKey(39); + if (c == 27) + break; + } + ImageProcessorImpl processor; + + if(parser.has("gray")){ + input = processor.CvtColor(input, rect); + } + else{ + cerr << "no flag" << endl; + } + cv::imshow("input", input); + cv::waitKey(0); + + return 0; +} From 678bab7dfb625b75ed3f2843435b549f9af81583 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 26 Oct 2017 16:46:21 +0300 Subject: [PATCH 04/17] implementation filters --- include/image_processing.hpp | 44 ++++++++++++++++++++++++++++++------ samples/imgproc_demo.cpp | 9 ++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/include/image_processing.hpp b/include/image_processing.hpp index 166ad6e..690c2c5 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -18,8 +18,7 @@ class ImageProcessor { const int kDivs) = 0; }; -class ImageProcessorImpl : public ImageProcessor -{ +class ImageProcessorImpl : public ImageProcessor { public: cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override { @@ -38,17 +37,48 @@ class ImageProcessorImpl : public ImageProcessor } cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { - - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::medianBlur(src_copy_roi, src_copy_roi, kSize); + return src_copy; } cv::Mat DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, const int kernelSize) override { - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_roi = src_copy(roi); + cv::Mat src_gray_roi; + cv::cvtColor(src_roi, src_gray_roi, CV_BGR2GRAY); + cv::Mat detected_edges; + cv::blur(src_gray_roi, src_gray_roi, cv::Size(filterSize, filterSize)); + cv::Canny(src_gray_roi, detected_edges, lowThreshold, lowThreshold + ratio, kernelSize); + cv::Mat dst; + src.copyTo(dst); + cv::Mat dst_roi = dst(roi); + dst_roi = cv::Scalar::all(0); + src_roi.copyTo(dst_roi, detected_edges); + return dst; } cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override { - return cv::Mat(); + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + int block_size_x = roi.width / kDivs; + int block_size_y = roi.height / kDivs; + for (int i = 0; i < src_copy_roi.rows; i += block_size_y) + for (int j = 0; j < src_copy_roi.cols; j += block_size_x) { + cv::Mat src_roi_block = src_copy_roi(cv::Rect(j, i, + j + block_size_x <= src_copy_roi.cols ? + block_size_x : src_copy_roi.cols - j, + i + block_size_y <= src_copy_roi.rows ? + block_size_y : src_copy_roi.rows - i)); + + cv::blur(src_roi_block, src_roi_block, cv::Size(block_size_x, block_size_y)); + } + return src_copy; } -}; \ No newline at end of file +}; diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp index e40d02b..c60a6af 100644 --- a/samples/imgproc_demo.cpp +++ b/samples/imgproc_demo.cpp @@ -84,6 +84,15 @@ int main(int argc, const char** argv) { if(parser.has("gray")){ input = processor.CvtColor(input, rect); } + else if(parser.has("pix")) { + input = processor.Pixelize(input, rect, 5); + } + else if(parser.has("edges")) { + input = processor.DetectEdges(input, rect, 5, 2, 3, 5); + } + else if(parser.has("median")) { + input = processor.Filter(input, rect, 5); + } else{ cerr << "no flag" << endl; } From 9efdad71d6835a8fd7d6dd54c5eef2e5c870de19 Mon Sep 17 00:00:00 2001 From: luba Date: Sun, 29 Oct 2017 16:15:37 +0300 Subject: [PATCH 05/17] detect --- include/detection.hpp | 40 +++++++++++++++++++++++++++++++++++++- samples/detection_demo.cpp | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 samples/detection_demo.cpp diff --git a/include/detection.hpp b/include/detection.hpp index 46930d3..f4b46e0 100644 --- a/include/detection.hpp +++ b/include/detection.hpp @@ -4,11 +4,49 @@ #include #include "opencv2/core/core.hpp" +#include "opencv2/objdetect.hpp" +#include "opencv2/highgui.hpp" +#include "opencv2/imgproc.hpp" + +#include + + +using namespace std; +using namespace cv; class Detector { public: - static std::shared_ptr CreateDetector(const std::string& name); + static std::shared_ptr CreateDetector(const std::string& name){ + if (name == "cascade") { + return std::make_shared(); + } + } virtual bool Init(const std::string& model_file_path) = 0; virtual void Detect(const cv::Mat& frame, std::vector& objects, std::vector& scores) = 0; }; + +class CascadeDetector : public Detector { +public: + virtual bool Init(const std::string& model_file_path){ + detector.load(model_file_path); + if (detector.empty() ) { //проверяет: загружен ли классификатор + return false; + } + else + return true; + } + virtual void Detect(const cv::Mat& frame, std::vector& objects, + std::vector& scores){ + if (detector.empty() ) { //проверяет: загружен ли классификатор + detector.detectMultiScale(frame, objects); + // scores[0] = objects.size(); + ///???? + } else return; + + } + +protected: + cv::CascadeClassifier detector; +}; + diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp new file mode 100644 index 0000000..9deb501 --- /dev/null +++ b/samples/detection_demo.cpp @@ -0,0 +1,38 @@ +#include +#include + +#include "opencv2/core.hpp" + +using namespace std; +using namespace cv; + +const char* kAbout = + "This is an empty application that can be treated as a template for your " + "own doing-something-cool applications."; + +const char* kOptions = + "{ i image | | image to process }" + "{ v video | | video to process }" + "{ c camera | | camera to get video from }" + "{ m model | | path to detector file }" + "{ h ? help usage | | print help message }"; + +int main(int argc, const char** argv) { + // Parse command line arguments. + CommandLineParser parser(argc, argv, kOptions); + parser.about(kAbout); + + // If help option is given, print help message and exit. + if (parser.get("help")) { + parser.printMessage(); + return 0; + } + + // Do something cool. + //cout << "This is empty template sample." << endl; + + CascadeDetector detector; + + + return 0; +} From 675fdf19592625ac98452ebf410d4c0b08f90660 Mon Sep 17 00:00:00 2001 From: temp110 Date: Mon, 30 Oct 2017 11:08:01 +0200 Subject: [PATCH 06/17] add video --- samples/detection_demo.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 9deb501..0816fc1 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -31,8 +31,30 @@ int main(int argc, const char** argv) { // Do something cool. //cout << "This is empty template sample." << endl; - CascadeDetector detector; - + CascadeDetector detector("../test/test_data/detection/cascades/intel_logo_cascade.xml"); + cv::VideoCapture video("../test/test_data/video/logo.mp4"); + std::vector objects; + std::vector scores; + + /*if(parser.has("v")){ + + } + else if(parser.has("i")){ + + } + else if(parser.has("c")){ + + } + else if(parser.has("m")){ + + }*/ + cv::Mat frame; + while(true){ + video >> frame; + if (!frame) + break; + detector.Detect(frame, objects, scores); + } return 0; } From 9c978fb12036267e497c8087ad49fbade228711c Mon Sep 17 00:00:00 2001 From: luba Date: Mon, 30 Oct 2017 21:25:22 +0300 Subject: [PATCH 07/17] fix detect --- include/detection.hpp | 16 ++++++++++------ include/image_processing.hpp | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/detection.hpp b/include/detection.hpp index f4b46e0..396e9bc 100644 --- a/include/detection.hpp +++ b/include/detection.hpp @@ -34,15 +34,19 @@ class CascadeDetector : public Detector { return false; } else - return true; + return true; } virtual void Detect(const cv::Mat& frame, std::vector& objects, std::vector& scores){ - if (detector.empty() ) { //проверяет: загружен ли классификатор - detector.detectMultiScale(frame, objects); - // scores[0] = objects.size(); - ///???? - } else return; + if (!detector.empty() ) { //проверяет: загружен ли классификатор + std::vector sc; + detector.detectMultiScale(frame, objects, sc); + std::copy(sc.begin(), sc.end(), scores.begin()); + } + else { + std::cerr << "error"; + exit(-1); + } } diff --git a/include/image_processing.hpp b/include/image_processing.hpp index 690c2c5..6ae04ca 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -32,6 +32,7 @@ class ImageProcessorImpl : public ImageProcessor { std::fill(channels.begin(), channels.end(), dst_gray_roi); cv::Mat dst_roi; cv::merge(channels, dst_roi); + cv::merge(dst_gray_roi, dst_gray_roi, dst_gray_roi, dst_roi); dst_roi.copyTo(src_copy_roi); return src_copy; } From 42a6eb0e32a91fe6dd7864d85ef98db5567b54d8 Mon Sep 17 00:00:00 2001 From: luba Date: Mon, 30 Oct 2017 22:03:54 +0300 Subject: [PATCH 08/17] fix image_process --- include/image_processing.hpp | 64 +++--------------------------- samples/detection_demo.cpp | 9 +++-- samples/imgproc_demo.cpp | 25 ++++++------ src/CMakeLists.txt | 2 +- src/image_processing.cpp | 75 ++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 78 deletions(-) create mode 100644 src/image_processing.cpp diff --git a/include/image_processing.hpp b/include/image_processing.hpp index 6ae04ca..f476261 100644 --- a/include/image_processing.hpp +++ b/include/image_processing.hpp @@ -20,66 +20,12 @@ class ImageProcessor { class ImageProcessorImpl : public ImageProcessor { public: - cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override { + cv::Mat CvtColor(const cv::Mat &src, const cv::Rect &roi) override; - cv::Mat src_copy; - src.copyTo(src_copy); - cv::Mat src_copy_roi = src_copy(roi); - cv::Mat dst_gray_roi; - cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); - std::vector channels(3); - - std::fill(channels.begin(), channels.end(), dst_gray_roi); - cv::Mat dst_roi; - cv::merge(channels, dst_roi); - cv::merge(dst_gray_roi, dst_gray_roi, dst_gray_roi, dst_roi); - dst_roi.copyTo(src_copy_roi); - return src_copy; - } - - cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override { - cv::Mat src_copy; - src.copyTo(src_copy); - cv::Mat src_copy_roi = src_copy(roi); - cv::medianBlur(src_copy_roi, src_copy_roi, kSize); - return src_copy; - } + cv::Mat Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) override; cv::Mat DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, - const int kernelSize) override { - cv::Mat src_copy; - src.copyTo(src_copy); - cv::Mat src_roi = src_copy(roi); - cv::Mat src_gray_roi; - cv::cvtColor(src_roi, src_gray_roi, CV_BGR2GRAY); - cv::Mat detected_edges; - cv::blur(src_gray_roi, src_gray_roi, cv::Size(filterSize, filterSize)); - cv::Canny(src_gray_roi, detected_edges, lowThreshold, lowThreshold + ratio, kernelSize); - cv::Mat dst; - src.copyTo(dst); - cv::Mat dst_roi = dst(roi); - dst_roi = cv::Scalar::all(0); - src_roi.copyTo(dst_roi, detected_edges); - return dst; - } - - cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override { - cv::Mat src_copy; - src.copyTo(src_copy); - cv::Mat src_copy_roi = src_copy(roi); - int block_size_x = roi.width / kDivs; - int block_size_y = roi.height / kDivs; - for (int i = 0; i < src_copy_roi.rows; i += block_size_y) - for (int j = 0; j < src_copy_roi.cols; j += block_size_x) { - cv::Mat src_roi_block = src_copy_roi(cv::Rect(j, i, - j + block_size_x <= src_copy_roi.cols ? - block_size_x : src_copy_roi.cols - j, - i + block_size_y <= src_copy_roi.rows ? - block_size_y : src_copy_roi.rows - i)); - - cv::blur(src_roi_block, src_roi_block, cv::Size(block_size_x, block_size_y)); - } - return src_copy; - } -}; + const int kernelSize) override; + cv::Mat Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) override; +}; \ No newline at end of file diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 0816fc1..4a2cab0 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "opencv2/core.hpp" @@ -31,8 +32,10 @@ int main(int argc, const char** argv) { // Do something cool. //cout << "This is empty template sample." << endl; - CascadeDetector detector("../test/test_data/detection/cascades/intel_logo_cascade.xml"); - cv::VideoCapture video("../test/test_data/video/logo.mp4"); + CascadeDetector detector; + //("../test/test_data/detection/cascades/intel_logo_cascade.xml"); + detector.Init("../test/test_data/detection/cascades/intel_logo_cascade.xml"); + cv::VideoCapture video("../test/test_data/video/pedestrians.mpg"); std::vector objects; std::vector scores; @@ -51,8 +54,6 @@ int main(int argc, const char** argv) { cv::Mat frame; while(true){ video >> frame; - if (!frame) - break; detector.Detect(frame, objects, scores); } diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp index c60a6af..cfca2c7 100644 --- a/samples/imgproc_demo.cpp +++ b/samples/imgproc_demo.cpp @@ -1,17 +1,12 @@ #include #include #include +#include #include -#include "opencv2/core.hpp" - using namespace std; using namespace cv; -const char* kAbout = - "This is an empty application that can be treated as a template for your " - "own doing-something-cool applications."; - const char* kOptions = "{ @image | | image to process }" "{ gray | | convert ROI to gray scale }" @@ -48,8 +43,6 @@ void onMouse(int event, int x, int y, int flag, void* param) int main(int argc, const char** argv) { // Parse command line arguments. CommandLineParser parser(argc, argv, kOptions); - parser.about(kAbout); - // If help option is given, print help message and exit. if (parser.get("help")) { @@ -61,7 +54,11 @@ int main(int argc, const char** argv) { // Do something cool. cv::namedWindow("input"); cv::setMouseCallback("input", onMouse); - cv::Mat input = cv::imread("/home/luba/Downloads/Lenna.jpg"); + std::string filePath; + if (parser.has("image"){ + filePath = parser.get("image"); + } + cv::Mat input = cv::imread(filePath); cv::Mat input_copy; cv::Rect rect; while(true) @@ -70,14 +67,14 @@ int main(int argc, const char** argv) { if(mouseCallbackState.is_selection_started && !mouseCallbackState.is_selection_finished) { rect = cv::Rect(mouseCallbackState.point_first.x, mouseCallbackState.point_first.y, - mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, - mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); + mouseCallbackState.point_second.x - mouseCallbackState.point_first.x, + mouseCallbackState.point_second.y - mouseCallbackState.point_first.y); } cv::rectangle(input_copy, rect, cv::Scalar(90, 108, 70)); cv::imshow("input", input_copy); - char c = cv::waitKey(39); - if (c == 27) - break; + char c = cv::waitKey(39); + if (c == 27) + break; } ImageProcessorImpl processor; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f5122c9..f9ec5af 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,7 @@ file(GLOB incl "../include/*.h*") file(GLOB hdrs "*.h*") file(GLOB srcs "*.cpp") -add_library(${target} STATIC ${srcs} ${hdrs} ${incl}) +add_library(${target} STATIC ${srcs} ${hdrs} ${incl} image_processing.cpp) if (UNIX) target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) endif (UNIX) diff --git a/src/image_processing.cpp b/src/image_processing.cpp new file mode 100644 index 0000000..badd196 --- /dev/null +++ b/src/image_processing.cpp @@ -0,0 +1,75 @@ + +#include +#include +#include +#include + +#include "opencv2/core/core.hpp" + + + + cv::Mat ImageProcessorImpl::CvtColor(const cv::Mat &src, const cv::Rect &roi) { + + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::Mat dst_gray_roi; + cv::cvtColor(src_copy_roi, dst_gray_roi, CV_BGR2GRAY); + std::vector channels(3); + + std::fill(channels.begin(), channels.end(), dst_gray_roi); + cv::Mat dst_roi; + cv::merge(channels, dst_roi); + // cv::merge(dst_gray_roi, dst_gray_roi, dst_gray_roi, dst_roi); + dst_roi.copyTo(src_copy_roi); + return src_copy; + } + + cv::Mat ImageProcessorImpl::Filter(const cv::Mat &src, const cv::Rect &roi, const int kSize) { + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + cv::medianBlur(src_copy_roi, src_copy_roi, kSize); + return src_copy; + } + + cv::Mat ImageProcessorImpl:: + DetectEdges(const cv::Mat &src, const cv::Rect &roi, const int filterSize, const int lowThreshold, const int ratio, + const int kernelSize) { + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_roi = src_copy(roi); + cv::Mat src_gray_roi; + cv::cvtColor(src_roi, src_gray_roi, CV_BGR2GRAY); + cv::Mat detected_edges; + cv::blur(src_gray_roi, src_gray_roi, cv::Size(filterSize, filterSize)); + cv::Canny(src_gray_roi, detected_edges, lowThreshold, lowThreshold + ratio, kernelSize); + cv::Mat dst; + src.copyTo(dst); + cv::Mat dst_roi = dst(roi); + dst_roi = cv::Scalar::all(0); + src_roi.copyTo(dst_roi, detected_edges); + return dst; + } + + cv::Mat ImageProcessorImpl::Pixelize(const cv::Mat &src, const cv::Rect &roi, const int kDivs) { + cv::Mat src_copy; + src.copyTo(src_copy); + cv::Mat src_copy_roi = src_copy(roi); + int block_size_x = roi.width / kDivs; + int block_size_y = roi.height / kDivs; + for (int i = 0; i < src_copy_roi.rows; i += block_size_y) + for (int j = 0; j < src_copy_roi.cols; j += block_size_x) { + cv::Mat src_roi_block = src_copy_roi(cv::Rect(j, i, + j + block_size_x <= src_copy_roi.cols ? + block_size_x : src_copy_roi.cols - j, + i + block_size_y <= src_copy_roi.rows ? + block_size_y : src_copy_roi.rows - i)); + + cv::blur(src_roi_block, src_roi_block, cv::Size(block_size_x, block_size_y)); + } + return src_copy; + } + + + From 4187af8eff2dccba7a4419a70272134b1610ae1b Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 2 Nov 2017 15:53:01 +0300 Subject: [PATCH 09/17] add models HAAR and LBP, add sample --- include/detection.hpp | 40 ++----- logo_cascade/intel_HAAR.xml | 203 ++++++++++++++++++++++++++++++++++++ logo_cascade/intel_LBP.xml | 118 +++++++++++++++++++++ samples/detection_demo.cpp | 75 +++++++++---- src/detection.cpp | 28 ++++- 5 files changed, 407 insertions(+), 57 deletions(-) create mode 100644 logo_cascade/intel_HAAR.xml create mode 100644 logo_cascade/intel_LBP.xml diff --git a/include/detection.hpp b/include/detection.hpp index 396e9bc..a38fee0 100644 --- a/include/detection.hpp +++ b/include/detection.hpp @@ -3,24 +3,18 @@ #include #include -#include "opencv2/core/core.hpp" -#include "opencv2/objdetect.hpp" -#include "opencv2/highgui.hpp" -#include "opencv2/imgproc.hpp" - -#include - +#include +#include +#include +#include using namespace std; using namespace cv; + class Detector { public: - static std::shared_ptr CreateDetector(const std::string& name){ - if (name == "cascade") { - return std::make_shared(); - } - } + static std::shared_ptr CreateDetector(const std::string& name); virtual bool Init(const std::string& model_file_path) = 0; virtual void Detect(const cv::Mat& frame, std::vector& objects, std::vector& scores) = 0; @@ -28,27 +22,9 @@ class Detector { class CascadeDetector : public Detector { public: - virtual bool Init(const std::string& model_file_path){ - detector.load(model_file_path); - if (detector.empty() ) { //проверяет: загружен ли классификатор - return false; - } - else - return true; - } + virtual bool Init(const std::string& model_file_path); virtual void Detect(const cv::Mat& frame, std::vector& objects, - std::vector& scores){ - if (!detector.empty() ) { //проверяет: загружен ли классификатор - std::vector sc; - detector.detectMultiScale(frame, objects, sc); - std::copy(sc.begin(), sc.end(), scores.begin()); - } - else { - std::cerr << "error"; - exit(-1); - } - - } + std::vector& scores); protected: cv::CascadeClassifier detector; diff --git a/logo_cascade/intel_HAAR.xml b/logo_cascade/intel_HAAR.xml new file mode 100644 index 0000000..975b99d --- /dev/null +++ b/logo_cascade/intel_HAAR.xml @@ -0,0 +1,203 @@ + + + + BOOST + HAAR + 32 + 32 + + GAB + 9.9500000476837158e-01 + 1.0000000149011612e-01 + 9.4999999999999996e-01 + 1 + 100 + + 0 + 1 + BASIC + 5 + + + <_> + 1 + 9.9700450897216797e-01 + + <_> + + 0 -1 9 2.6727295480668545e-03 + + -1. 9.9700450897216797e-01 + + <_> + 2 + 1.6233325004577637e-02 + + <_> + + 0 -1 8 -2.8224483132362366e-02 + + 9.7911489009857178e-01 -9.5910018682479858e-01 + <_> + + 0 -1 5 1.6519669443368912e-03 + + -9.4611197710037231e-01 9.7533351182937622e-01 + + <_> + 2 + -2.3966401815414429e-01 + + <_> + + 0 -1 2 -1.8159887194633484e-01 + + 8.1481480598449707e-01 -9.9596774578094482e-01 + <_> + + 0 -1 0 2.7227598428726196e-01 + + -9.9537020921707153e-01 7.5630372762680054e-01 + + <_> + 4 + -9.0893095731735229e-01 + + <_> + + 0 -1 7 5.1844924688339233e-02 + + -9.2384767532348633e-01 2.1568627655506134e-01 + <_> + + 0 -1 1 4.7272067517042160e-02 + + -8.6901932954788208e-01 5.2460169792175293e-01 + <_> + + 0 -1 4 -5.4597020149230957e-02 + + 4.5749512314796448e-01 -9.4674235582351685e-01 + <_> + + 0 -1 3 9.5191076397895813e-02 + + -9.6718007326126099e-01 4.7605302929878235e-01 + + <_> + 4 + -9.4207423925399780e-01 + + <_> + + 0 -1 10 -2.4780812673270702e-03 + + 1.2500000000000000e-01 -9.4238680601119995e-01 + <_> + + 0 -1 11 2.1410051733255386e-02 + + -9.3719536066055298e-01 3.8065698742866516e-01 + <_> + + 0 -1 6 -1.3540741056203842e-03 + + 5.3382533788681030e-01 -9.2854446172714233e-01 + <_> + + 0 -1 12 -1.2107914313673973e-02 + + 4.0368261933326721e-01 -9.4481426477432251e-01 + + <_> + + <_> + 1 0 30 18 -1. + <_> + 1 9 30 9 2. + 0 + <_> + + <_> + 3 0 2 30 -1. + <_> + 3 10 2 10 3. + 0 + <_> + + <_> + 4 16 21 16 -1. + <_> + 4 24 21 8 2. + 0 + <_> + + <_> + 10 1 12 14 -1. + <_> + 10 8 12 7 2. + 0 + <_> + + <_> + 11 18 7 12 -1. + <_> + 11 24 7 6 2. + 0 + <_> + + <_> + 12 15 2 2 -1. + <_> + 13 15 1 2 2. + 0 + <_> + + <_> + 13 16 2 1 -1. + <_> + 14 16 1 1 2. + 0 + <_> + + <_> + 14 1 6 10 -1. + <_> + 14 6 6 5 2. + 0 + <_> + + <_> + 14 10 6 12 -1. + <_> + 14 14 6 4 3. + 0 + <_> + + <_> + 15 14 2 3 -1. + <_> + 16 14 1 3 2. + 0 + <_> + + <_> + 15 17 2 3 -1. + <_> + 15 18 2 1 3. + 0 + <_> + + <_> + 23 9 9 16 -1. + <_> + 26 9 3 16 3. + 0 + <_> + + <_> + 26 1 6 21 -1. + <_> + 29 1 3 21 2. + 0 + diff --git a/logo_cascade/intel_LBP.xml b/logo_cascade/intel_LBP.xml new file mode 100644 index 0000000..4b03540 --- /dev/null +++ b/logo_cascade/intel_LBP.xml @@ -0,0 +1,118 @@ + + + + BOOST + LBP + 32 + 32 + + GAB + 9.9500000476837158e-01 + 1.0000000149011612e-01 + 9.4999999999999996e-01 + 1 + 100 + + 256 + 1 + 5 + + + <_> + 1 + 9.2307692766189575e-01 + + <_> + + 0 -1 2 -243210785 -787906557 -2147483628 -251559935 + -2004832114 -1744175104 -2139094902 -1710022005 + + -1. 9.2307692766189575e-01 + + <_> + 1 + 8.5185188055038452e-01 + + <_> + + 0 -1 5 -711997505 -685438579 -1589539441 -14554131 + -525422401 -1316317151 -775886449 -2561 + + -1. 8.5185188055038452e-01 + + <_> + 2 + -1.9843308627605438e-01 + + <_> + + 0 -1 3 -95423497 -546619001 336412677 -742678013 704878731 + -879050368 -721237617 -7360593 + + -9.8792755603790283e-01 7.7358490228652954e-01 + <_> + + 0 -1 1 -715796001 2035925121 809877593 -782286833 + -1868516725 -1738399606 -486027045 -1053973 + + -9.7201800346374512e-01 8.7086659669876099e-01 + + <_> + 2 + -2.9111653566360474e-01 + + <_> + + 0 -1 7 -687219745 -108445629 402928799 2009842059 + -2061381443 -88481708 1237551339 -1056853 + + -9.9594318866729736e-01 7.1929824352264404e-01 + <_> + + 0 -1 0 -41550371 -249867389 1152347 -680009681 -255277921 + -1437676915 -1056708165 -37750337 + + -9.8999285697937012e-01 7.0482665300369263e-01 + + <_> + 2 + -3.8104116916656494e-01 + + <_> + + 0 -1 6 -537920513 -1001164153 860206851 831950969 -519970677 + -207503230 -931091729 -1049861 + + -9.9590164422988892e-01 5.8064514398574829e-01 + <_> + + 0 -1 4 -237505057 1430488223 -725499767 -184036437 + -628901445 -107380601 -97681173 -72363589 + + -9.6168631315231323e-01 8.2039451599121094e-01 + + <_> + + 7 1 8 10 + <_> + + 9 3 5 8 + <_> + + 11 12 5 3 + <_> + + 12 14 1 1 + <_> + + 12 15 1 1 + <_> + + 15 14 1 1 + <_> + + 15 15 1 2 + <_> + + 15 16 1 2 + diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 4a2cab0..9fb34fb 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -1,16 +1,8 @@ -#include -#include -#include - -#include "opencv2/core.hpp" +#include "detection.hpp" using namespace std; using namespace cv; -const char* kAbout = - "This is an empty application that can be treated as a template for your " - "own doing-something-cool applications."; - const char* kOptions = "{ i image | | image to process }" "{ v video | | video to process }" @@ -21,7 +13,6 @@ const char* kOptions = int main(int argc, const char** argv) { // Parse command line arguments. CommandLineParser parser(argc, argv, kOptions); - parser.about(kAbout); // If help option is given, print help message and exit. if (parser.get("help")) { @@ -29,33 +20,77 @@ int main(int argc, const char** argv) { return 0; } - // Do something cool. - //cout << "This is empty template sample." << endl; - CascadeDetector detector; //("../test/test_data/detection/cascades/intel_logo_cascade.xml"); - detector.Init("../test/test_data/detection/cascades/intel_logo_cascade.xml"); - cv::VideoCapture video("../test/test_data/video/pedestrians.mpg"); + detector.Init("/home/luba/github/itseez-ss-2016-practice/logo_cascade/intel_LBP.xml"); + + cv::Mat frame1; std::vector objects; std::vector scores; /*if(parser.has("v")){ + std::string filePath; + filePath = parser.get("v"); + cv::VideoCapture video(filePath); + while(true){ + video >> frame1; + detector.Detect(frame1, objects, scores); + } } else if(parser.has("i")){ - + std::string filePath; + filePath = parser.get("i"); + cv::Mat input = cv::imread(filePath); + detector.Detect(input, objects, scores); } else if(parser.has("c")){ + CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); + assert( capture ); + IplImage* frame=0; + cvNamedWindow("capture", CV_WINDOW_AUTOSIZE); + printf("[i] press Esc for quit!\n\n"); + while(true) {// получаем кадр + frame = cvQueryFrame(capture); + detector.Detect(frame1, objects, scores); + cvShowImage("capture", frame); + char c = cvWaitKey(33); + if (c == 27) { // нажата ESC + break; + } + } + cvReleaseCapture( &capture ); + cvDestroyWindow("capture"); } else if(parser.has("m")){ - + std::string filePathDetector; + filePathDetector = parser.has("m"); + detector.Init( filePathDetector); }*/ - cv::Mat frame; - while(true){ - video >> frame; + + VideoCapture cap(0); // open the default camera + if(!cap.isOpened()) // check if we succeeded + return -1; + Mat frame; + //namedWindow("capture", CV_WINDOW_AUTOSIZE); + + while(true) { + cap >> frame; detector.Detect(frame, objects, scores); + for(const auto& rect : objects){ + rectangle(frame, rect, Scalar(250, 150, 10)); + } + imshow("capture", frame); + char c = waitKey(33); + if (c == 27) { // нажата ESC + break; + } + objects.clear(); } + cap.release(); return 0; } + + diff --git a/src/detection.cpp b/src/detection.cpp index 15e7fd1..3114fd0 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -1,13 +1,31 @@ -#include "detection.hpp" - #include +#include "detection.hpp" using std::string; using std::shared_ptr; using namespace cv; shared_ptr Detector::CreateDetector(const string& name) { - std::cerr << "Failed to create detector with name '" << name << "'" - << std::endl; - return nullptr; + if (name == "cascade") { + return std::make_shared(); + } +} + + +bool CascadeDetector::Init(const std::string &model_file_path) { + detector.load(model_file_path); + return detector.empty(); +} + +void CascadeDetector::Detect(const cv::Mat &frame, std::vector &objects, std::vector &scores) { + if (!detector.empty() ) { + std::vector sc; + detector.detectMultiScale(frame, objects, sc); + scores.resize(sc.size()); + std::copy(sc.begin(), sc.end(), scores.begin()); + } + else { + std::cerr << "Error"; + exit(-1); + } } From e6938d20e542d3a98f012c7d6e47a96c9ae46098 Mon Sep 17 00:00:00 2001 From: luba Date: Thu, 2 Nov 2017 20:59:59 +0300 Subject: [PATCH 10/17] add parser --- samples/detection_demo.cpp | 110 +++++++++++++++++++------------------ src/detection.cpp | 2 +- 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 9fb34fb..46cd0be 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -1,3 +1,4 @@ +#include #include "detection.hpp" using namespace std; @@ -15,80 +16,81 @@ int main(int argc, const char** argv) { CommandLineParser parser(argc, argv, kOptions); // If help option is given, print help message and exit. - if (parser.get("help")) { + if (parser.get("h")) { parser.printMessage(); return 0; } CascadeDetector detector; - //("../test/test_data/detection/cascades/intel_logo_cascade.xml"); - detector.Init("/home/luba/github/itseez-ss-2016-practice/logo_cascade/intel_LBP.xml"); - cv::Mat frame1; std::vector objects; std::vector scores; - /*if(parser.has("v")){ - std::string filePath; - filePath = parser.get("v"); - cv::VideoCapture video(filePath); + std::string filePath; - while(true){ - video >> frame1; - detector.Detect(frame1, objects, scores); - } + if(parser.has("m")){ + std::string filePathDetector; + filePathDetector = parser.get("m"); + detector.Init(filePathDetector); } - else if(parser.has("i")){ + + if(parser.has("i")){ std::string filePath; - filePath = parser.get("i"); - cv::Mat input = cv::imread(filePath); + filePath = parser.get("i"); + Mat input = imread(filePath); detector.Detect(input, objects, scores); } - else if(parser.has("c")){ + else if(parser.has("v")){ + Mat frame; + std::string filePath; + filePath = parser.get("v"); + cv::VideoCapture video(filePath); - CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); - assert( capture ); - IplImage* frame=0; - cvNamedWindow("capture", CV_WINDOW_AUTOSIZE); - printf("[i] press Esc for quit!\n\n"); - while(true) {// получаем кадр - frame = cvQueryFrame(capture); - detector.Detect(frame1, objects, scores); - cvShowImage("capture", frame); - char c = cvWaitKey(33); - if (c == 27) { // нажата ESC + while(true) { + video >> frame; + detector.Detect(frame, objects, scores); + for(const auto& rect : objects){ + rectangle(frame, rect, Scalar(250, 150, 10)); + } + imshow("capture", frame); + char c = waitKey(33); + if (c == 27) { break; } + objects.clear(); } - cvReleaseCapture( &capture ); - cvDestroyWindow("capture"); + video.release(); } - else if(parser.has("m")){ - std::string filePathDetector; - filePathDetector = parser.has("m"); - detector.Init( filePathDetector); - }*/ + else if(parser.has("c")){ - VideoCapture cap(0); // open the default camera - if(!cap.isOpened()) // check if we succeeded - return -1; - Mat frame; - //namedWindow("capture", CV_WINDOW_AUTOSIZE); - - while(true) { - cap >> frame; - detector.Detect(frame, objects, scores); - for(const auto& rect : objects){ - rectangle(frame, rect, Scalar(250, 150, 10)); - } - imshow("capture", frame); - char c = waitKey(33); - if (c == 27) { // нажата ESC - break; - } - objects.clear(); - } - cap.release(); + VideoCapture cap(0); + if(!cap.isOpened()) + return -1; + Mat frame; + if (detector.Init(parser.get("m"))) { + while (true) { + cap >> frame; + detector.Detect(frame, objects, scores); + for (const auto &rect : objects) { + rectangle(frame, rect, Scalar(250, 150, 10)); + } + imshow("capture", frame); + char c = waitKey(33); + if (c == 27) { + break; + } + objects.clear(); + } + cap.release(); + } + else{ + cerr << "Error load model"; + return -1; + } + } + else{ + cerr << "no flag" << endl; + } return 0; } diff --git a/src/detection.cpp b/src/detection.cpp index 3114fd0..301c06c 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -18,7 +18,7 @@ bool CascadeDetector::Init(const std::string &model_file_path) { } void CascadeDetector::Detect(const cv::Mat &frame, std::vector &objects, std::vector &scores) { - if (!detector.empty() ) { + if (!detector.empty()) { std::vector sc; detector.detectMultiScale(frame, objects, sc); scores.resize(sc.size()); From 1098d3ea7851f70a894e136900821b82f659aa70 Mon Sep 17 00:00:00 2001 From: luba Date: Fri, 3 Nov 2017 15:49:59 +0300 Subject: [PATCH 11/17] final lab3 --- samples/detection_demo.cpp | 18 +++++++++--------- samples/imgproc_demo.cpp | 2 +- src/detection.cpp | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 46cd0be..048d091 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -31,11 +31,17 @@ int main(int argc, const char** argv) { if(parser.has("m")){ std::string filePathDetector; filePathDetector = parser.get("m"); - detector.Init(filePathDetector); + if(!detector.Init(filePathDetector)){ + cerr << "Error init detector" << endl; + return -1; + }; } - + else{ + cerr << "enter key m" << endl; + return -1; + } + if(parser.has("i")){ - std::string filePath; filePath = parser.get("i"); Mat input = imread(filePath); detector.Detect(input, objects, scores); @@ -67,7 +73,6 @@ int main(int argc, const char** argv) { if(!cap.isOpened()) return -1; Mat frame; - if (detector.Init(parser.get("m"))) { while (true) { cap >> frame; detector.Detect(frame, objects, scores); @@ -82,11 +87,6 @@ int main(int argc, const char** argv) { objects.clear(); } cap.release(); - } - else{ - cerr << "Error load model"; - return -1; - } } else{ cerr << "no flag" << endl; diff --git a/samples/imgproc_demo.cpp b/samples/imgproc_demo.cpp index cfca2c7..4466d4c 100644 --- a/samples/imgproc_demo.cpp +++ b/samples/imgproc_demo.cpp @@ -55,7 +55,7 @@ int main(int argc, const char** argv) { cv::namedWindow("input"); cv::setMouseCallback("input", onMouse); std::string filePath; - if (parser.has("image"){ + if (parser.has("image")){ filePath = parser.get("image"); } cv::Mat input = cv::imread(filePath); diff --git a/src/detection.cpp b/src/detection.cpp index 301c06c..9d89b73 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -14,7 +14,7 @@ shared_ptr Detector::CreateDetector(const string& name) { bool CascadeDetector::Init(const std::string &model_file_path) { detector.load(model_file_path); - return detector.empty(); + return !detector.empty(); } void CascadeDetector::Detect(const cv::Mat &frame, std::vector &objects, std::vector &scores) { From fef4f3373e1c0726719cff982c71fae6657a878f Mon Sep 17 00:00:00 2001 From: luba Date: Mon, 6 Nov 2017 12:58:50 +0300 Subject: [PATCH 12/17] add function detectOnVideo --- samples/detection_demo.cpp | 71 +++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 048d091..5046719 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -11,6 +11,27 @@ const char* kOptions = "{ m model | | path to detector file }" "{ h ? help usage | | print help message }"; +void detectOnVideo( VideoCapture& capture, CascadeDetector& detector){ + Mat frame; + vector objects; + vector scores; + while(true) { + capture >> frame; + detector.Detect(frame, objects, scores); + for(const auto& rect : objects){ + rectangle(frame, rect, Scalar(250, 150, 10)); + } + imshow("capture", frame); + char c = waitKey(33); + if (c == 27) { + break; + } + objects.clear(); + scores.clear(); + } + +} + int main(int argc, const char** argv) { // Parse command line arguments. CommandLineParser parser(argc, argv, kOptions); @@ -31,40 +52,23 @@ int main(int argc, const char** argv) { if(parser.has("m")){ std::string filePathDetector; filePathDetector = parser.get("m"); - if(!detector.Init(filePathDetector)){ - cerr << "Error init detector" << endl; - return -1; - }; + detector.Init(filePathDetector); } - else{ - cerr << "enter key m" << endl; + else{ + cerr << "Error load model"; return -1; } if(parser.has("i")){ + std::string filePath; filePath = parser.get("i"); Mat input = imread(filePath); detector.Detect(input, objects, scores); } else if(parser.has("v")){ - Mat frame; - std::string filePath; - filePath = parser.get("v"); + std::string filePath = parser.get("v");; cv::VideoCapture video(filePath); - - while(true) { - video >> frame; - detector.Detect(frame, objects, scores); - for(const auto& rect : objects){ - rectangle(frame, rect, Scalar(250, 150, 10)); - } - imshow("capture", frame); - char c = waitKey(33); - if (c == 27) { - break; - } - objects.clear(); - } + detectOnVideo(video, detector); video.release(); } else if(parser.has("c")){ @@ -72,22 +76,11 @@ int main(int argc, const char** argv) { VideoCapture cap(0); if(!cap.isOpened()) return -1; - Mat frame; - while (true) { - cap >> frame; - detector.Detect(frame, objects, scores); - for (const auto &rect : objects) { - rectangle(frame, rect, Scalar(250, 150, 10)); - } - imshow("capture", frame); - char c = waitKey(33); - if (c == 27) { - break; - } - objects.clear(); - } - cap.release(); - } + detectOnVideo(cap, detector); + cap.release(); + } + + else{ cerr << "no flag" << endl; } From ec6679b8136ec007d94c924cd1a8fe0ce2a48698 Mon Sep 17 00:00:00 2001 From: luba Date: Mon, 6 Nov 2017 13:02:32 +0300 Subject: [PATCH 13/17] fix CmakeLists --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f9ec5af..f5122c9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,7 @@ file(GLOB incl "../include/*.h*") file(GLOB hdrs "*.h*") file(GLOB srcs "*.cpp") -add_library(${target} STATIC ${srcs} ${hdrs} ${incl} image_processing.cpp) +add_library(${target} STATIC ${srcs} ${hdrs} ${incl}) if (UNIX) target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) endif (UNIX) From 0b2d5802bcd71880d1bcd233169498cc457a56d5 Mon Sep 17 00:00:00 2001 From: tolik Date: Mon, 6 Nov 2017 13:23:58 +0300 Subject: [PATCH 14/17] add exception in CreateDetector --- src/detection.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/detection.cpp b/src/detection.cpp index 9d89b73..6e00f92 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -3,12 +3,18 @@ using std::string; using std::shared_ptr; +using std::invalid_argument; using namespace cv; shared_ptr Detector::CreateDetector(const string& name) { if (name == "cascade") { return std::make_shared(); } + else + throw invalid_argument("invalid name expected cascade"); + + + } From fbd959fa1744caa453e42efa31a4580f7a55e7d1 Mon Sep 17 00:00:00 2001 From: tolik Date: Mon, 6 Nov 2017 13:44:10 +0300 Subject: [PATCH 15/17] add exception in Detect --- samples/detection_demo.cpp | 50 ++++++++++++++++++++++++++++---------- src/detection.cpp | 6 ++--- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/samples/detection_demo.cpp b/samples/detection_demo.cpp index 5046719..810d567 100644 --- a/samples/detection_demo.cpp +++ b/samples/detection_demo.cpp @@ -11,18 +11,23 @@ const char* kOptions = "{ m model | | path to detector file }" "{ h ? help usage | | print help message }"; -void detectOnVideo( VideoCapture& capture, CascadeDetector& detector){ +void detectOnVideo( VideoCapture& capture, shared_ptr detector){ Mat frame; vector objects; vector scores; while(true) { capture >> frame; - detector.Detect(frame, objects, scores); + try { + detector->Detect(frame, objects, scores); + } + catch (char* msg){ + throw msg; + } for(const auto& rect : objects){ rectangle(frame, rect, Scalar(250, 150, 10)); } imshow("capture", frame); - char c = waitKey(33); + int c = waitKey(33); if (c == 27) { break; } @@ -41,8 +46,13 @@ int main(int argc, const char** argv) { parser.printMessage(); return 0; } - - CascadeDetector detector; + std::shared_ptr detector; + try { + detector = Detector::CreateDetector("cascade"); + } + catch (const std::exception& e){ + cout << e.what() << endl; + } std::vector objects; std::vector scores; @@ -52,7 +62,7 @@ int main(int argc, const char** argv) { if(parser.has("m")){ std::string filePathDetector; filePathDetector = parser.get("m"); - detector.Init(filePathDetector); + detector->Init(filePathDetector); } else{ cerr << "Error load model"; @@ -60,15 +70,25 @@ int main(int argc, const char** argv) { } if(parser.has("i")){ - std::string filePath; - filePath = parser.get("i"); - Mat input = imread(filePath); - detector.Detect(input, objects, scores); + std::string filePath = parser.get("i");; + Mat input = imread(filePath); + try { + detector->Detect(input, objects, scores); + } + catch(char* msg){ + throw msg; + } } else if(parser.has("v")){ std::string filePath = parser.get("v");; cv::VideoCapture video(filePath); - detectOnVideo(video, detector); + try { + detectOnVideo(video, detector); + } + catch (char* msg){ + cout << msg; + video.release(); + } video.release(); } else if(parser.has("c")){ @@ -76,11 +96,15 @@ int main(int argc, const char** argv) { VideoCapture cap(0); if(!cap.isOpened()) return -1; - detectOnVideo(cap, detector); + try { + detectOnVideo(cap, detector); + } + catch (char* msg){ + cout << msg; + } cap.release(); } - else{ cerr << "no flag" << endl; } diff --git a/src/detection.cpp b/src/detection.cpp index 6e00f92..2a0d008 100644 --- a/src/detection.cpp +++ b/src/detection.cpp @@ -30,8 +30,6 @@ void CascadeDetector::Detect(const cv::Mat &frame, std::vector &object scores.resize(sc.size()); std::copy(sc.begin(), sc.end(), scores.begin()); } - else { - std::cerr << "Error"; - exit(-1); - } + else + throw "detector is empty"; } From e0e7aba33be552056572423ba2aa472c4bc11d7d Mon Sep 17 00:00:00 2001 From: tolik Date: Mon, 6 Nov 2017 13:47:10 +0300 Subject: [PATCH 16/17] fix headers --- src/image_processing.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/image_processing.cpp b/src/image_processing.cpp index badd196..117f4d2 100644 --- a/src/image_processing.cpp +++ b/src/image_processing.cpp @@ -1,10 +1,13 @@ #include #include + #include -#include +#include + +#include "image_processing.hpp" + -#include "opencv2/core/core.hpp" From aca31fd52dbbbbcb7051fab39e3bd07ad4a29670 Mon Sep 17 00:00:00 2001 From: tolik Date: Mon, 6 Nov 2017 14:00:12 +0300 Subject: [PATCH 17/17] delete spaces --- src/image_processing.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/image_processing.cpp b/src/image_processing.cpp index 117f4d2..8c8330e 100644 --- a/src/image_processing.cpp +++ b/src/image_processing.cpp @@ -1,4 +1,3 @@ - #include #include @@ -7,10 +6,6 @@ #include "image_processing.hpp" - - - - cv::Mat ImageProcessorImpl::CvtColor(const cv::Mat &src, const cv::Rect &roi) { cv::Mat src_copy;